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 all 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
3 changes: 1 addition & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
# 3.4.2

- fix: No output when compiling to JavaScript on Windows (https://github.com/dafny-lang/dafny/pull/1824)
- fix: CanCall assumptions for loop invariants (https://github.com/dafny-lang/dafny/pull/1813)
- fix: Behavior of the C# runtime in a concurrent setting (https://github.com/dafny-lang/dafny/pull/1780)
- feat: `continue` statements. Like Dafny's `break` statements, these come in two forms: one that uses a label to name the continue target and one that specifies the continue target by nesting level. See section 19.2 of the Reference Manual. (https://github.com/dafny-lang/dafny/pull/1839)


# 3.4.1
Expand Down
26 changes: 19 additions & 7 deletions Source/Dafny/AST/DafnyAst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7187,28 +7187,40 @@ public static string SingleName(Expression e) {
}
}

/// <summary>
/// Class "BreakStmt" represents both "break" and "continue" statements.
/// </summary>
public class BreakStmt : Statement {
public readonly IToken TargetLabel;
public readonly int BreakCount;
public Statement TargetStmt; // filled in during resolution
public readonly bool IsContinue;
public string Kind => IsContinue ? "continue" : "break";
public readonly int BreakAndContinueCount;
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)
public BreakStmt(IToken tok, IToken endTok, IToken targetLabel, bool isContinue)
: base(tok, endTok) {
Contract.Requires(tok != null);
Contract.Requires(endTok != null);
Contract.Requires(targetLabel != null);
this.TargetLabel = targetLabel;
this.IsContinue = isContinue;
}
public BreakStmt(IToken tok, IToken endTok, int breakCount)

/// <summary>
/// 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 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
12 changes: 5 additions & 7 deletions Source/Dafny/AST/Printer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,16 +1205,14 @@ public void PrintStatement(Statement stmt, int indent) {
wr.Write(";");

} else if (stmt is BreakStmt) {
BreakStmt s = (BreakStmt)stmt;
var s = (BreakStmt)stmt;
if (s.TargetLabel != null) {
wr.Write("break {0};", s.TargetLabel);
wr.Write($"{s.Kind} {s.TargetLabel.val};");
} else {
string sep = "";
for (int i = 0; i < s.BreakCount; i++) {
wr.Write("{0}break", sep);
sep = " ";
for (int i = 0; i < s.BreakAndContinueCount - 1; i++) {
wr.Write("break ");
}
wr.Write(";");
wr.Write($"{s.Kind};");
}

} else if (stmt is ProduceStmt) {
Expand Down
4 changes: 2 additions & 2 deletions Source/Dafny/Cloner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,9 @@ public virtual Statement CloneStmt(Statement stmt) {
} else if (stmt is BreakStmt) {
var s = (BreakStmt)stmt;
if (s.TargetLabel != null) {
r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.TargetLabel);
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);
r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.BreakAndContinueCount, s.IsContinue);
}

} else if (stmt is ReturnStmt) {
Expand Down
12 changes: 9 additions & 3 deletions Source/Dafny/Compilers/Compiler-Csharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,9 +1724,10 @@ protected override void EmitReturn(List<Formal> outParams, ConcreteSyntaxTree wr
wr.WriteLine($"return {returnExpr};");
}

protected override ConcreteSyntaxTree CreateLabeledCode(string label, ConcreteSyntaxTree wr) {
protected override ConcreteSyntaxTree CreateLabeledCode(string label, bool createContinueLabel, ConcreteSyntaxTree wr) {
var w = wr.Fork();
wr.Fork(-1).WriteLine($"after_{label}: ;");
var prefix = createContinueLabel ? "continue_" : "after_";
wr.Fork(-1).WriteLine($"{prefix}{label}: ;");
return w;
}

Expand All @@ -1738,6 +1739,10 @@ protected override void EmitBreak(string/*?*/ label, ConcreteSyntaxTree wr) {
}
}

protected override void EmitContinue(string label, ConcreteSyntaxTree wr) {
wr.WriteLine("goto continue_{0};", label);
}

protected override void EmitYield(ConcreteSyntaxTree wr) {
wr.WriteLine("yield return null;");
}
Expand All @@ -1760,7 +1765,7 @@ protected override void EmitHalt(Bpl.IToken tok, Expression/*?*/ messageExpr, Co
}

protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loopIndex, bool goingUp, string /*?*/ endVarName,
List<Statement> body, ConcreteSyntaxTree wr) {
List<Statement> body, LList<Label> labels, ConcreteSyntaxTree wr) {

wr.Write($"for ({TypeName(loopIndex.Type, wr, tok)} {loopIndex.CompileName} = ");
var startWr = wr.Fork();
Expand All @@ -1775,6 +1780,7 @@ protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loop
bodyWr = wr.NewBlock($"; )");
bodyWr.WriteLine($"{loopIndex.CompileName}--;");
}
bodyWr = EmitContinueLabel(labels, bodyWr);
TrStmtList(body, bodyWr);

