-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Constant initialization in the Dafny-to-Rust code generator #5837
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb71839
Fix: Constant initialization in the Dafny-to-Rust code generator
MikaelMayer 2eb3b7c
Fixed CI issues
MikaelMayer 1d6d383
Merge branch 'master' into fix-5833-const-initialization
MikaelMayer c0df44f
Review comments
MikaelMayer e3ad794
Merge branch 'fix-5833-const-initialization' of https://github.com/da…
MikaelMayer 6b301e4
Merge branch 'master' into fix-5833-const-initialization
MikaelMayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,8 +38,8 @@ module {:extern "DCOMP"} DafnyToRustCompiler { | |
if pointerType.Raw? then "construct" else "construct_object" | ||
const modify_macro := R.dafny_runtime.MSel(if pointerType.Raw? then "modify!" else "md!").AsExpr() | ||
const read_macro := R.dafny_runtime.MSel(if pointerType.Raw? then "read!" else "rd!").AsExpr() | ||
const modify_field_macro := R.dafny_runtime.MSel("modify_field!").AsExpr() | ||
const read_field_macro := R.dafny_runtime.MSel("read_field!").AsExpr() | ||
const modify_mutable_field_macro := R.dafny_runtime.MSel("modify_field!").AsExpr() | ||
const read_mutable_field_macro := R.dafny_runtime.MSel("read_field!").AsExpr() | ||
|
||
function Object(underlying: R.Type): R.Type { | ||
if pointerType.Raw? then R.PtrType(underlying) else R.ObjectType(underlying) | ||
|
@@ -1613,13 +1613,13 @@ module {:extern "DCOMP"} DafnyToRustCompiler { | |
newEnv := newEnv.RemoveAssigned(isAssignedVar); | ||
} else { | ||
// Already assigned, safe to override | ||
generated := modify_field_macro.Apply([read_macro.Apply1(thisInConstructor).Sel(fieldName), rhs]); | ||
generated := modify_mutable_field_macro.Apply([read_macro.Apply1(thisInConstructor).Sel(fieldName), rhs]); | ||
} | ||
case _ => | ||
if onExpr != R.Identifier("self") { | ||
onExpr := read_macro.Apply1(onExpr); | ||
} | ||
generated := modify_field_macro.Apply([onExpr.Sel(fieldName), rhs]); | ||
generated := modify_mutable_field_macro.Apply([onExpr.Sel(fieldName), rhs]); | ||
} | ||
readIdents := recIdents; | ||
needsIIFE := false; | ||
|
@@ -3265,7 +3265,7 @@ module {:extern "DCOMP"} DafnyToRustCompiler { | |
readIdents := recIdents; | ||
return; | ||
} | ||
case Select(on, field, isConstant, isDatatype, fieldType) => { | ||
case Select(on, field, fieldMutability, isDatatype, fieldType) => { | ||
if on.Companion? || on.ExternCompanion? { | ||
var onExpr, onOwned, recIdents := GenExpr(on, selfIdent, env, OwnershipAutoBorrowed); | ||
|
||
|
@@ -3298,11 +3298,14 @@ module {:extern "DCOMP"} DafnyToRustCompiler { | |
r := read_macro.Apply1(r); | ||
} | ||
r := r.Sel(escapeVar(field)); | ||
if isConstant { | ||
if fieldMutability.ConstantField? { | ||
r := r.Apply0(); | ||
r := r.Clone(); // self could be &mut, so to avoid any borrow checker problem, we clone the value. | ||
} else if fieldMutability.InternalConstantField? { | ||
r := r.Clone(); | ||
} else { | ||
r := read_field_macro.Apply1(r); // Already contains a clone. | ||
assert fieldMutability.MutableField?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't a match statement be a little cleaner? (Instead of the chained if) |
||
r := read_mutable_field_macro.Apply1(r); // Already contains a clone. | ||
} | ||
r, resultingOwnership := FromOwned(r, expectedOwnership); | ||
readIdents := recIdents; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't understand "InternalConstantField" until I looked at the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ClassMutableField is ok, but the last one is really internal, so it should be ClassInternalConstantField, all right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the comment, that helps