-
Notifications
You must be signed in to change notification settings - Fork 274
Fix: Recursive constant initialization was not checked if in constructor #2862
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
Open
MikaelMayer
wants to merge
21
commits into
master
Choose a base branch
from
fix-2727-soundness-constant
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0b34697
Fix: Recursive constant initialization was not checked if in constructor
MikaelMayer dd2ec2e
Update RELEASE_NOTES.md
MikaelMayer 75344fb
Added review comment use case and fixed the code
MikaelMayer b03a40a
Merge branch 'fix-2727-soundness-constant' of https://github.com/dafn…
MikaelMayer 4f7300f
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer f7e90a2
Better handling of alternatives
MikaelMayer 04eeb43
Refactoring
MikaelMayer a4d3a27
Merge branch 'fix-2727-soundness-constant' of https://github.com/dafn…
MikaelMayer 56dbf3b
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer 5568664
Fix CI
MikaelMayer bac8625
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer bd717d1
Create 2862.fix
MikaelMayer e956e87
Merge branch 'fix-2727-soundness-constant' of https://github.com/dafn…
MikaelMayer 0c20291
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer baa398f
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer 2e98a55
Prevent constants to be assigned twice
MikaelMayer f18254b
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer d7fd892
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer b53829c
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer 3ac8137
One more soundness fix
MikaelMayer 5555cde
One more soundness fix
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2846,7 +2846,7 @@ public void ResolveTopLevelDecls_Core(List<TopLevelDecl/*!*/>/*!*/ declarations, | |
} | ||
|
||
// ---------------------------------- Pass 1 ---------------------------------- | ||
// This pass: | ||
// This pass or phase: | ||
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. I keep forgetting that these are called "pass", I'm always looking for "phase". Next time, if I forget again, I will be able to find them. |
||
// * checks that type inference was able to determine all types | ||
// * check that shared destructors in datatypes are in agreement | ||
// * fills in the .ResolvedOp field of binary expressions | ||
|
@@ -13768,8 +13768,65 @@ void ResolveBlockStatement(BlockStmt blockStmt, ResolutionContext resolutionCont | |
var div = (DividedBlockStmt)blockStmt; | ||
Contract.Assert(currentMethod is Constructor); // divided bodies occur only in class constructors | ||
Contract.Assert(!resolutionContext.InFirstPhaseConstructor); // divided bodies are never nested | ||
// In the first phase of the constructor, we keep track of constants which are fully determined. | ||
// We prevent the reading of constants which have dependencies that are yet unassigned constants | ||
var constantsAlreadyAssigned = new HashSet<ConstantField>(); | ||
var constantsWithErrors = new HashSet<ConstantField>(); | ||
|
||
// Returns an error if the expression cannot be fully determined | ||
(string, ConstantField)? GetErrorIfNotFullyDetermined(Expression expr, List<ConstantField> visited) { | ||
if (expr is MemberSelectExpr { Member: ConstantField field } memberSelectExpr && Expression.AsThis(memberSelectExpr.Obj) != null) { | ||
if (visited.IndexOf(field) is var index && index >= 0) { | ||
var msg = "Please break this constant initialization cycle: " + visited[index].Name; | ||
for (var i = index + 1; i < visited.Count; i++) { | ||
msg += " -> " + visited[i].Name; | ||
} | ||
msg += " -> " + field.Name; | ||
return (msg, field); | ||
} | ||
if (field.Rhs == null && !constantsAlreadyAssigned.Contains(field)) { | ||
var msg = "Missing initialization of field "; | ||
for (var i = 0; i < visited.Count; i++) { | ||
msg += (i == 0 ? "through the dependency " : "") + visited[i].Name + " -> "; | ||
} | ||
msg += field.Name + ", which needs to be assigned at this point."; | ||
return (msg, field); | ||
} | ||
if (field.Rhs != null) { | ||
foreach (var subExpr in field.Rhs.SubExpressions) { | ||
if (GetErrorIfNotFullyDetermined(subExpr, visited.Append(field).ToList()) is var msgField && msgField != null) { | ||
return msgField; | ||
} | ||
} | ||
} | ||
} | ||
foreach (var subExpr in expr.SubExpressions) { | ||
if (GetErrorIfNotFullyDetermined(subExpr, visited) is var msgField && msgField != null) { | ||
return msgField; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
foreach (Statement ss in div.BodyInit) { | ||
ResolveStatementWithLabels(ss, resolutionContext with { InFirstPhaseConstructor = true }); | ||
var subExpressions = (ss is UpdateStmt updateStmt | ||
? updateStmt.Rhss.SelectMany(rhs => rhs.SubExpressions).ToList() | ||
: Microsoft.Dafny.Triggers.ExprExtensions.AllSubExpressions(ss, false, false)); | ||
foreach (var rhs in subExpressions) { | ||
if (GetErrorIfNotFullyDetermined(rhs, new List<ConstantField>()) is var msgField && msgField != null && | ||
!constantsWithErrors.Contains(msgField.Value.Item2)) { | ||
reporter.Error(MessageSource.Resolver, rhs, "Constant not initialized yet. " + msgField.Value.Item1); | ||
constantsWithErrors.Add(msgField.Value.Item2); | ||
} | ||
} | ||
if (ss is UpdateStmt updateStmt2) { | ||
foreach (var lhs in updateStmt2.Lhss) { | ||
if (lhs is MemberSelectExpr { Member: ConstantField { IsMutable: false } field } memberSelectExpr && Expression.AsThis(memberSelectExpr.Obj) != null) { | ||
constantsAlreadyAssigned.Add(field); | ||
} | ||
} | ||
} | ||
} | ||
foreach (Statement ss in div.BodyProper) { | ||
ResolveStatementWithLabels(ss, resolutionContext); | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// RUN: %dafny_0 -compile:0 "%s" > "%t" | ||
// RUN: %diff "%s.expect" "%t" | ||
|
||
class C { | ||
const a := b + b | ||
const b: int | ||
|
||
constructor (x: int) { | ||
var k := a; | ||
print a, "\n"; | ||
b := x; | ||
assert k == a; | ||
print a, "\n"; | ||
if k != a { | ||
var y := 5 / 0; // this can crash | ||
} | ||
} | ||
} | ||
|
||
class D { | ||
const a: int := b | ||
const b: int | ||
|
||
constructor () { | ||
b := a + 1; | ||
new; | ||
assert false; | ||
} | ||
} | ||
|
||
method Main() { | ||
var c := new C(5); | ||
var d := new D(); | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
git-issue-2727.dfy(9,13): Error: Constant not initialized yet. Missing initialization of field through the dependency a -> b, which needs to be assigned at this point. | ||
git-issue-2727.dfy(25,11): Error: Constant not initialized yet. Missing initialization of field through the dependency a -> b, which needs to be assigned at this point. | ||
2 resolution/type errors detected in git-issue-2727.dfy |
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.
This has nothing to do with this PR, but I tried having dafny in a folder with space in its name and I had to fix this to make it work.