Skip to content

Commit

Permalink
Fix C# codegen for tail-recursive member functions
Browse files Browse the repository at this point in the history
Closes #2173.
  • Loading branch information
cpitclaudel committed Jun 4, 2022
1 parent 3145da6 commit 461791a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- fix: No more display of previous obsolete verification diagnostics if newer are available. (https://github.com/dafny-lang/dafny/pull/2142)
- feat: Live verification diagnostics for the language server (https://github.com/dafny-lang/dafny/pull/1942)
- fix: Prevent the language server from crashing and not responding on resolution or ghost diagnostics errors (https://github.com/dafny-lang/dafny/pull/2080)
- fix: Correctly specify the type of the receiver parameter when translating tail-recursive member functions to C# (https://github.com/dafny-lang/dafny/pull/2205)

# 3.6.0

Expand Down
4 changes: 3 additions & 1 deletion Source/Dafny/Compilers/Compiler-Csharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,9 @@ public bool ProcessDllImport(MemberDecl decl, ConcreteSyntaxTree wr) {
protected override ConcreteSyntaxTree EmitTailCallStructure(MemberDecl member, ConcreteSyntaxTree wr) {
Contract.Assume((member is Method m0 && m0.IsTailRecursive) || (member is Function f0 && f0.IsTailRecursive)); // precondition
if (!member.IsStatic && !NeedsCustomReceiver(member)) {
wr.WriteLine("var _this = this;");
var receiverType = member.EnclosingClass is DatatypeDecl dt ?
"_I" + dt.CompileName + TypeParameters(SelectNonGhost(dt, dt.TypeArgs)) : "var";
wr.WriteLine($"{receiverType} _this = this;");
}
wr.Fork(-1).WriteLine("TAIL_CALL_START: ;");
return wr;
Expand Down
12 changes: 12 additions & 0 deletions Test/git-issues/git-issue-2173.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %dafny /compile:4 /compileTarget:cs "%s" >> "%t"

datatype T = Leaf(x: int) | T(t: T) {
function method {:tailrecursion} TR() : int {
if Leaf? then 0
else t.TR()
}
}

method Main() {
print Leaf(0).TR();
}
5 changes: 5 additions & 0 deletions Test/git-issues/git-issue-2173.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

Dafny program verifier finished with 1 verified, 0 errors
Running...

0

0 comments on commit 461791a

Please sign in to comment.