Skip to content

Conversation

@MikaelMayer
Copy link
Member

@MikaelMayer MikaelMayer commented Jun 23, 2025

Fixes #6261

The root cause of the bug was that shadowed variables are never accessible in Dafny anymore except for one case: Output variables, through the return statement.
Indeed, a return statement with an expression assigns the result of the expression to the (unique) output variable, and then returns. Even in the presence of shadowing when another local variable overrides the output variable, the (outer) output variable is correctly assigned in the compiler.
I had not considered this case when considering enabling shadowing.

What was changed?

  • Whether a variable's name results in the shadowable name instead of the unique name is now more constrained: it requires that the variable name is different from any output variables.

How has this been tested?

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@MikaelMayer MikaelMayer enabled auto-merge (squash) June 24, 2025 19:55
return preventShadowing ? v.GetOrCreateCompileName(currentIdGenerator) : v.CompileNameShadowable;
var canShadow = !preventShadowing &&
!(enclosingMethod != null && enclosingMethod.Outs.Any(outVar => outVar.Name == v.Name));
return canShadow ? v.CompileNameShadowable : v.GetOrCreateCompileName(currentIdGenerator);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it acceptable to shadow a name declared in the body of the enclosing method, not necessarily among the out-parameters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Yes, because there is no way in Dafny to refer to outer variables, so there won't be an ambiguity in the generated code.

@MikaelMayer MikaelMayer merged commit 88f0ea0 into master Jun 24, 2025
34 of 35 checks passed
@MikaelMayer MikaelMayer deleted the fix-6261-shadowing-output-variables branch June 24, 2025 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad Rust code generated when local variable shares name with return variable.

2 participants