return startWr;
Expand Down
13 changes: 9 additions & 4 deletions Source/Dafny/Compilers/Compiler-cpp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,9 +1233,10 @@ protected override void EmitReturn(List<Formal> outParams, ConcreteSyntaxTree wr
}
}

protected override ConcreteSyntaxTree CreateLabeledCode(string label, ConcreteSyntaxTree wr) {
protected override ConcreteSyntaxTree CreateLabeledCode(string label, bool createContinueLabel, ConcreteSyntaxTree wr) {
var w = wr.Fork();
wr.Fork(-1).WriteLine("after_{0}: ;", label);
var prefix = createContinueLabel ? "continue_" : "after_";
wr.Fork(-1).WriteLine($"{prefix}{label}: ;");
return w;
}

Expand All @@ -1247,6 +1248,10 @@ protected override void EmitBreak(string/*?*/ label, ConcreteSyntaxTree wr) {
}
}

protected override void EmitContinue(string label, ConcreteSyntaxTree wr) {
wr.WriteLine("goto continue_{0};", label);
}

protected override void EmitYield(ConcreteSyntaxTree wr) {
throw NotSupported("EmitYield");
}
Expand All @@ -1269,7 +1274,7 @@ protected override void EmitHalt(Bpl.IToken tok, Expression messageExpr, Concret
}

protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loopIndex, bool goingUp, string /*?*/ endVarName,
List<Statement> body, ConcreteSyntaxTree wr) {
List<Statement> body, LList<Label> labels, ConcreteSyntaxTree wr) {

throw new NotImplementedException("for loops have not yet been implemented");
}
Expand Down Expand Up @@ -2376,7 +2381,7 @@ public override bool CompileTargetProgram(string dafnyProgramName, string target
Contract.Assert(assemblyLocation != null);
var codebase = System.IO.Path.GetDirectoryName(assemblyLocation);
Contract.Assert(codebase != null);
var warnings = "-Wall -Wextra -Wpedantic -Wno-unused-variable -Wno-deprecated-copy";
var warnings = "-Wall -Wextra -Wpedantic -Wno-unused-variable -Wno-deprecated-copy -Wno-unused-label";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
warnings += " -Wno-unused-but-set-variable";
} else {
Expand Down
14 changes: 10 additions & 4 deletions Source/Dafny/Compilers/Compiler-go.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,10 +1779,11 @@ protected void EmitReturnWithCoercions(List<Formal> outParams, List<Formal>/*?*/
wr.WriteLine();
}

protected override ConcreteSyntaxTree CreateLabeledCode(string label, ConcreteSyntaxTree wr) {
protected override ConcreteSyntaxTree CreateLabeledCode(string label, bool createContinueLabel, ConcreteSyntaxTree wr) {
var w = wr.Fork();
wr.WriteLine("goto L{0};", label);
wr.Fork(-1).WriteLine("L{0}:", label);
var prefix = createContinueLabel ? "C" : "L";
wr.WriteLine($"goto {prefix}{label};");
wr.Fork(-1).WriteLine($"{prefix}{label}:");
return w;
}

Expand All @@ -1794,6 +1795,10 @@ protected override void EmitBreak(string/*?*/ label, ConcreteSyntaxTree wr) {
}
}

protected override void EmitContinue(string label, ConcreteSyntaxTree wr) {
wr.WriteLine("goto C{0};", label);
}

protected override void EmitYield(ConcreteSyntaxTree wr) {
wr.WriteLine("_yielded <- struct{}{}");
wr.WriteLine("_, _ok = <- _cont");
Expand Down Expand Up @@ -1826,7 +1831,7 @@ protected override ConcreteSyntaxTree CreateWhileLoop(out ConcreteSyntaxTree gua
}

protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loopIndex, bool goingUp, string /*?*/ endVarName,
List<Statement> body, ConcreteSyntaxTree wr) {
List<Statement> body, LList<Label> labels, ConcreteSyntaxTree wr) {

wr.Write($"for {loopIndex.CompileName} := ");
var startWr = wr.Fork();
Expand Down Expand Up @@ -1861,6 +1866,7 @@ protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loop
bodyWr.WriteLine($"{loopIndex.CompileName}--");
}
}
bodyWr = EmitContinueLabel(labels, bodyWr);
TrStmtList(body, bodyWr);

return startWr;
Expand Down
12 changes: 9 additions & 3 deletions Source/Dafny/Compilers/Compiler-java.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3348,14 +3348,19 @@ protected override string GetCollectionBuilder_Build(CollectionType ct, Bpl.ITok
}
}

