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

feat: continue statements #1839

Merged
merged 23 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d710d93
Parse and print
RustanLeino Feb 17, 2022
db5dbf9
Resolve
RustanLeino Feb 17, 2022
0bcfe24
Verify
RustanLeino Feb 17, 2022
d83bd68
fix: Labeled-statement regressions
RustanLeino Feb 18, 2022
c7810d6
Compile
RustanLeino Feb 18, 2022
46e9549
Don’t warn about unused C++ labels
RustanLeino Feb 18, 2022
364d5ad
Simplify target code
RustanLeino Feb 18, 2022
bceb32a
Document
RustanLeino Feb 18, 2022
a3b82fe
Remove whitespace
RustanLeino Feb 18, 2022
1241fde
Merge branch 'master' into issue-1738-continue
RustanLeino Feb 18, 2022
22356c9
Add release notes
RustanLeino Feb 18, 2022
cf43bd6
Improve error messages
RustanLeino Feb 18, 2022
31a40ee
Improve wording in documentation
RustanLeino Feb 18, 2022
e32af49
Explain that loop invariants are not checked at break/continue
RustanLeino Feb 18, 2022
617063e
Update RELEASE_NOTES.md
RustanLeino Feb 18, 2022
bed7e41
Merge branch 'issue-1738-continue' of https://github.com/RustanLeino/…
RustanLeino Feb 18, 2022
60a0846
Fix whitespace
RustanLeino Feb 18, 2022
b563fab
Merge branch 'master' into issue-1738-continue
RustanLeino Feb 18, 2022
62c6f70
chore: Rename BreakCount to BreakAndContinueCount
RustanLeino Feb 21, 2022
89e3227
Assert condition that should hold from class invariant
RustanLeino Feb 21, 2022
caaa60a
Merge branch 'master' into issue-1738-continue
RustanLeino Feb 21, 2022
e10fb7c
Update Source/Dafny/AST/DafnyAst.cs
RustanLeino Feb 22, 2022
a1e2430
Merge branch 'master' into issue-1738-continue
RustanLeino Feb 22, 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
14 changes: 7 additions & 7 deletions Source/Dafny/AST/DafnyAst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7194,11 +7194,11 @@ public class BreakStmt : Statement {
public readonly IToken TargetLabel;
public readonly bool IsContinue;
public string Kind => IsContinue ? "continue" : "break";
public readonly int BreakCount; // this includes the final "continue", if any
public readonly int BreakAndContinueCount; // this includes the final "continue", if any
RustanLeino marked this conversation as resolved.
Show resolved Hide resolved
public Statement TargetStmt; // filled in during resolution
[ContractInvariantMethod]
void ObjectInvariant() {
Contract.Invariant(TargetLabel != null || 1 <= BreakCount);
Contract.Invariant(TargetLabel != null || 1 <= BreakAndContinueCount);
}

public BreakStmt(IToken tok, IToken endTok, IToken targetLabel, bool isContinue)
Expand All @@ -7211,15 +7211,15 @@ public BreakStmt(IToken tok, IToken endTok, IToken targetLabel, bool isContinue)
}

