You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoiding naming clashes for result in function contracts #2597
It produces a conflict in shadowing variable names as the following:
#[allow(dead_code, unused_variables)]
#[kanitool :: is_contract_generated(check)]
fn simple_addition_check_222d75(a_renamed : u32, a : u32) -> u32
{
let a_renamed = kani :: internal :: untracked_deref(& a);
let a_renamed_renamed = kani :: internal :: untracked_deref(& a_renamed);
kani :: assume(a_renamed == 5 && a == 4);
let result : u32 = simple_addition_wrapper_222d75(a_renamed, a);
kani :: assert(a_renamed == 4 && a_renamed_renamed == 5, stringify! (a == 4 && a_renamed == 5));
std :: mem :: forget(a_renamed);
std :: mem :: forget(a_renamed_renamed);
result
}
Specifically naming your variables "a" and "a_renamed" seems to be unlikely to come up in practice, while naming a variable "result" is far more likely. Moreover, the specific order in which variables are binded is dependent on the order in which they are populated into a hashmap, so it is difficult to produce this error.
The same can technically be done with function names via already knowing the hash that will be produced and intentionally creating a function with that same hash, but to do that would require quite a lot of hacking and won't come up without nefarious intent.
This is related to #3026, where the internal representation of the variables in checks leaks out to the user. If we rename result internally to avoid collisions, there might also be error leaks with this internal result name
Hey @pi314mm, have you tested whether this name clash actually causes compile/semantic failures? The reason I ask is there are theoretically hygiene protections in macros which (I would hope) would mean that the arg1 and arg1_renamed cases should actually not conflict, but I could be wrong.
Activity
pi314mm commentedon May 28, 2024
There are also more complicated naming clashes in play.
For checking
fn_name(arg1 : u32, arg2 : u32)we produce something like
If the user decided to name two of their input variables as "a" and "a_renamed" such as in this test code:
It produces a conflict in shadowing variable names as the following:
Specifically naming your variables "a" and "a_renamed" seems to be unlikely to come up in practice, while naming a variable "result" is far more likely. Moreover, the specific order in which variables are binded is dependent on the order in which they are populated into a hashmap, so it is difficult to produce this error.
The same can technically be done with function names via already knowing the hash that will be produced and intentionally creating a function with that same hash, but to do that would require quite a lot of hacking and won't come up without nefarious intent.
This is related to #3026, where the internal representation of the variables in checks leaks out to the user. If we rename result internally to avoid collisions, there might also be error leaks with this internal result name
JustusAdam commentedon May 28, 2024
Hey @pi314mm, have you tested whether this name clash actually causes compile/semantic failures? The reason I ask is there are theoretically hygiene protections in macros which (I would hope) would mean that the
arg1andarg1_renamedcases should actually not conflict, but I could be wrong.Change ensures into closures (#3207)