Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl BindingContext {
pub fn new() -> Self {
let mut ctx = BindingContext { bindings: HashMap::new(), names: HashSet::new() };
ctx.push_new_binding(&Rc::from("static"));
ctx.push_new_binding(&Rc::from("unknown"));
ctx
}
}
Expand Down Expand Up @@ -154,9 +155,16 @@ impl<'a> LifetimeDefaults<'a> {
) -> LifetimeState {
match lifetime {
[] => LifetimeState::Unseen,
[id] => LifetimeState::Single(
self.bindings.get_or_push_new_binding(id, |name| new_bindings.push(name.clone())),
),
[id] => {
let binding = self
.bindings
.get_or_push_new_binding(id, |name| new_bindings.push(name.clone()));
if binding.as_ref() == "unknown" {
LifetimeState::Unknown
} else {
LifetimeState::Single(binding)
}
}
// TODO(b/454627672): multiple variables.
_ => LifetimeState::Unknown,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,37 @@ fn test_unique_lifetime_returned_for_single_ref() -> Result<()> {
Ok(())
}

#[gtest]
fn test_unknown_lifetime_inhibits_default_lifetimes() -> Result<()> {
let ir = ir_from_assumed_lifetimes_cc(
&(with_full_lifetime_macros()
+ r#"
int& f(int& $unknown i1);
"#),
)?;
let dir = lifetime_defaults_transform(&ir)?;
assert_ir_matches!(
dir,
quote! {
Func {
cc_name: "f",
rs_name: "f", ...
return_type: CcType { ... explicit_lifetimes: [] ... }, ...
params: [
FuncParam {
type_: CcType { ... explicit_lifetimes: [] ... },
identifier: "i1", ...
}
],
...
lifetime_inputs: [],
...
}
}
);
Ok(())
}

#[gtest]
fn test_no_lifetime_returned_for_distinct_ref_parameters() -> Result<()> {
let ir = ir_from_assumed_lifetimes_cc(
Expand Down Expand Up @@ -775,14 +806,19 @@ fn test_param_lifetimebound_to_this_in_constructor_explicit_lifetime() -> Result
}

#[gtest]
fn test_binding_context_has_static() -> Result<()> {
fn test_binding_context_has_builtin_lifetimes() -> Result<()> {
let mut ctx = BindingContext::new();
let mut called = false;
assert_eq!(
ctx.get_or_push_new_binding(&Rc::from("static"), |_| called = true),
"static".into()
);
assert!(!called);
assert_eq!(
ctx.get_or_push_new_binding(&Rc::from("unknown"), |_| called = true),
"unknown".into()
);
assert!(!called);
Ok(())
}

Expand Down Expand Up @@ -871,6 +907,44 @@ fn test_struct_binds_lifetime_param() -> Result<()> {
Ok(())
}

#[gtest]
fn test_struct_shadows_unknown_lifetime_param() -> Result<()> {
let ir = ir_from_assumed_lifetimes_cc(
&(with_full_lifetime_macros()
+ r#"
struct LIFETIME_PARAMS("unknown") S { int& $unknown f(); };
"#),
)?;
let dir = lifetime_defaults_transform(&ir)?;
assert_ir_matches!(
dir,
quote! {
Record {
...
cc_name: "S",
...
lifetime_inputs: ["unknown_0"],
...
}
}
);
assert_ir_matches!(
dir,
quote! {
Func {
cc_name: "f",
rs_name: "f", ...
return_type: CcType { ... explicit_lifetimes: ["unknown_0"] ... }, ...
lifetime_params: [],
...
lifetime_inputs: [],
...
}
}
);
Ok(())
}

#[gtest]
fn test_struct_does_not_shadow_unrelated_lifetime_param() -> Result<()> {
let ir = ir_from_assumed_lifetimes_cc(
Expand Down
1 change: 1 addition & 0 deletions rs_bindings_from_cc/ir_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub fn with_full_lifetime_macros() -> String {
result.push_str(&format!("#define ${} $({})\n", l, l));
}
result.push_str("#define $static $(static)\n");
result.push_str("#define $unknown $(unknown)\n");
result
}

Expand Down