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 C# codegen for tail-recursive member functions #2205

Merged
merged 7 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, since the fields stores an interface, you need to provide the interface for the base type. Nice and easy fix.

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