Skip to content
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: Recursive constant initialization was not checked if in constructor #2862

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 Oct 7, 2022
dd2ec2e
Update RELEASE_NOTES.md
MikaelMayer Oct 7, 2022
75344fb
Added review comment use case and fixed the code
MikaelMayer Oct 7, 2022
b03a40a
Merge branch 'fix-2727-soundness-constant' of https://github.com/dafn…
MikaelMayer Oct 7, 2022
4f7300f
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer Oct 7, 2022
f7e90a2
Better handling of alternatives
MikaelMayer Oct 11, 2022
04eeb43
Refactoring
MikaelMayer Oct 11, 2022
a4d3a27
Merge branch 'fix-2727-soundness-constant' of https://github.com/dafn…
MikaelMayer Oct 11, 2022
56dbf3b
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer Oct 11, 2022
5568664
Fix CI
MikaelMayer Oct 11, 2022
bac8625
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer Oct 12, 2022
bd717d1
Create 2862.fix
MikaelMayer Oct 12, 2022
e956e87
Merge branch 'fix-2727-soundness-constant' of https://github.com/dafn…
MikaelMayer Oct 12, 2022
0c20291
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer Oct 14, 2022
baa398f
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer Oct 14, 2022
2e98a55
Prevent constants to be assigned twice
MikaelMayer Oct 14, 2022
f18254b
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer Oct 21, 2022
d7fd892
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer Oct 24, 2022
b53829c
Merge branch 'master' into fix-2727-soundness-constant
MikaelMayer Oct 24, 2022
3ac8137
One more soundness fix
MikaelMayer Oct 26, 2022
5555cde
One more soundness fix
MikaelMayer Oct 26, 2022
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
Prev Previous commit
One more soundness fix
  • Loading branch information
MikaelMayer committed Oct 26, 2022
commit 5555cde4be8a15bb63e149d5fc5cc9620257ae44
11 changes: 10 additions & 1 deletion Source/DafnyCore/Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9281,13 +9281,22 @@ void LiberalRHSVisit(Expression expr) {
}
}
}
} else if (expr is FunctionCallExpr { tok: var tok, Function: { IsStatic: false } function }) {

return null; // No problem to declare for this field
} else if (expr is FunctionCallExpr { tok: var tok, Function: { IsStatic: false } function } && visited.Count > 0) {
var msg = "Constant field '" + visited[0].Name + "' cannot be accessed before 'new;'";
for (var i = 1; i < visited.Count; i++) {
msg += (i == 1 ? " because of the dependency " + visited[0].Name : "") + " -> " + visited[i].Name;
}
msg += ", " + (visited.Count > 1 ? "and" : "because") + " '" + visited[visited.Count - 1].Name + "' depends on the non-static function '" + function.Name + "' that can potentially read other uninitialized constants.";
return (msg, visited[0]);
} else if (expr is ThisExpr && visited.Count > 0) {
var msg = "Constant field '" + visited[0].Name + "' cannot be accessed before 'new;'";
for (var i = 1; i < visited.Count; i++) {
msg += (i == 1 ? " because of the dependency " + visited[0].Name : "") + " -> " + visited[i].Name;
}
msg += ", " + (visited.Count > 1 ? "and" : "because") + " '" + visited[visited.Count - 1].Name + "' depends on the object 'this' itself, that can potentially read other uninitialized constants.";
return (msg, visited[0]);
}
foreach (var subExpr in expr.SubExpressions) {
if (GetErrorIfConstantFieldNotInitialized(subExpr, visited) is var msgField && msgField != null) {
Expand Down
12 changes: 12 additions & 0 deletions Test/git-issues/git-issue-2727.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ class C extends Tr {
}
}

class DeepThisError {
const d: Dummy
const t: set<DeepThisError> := {this}

constructor() {
var x := set n: DeepThisError | n in t :: n.d.i;
// Error above: cannot use 't' because it depends on "this" which might have uninitialized constants
d := new Dummy(1);
new;
}
}

method Main() {
var c := new SecondInitializationError(5);
var d := new RecursiveError();
Expand Down
3 changes: 2 additions & 1 deletion Test/git-issues/git-issue-2727.dfy.expect
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ git-issue-2727.dfy(139,4): Error: The constant b cannot be assigned twice.
git-issue-2727.dfy(150,9): Error: Constant field 'a' cannot be accessed before 'new;', because 'a' depends on the non-static function 'B' that can potentially read other uninitialized constants.
git-issue-2727.dfy(162,9): Error: Constant field 'c' cannot be accessed before 'new;' because of the dependency c -> a, and 'a' depends on the non-static function 'B' that can potentially read other uninitialized constants.
git-issue-2727.dfy(178,9): Error: Constant field 'a' cannot be accessed before 'new;', because 'a' depends on the non-static function 'B' that can potentially read other uninitialized constants.
12 resolution/type errors detected in git-issue-2727.dfy
git-issue-2727.dfy(189,41): Error: Constant field 't' cannot be accessed before 'new;', because 't' depends on the object 'this' itself, that can potentially read other uninitialized constants.
13 resolution/type errors detected in git-issue-2727.dfy