protected override ConcreteSyntaxTree CreateLabeledCode(string label, ConcreteSyntaxTree wr) {
return wr.NewNamedBlock($"goto_{label}:");
protected override ConcreteSyntaxTree CreateLabeledCode(string label, bool createContinueLabel, ConcreteSyntaxTree wr) {
var prefix = createContinueLabel ? "continue_" : "goto_";
return wr.NewNamedBlock($"{prefix}{label}:");
}

protected override void EmitBreak(string label, ConcreteSyntaxTree wr) {
wr.WriteLine(label == null ? "break;" : $"break goto_{label};");
}

protected override void EmitContinue(string label, ConcreteSyntaxTree wr) {
wr.WriteLine($"break continue_{label};");
}

protected override void EmitAbsurd(string message, ConcreteSyntaxTree wr) {
if (message == null) {
message = "unexpected control point";
Expand Down Expand Up @@ -3524,7 +3529,7 @@ protected override ConcreteSyntaxTree CreateForLoop(string indexVar, string boun
}

protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loopIndex, bool goingUp, string /*?*/ endVarName,
List<Statement> body, ConcreteSyntaxTree wr) {
List<Statement> body, LList<Label> labels, ConcreteSyntaxTree wr) {

var nativeType = AsNativeType(loopIndex.Type);

Expand Down Expand Up @@ -3565,6 +3570,7 @@ protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loop
bodyWr.WriteLine($"{loopIndex.CompileName}--;");
}
}
bodyWr = EmitContinueLabel(labels, bodyWr);
TrStmtList(body, bodyWr);

return startWr;
Expand Down
12 changes: 9 additions & 3 deletions Source/Dafny/Compilers/Compiler-js.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,9 @@ protected override void EmitReturn(List<Formal> outParams, ConcreteSyntaxTree wr
}
}

protected override ConcreteSyntaxTree CreateLabeledCode(string label, ConcreteSyntaxTree wr) {
return wr.NewNamedBlock("L{0}:", label);
protected override ConcreteSyntaxTree CreateLabeledCode(string label, bool createContinueLabel, ConcreteSyntaxTree wr) {
var prefix = createContinueLabel ? "C" : "L";
return wr.NewNamedBlock($"{prefix}{label}:");
}

protected override void EmitBreak(string/*?*/ label, ConcreteSyntaxTree wr) {
Expand All @@ -1115,6 +1116,10 @@ protected override void EmitBreak(string/*?*/ label, ConcreteSyntaxTree wr) {
}
}

protected override void EmitContinue(string label, ConcreteSyntaxTree wr) {
wr.WriteLine("break C{0};", label);
}

protected override void EmitYield(ConcreteSyntaxTree wr) {
wr.WriteLine("yield null;");
}
Expand All @@ -1137,7 +1142,7 @@ protected override void EmitHalt(Bpl.IToken tok, Expression/*?*/ messageExpr, Co
}

protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loopIndex, bool goingUp, string /*?*/ endVarName,
List<Statement> body, ConcreteSyntaxTree wr) {
List<Statement> body, LList<Label> labels, ConcreteSyntaxTree wr) {

var nativeType = AsNativeType(loopIndex.Type);

Expand Down Expand Up @@ -1174,6 +1179,7 @@ protected override ConcreteSyntaxTree EmitForStmt(Bpl.IToken tok, IVariable loop
bodyWr.WriteLine($"{loopIndex.CompileName}--;");
}
}
bodyWr = EmitContinueLabel(labels, bodyWr);
TrStmtList(body, bodyWr);

return startWr;
Expand Down
9 changes: 6 additions & 3 deletions Source/Dafny/Compilers/Compiler-python.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,18 @@ protected override void EmitReturn(List<Formal> outParams, ConcreteSyntaxTree wr
throw new NotImplementedException();
}

protected override ConcreteSyntaxTree CreateLabeledCode(string label, ConcreteSyntaxTree wr) {
protected override ConcreteSyntaxTree CreateLabeledCode(string label, bool createContinueLabel, ConcreteSyntaxTree wr) {
throw new NotImplementedException();
}

protected override void EmitBreak(string label, ConcreteSyntaxTree wr) {
throw new NotImplementedException();
}

protected override void EmitContinue(string label, ConcreteSyntaxTree wr) {
throw new NotImplementedException();
}

protected override void EmitYield(ConcreteSyntaxTree wr) {
throw new NotImplementedException();
}
Expand All @@ -310,8 +314,7 @@ protected override void EmitHalt(IToken tok, Expression messageExpr, ConcreteSyn
}

protected override ConcreteSyntaxTree EmitForStmt(IToken tok, IVariable loopIndex, bool goingUp, string endVarName,
List<Statement> body,
ConcreteSyntaxTree wr) {
List<Statement> body, LList<Label> labels, ConcreteSyntaxTree wr) {
throw new NotImplementedException();
}

Expand Down
Loading