/// <summary>
/// For "isContinue == false", represents the statement "break ^breakCount ;".
/// For "isContinue == true", represents the statement "break ^(breakCount - 1) continue;".
/// For "isContinue == false", represents the statement "break ^breakAndContinueCount ;".
/// For "isContinue == true", represents the statement "break ^(breakAndContinueCount - 1) continue;".
/// </summary>
public BreakStmt(IToken tok, IToken endTok, int breakCount, bool isContinue)
public BreakStmt(IToken tok, IToken endTok, int breakAndContinueCount, bool isContinue)
: base(tok, endTok) {
Contract.Requires(tok != null);
Contract.Requires(endTok != null);
Contract.Requires(1 <= breakCount);
this.BreakCount = breakCount;
Contract.Requires(1 <= breakAndContinueCount);
this.BreakAndContinueCount = breakAndContinueCount;
this.IsContinue = isContinue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Dafny/AST/Printer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ public void PrintStatement(Statement stmt, int indent) {
if (s.TargetLabel != null) {
wr.Write($"{s.Kind} {s.TargetLabel.val};");
} else {
for (int i = 0; i < s.BreakCount - 1; i++) {
for (int i = 0; i < s.BreakAndContinueCount - 1; i++) {
wr.Write("break ");
}
wr.Write($"{s.Kind};");
Expand Down
2 changes: 1 addition & 1 deletion Source/Dafny/Cloner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public virtual Statement CloneStmt(Statement stmt) {
if (s.TargetLabel != null) {
r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.TargetLabel, s.IsContinue);
} else {
r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.BreakCount, s.IsContinue);
r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.BreakAndContinueCount, s.IsContinue);
}

} else if (stmt is ReturnStmt) {
Expand Down
10 changes: 5 additions & 5 deletions Source/Dafny/Dafny.atg
Original file line number Diff line number Diff line change
Expand Up @@ -2479,24 +2479,24 @@ BreakStmt<out Statement/*!*/ s>
= (. var isContinue = false;
IToken start = Token.NoToken;
IToken label = null;
int breakCount = 1;
int breakAndContinueCount = 1;
.)
( "continue" (. start = t; isContinue = true; .)
[ LabelName<out label> ]
| "break" (. start = t; .)
( LabelName<out label>
| { "break" (. breakCount++; .)
| { "break" (. breakAndContinueCount++; .)
}
[ "continue" (. breakCount++; isContinue = true; .)
[ "continue" (. breakAndContinueCount++; isContinue = true; .)
]
)
)
SYNC
";"
(. Contract.Assert(label == null || breakCount == 1);
(. Contract.Assert(label == null || breakAndContinueCount == 1);
s = label != null ?
new BreakStmt(start, t, label, isContinue) :
new BreakStmt(start, t, breakCount, isContinue);
new BreakStmt(start, t, breakAndContinueCount, isContinue);
.)
.

Expand Down
2 changes: 1 addition & 1 deletion Source/Dafny/RefinementTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ void CheckIsOkayNewStatement(Statement s, Stack<string> labels, int loopLevels)
Reporter.Error(MessageSource.RefinementTransformer, s, "yield statements are not allowed in skeletons");
} else if (s is BreakStmt) {
var b = (BreakStmt)s;
if (b.TargetLabel != null ? !labels.Contains(b.TargetLabel.val) : loopLevels < b.BreakCount) {
if (b.TargetLabel != null ? !labels.Contains(b.TargetLabel.val) : loopLevels < b.BreakAndContinueCount) {
Reporter.Error(MessageSource.RefinementTransformer, s, $"{b.Kind} statement in skeleton is not allowed to break outside the skeleton fragment");
}
} else if (s is AssignStmt) {
Expand Down
11 changes: 6 additions & 5 deletions Source/Dafny/Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11075,16 +11075,17 @@ public void ResolveStatement(Statement stmt, ICodeContext codeContext) {
s.TargetStmt = target;
}
} else {
var jumpStmt = s.BreakCount == 1 ?
Contract.Assert(1 <= s.BreakAndContinueCount); // follows from BreakStmt class invariant and the guard for this "else" branch
var jumpStmt = s.BreakAndContinueCount == 1 ?
$"a non-labeled '{s.Kind}' statement" :
$"a '{Util.Repeat(s.BreakCount - 1, "break ")}{s.Kind}' statement";
$"a '{Util.Repeat(s.BreakAndContinueCount - 1, "break ")}{s.Kind}' statement";
if (loopStack.Count == 0) {
reporter.Error(MessageSource.Resolver, s, $"{jumpStmt} is allowed only in loops");
} else if (loopStack.Count < s.BreakCount) {
} else if (loopStack.Count < s.BreakAndContinueCount) {
reporter.Error(MessageSource.Resolver, s,
$"{jumpStmt} is allowed only in contexts with {s.BreakCount} enclosing loops, but the current context only has {loopStack.Count}");
$"{jumpStmt} is allowed only in contexts with {s.BreakAndContinueCount} enclosing loops, but the current context only has {loopStack.Count}");
} else {
Statement target = loopStack[loopStack.Count - s.BreakCount];
Statement target = loopStack[loopStack.Count - s.BreakAndContinueCount];
if (target.Labels == null) {
// make sure there is a label, because the compiler and translator will want to see a unique ID
target.Labels = new LList<Label>(new Label(target.Tok, null), null);
Expand Down
2 changes: 1 addition & 1 deletion Source/Dafny/Substituter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ protected virtual Statement SubstStmt(Statement stmt) {
if (s.TargetLabel != null) {
rr = new BreakStmt(s.Tok, s.EndTok, s.TargetLabel, s.IsContinue);
} else {
rr = new BreakStmt(s.Tok, s.EndTok, s.BreakCount, s.IsContinue);
rr = new BreakStmt(s.Tok, s.EndTok, s.BreakAndContinueCount, s.IsContinue);
}
// r.TargetStmt will be filled in as later
List<BreakStmt> breaks;
Expand Down