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

Conversation

cpitclaudel
Copy link
Member

Closes #2173.

MikaelMayer
MikaelMayer previously approved these changes Jun 6, 2022
Copy link
Member

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

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

Bien trouvé !

@@ -1331,7 +1331,9 @@ protected class ClassWriter : IClassWriter {
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.

@cpitclaudel cpitclaudel self-assigned this Jun 7, 2022
@cpitclaudel cpitclaudel enabled auto-merge (squash) June 7, 2022 15:37
fabiomadge
fabiomadge previously approved these changes Jun 7, 2022
Comment on lines +1336 to +1337
var receiverType = member.EnclosingClass is DatatypeDecl dt ? DtTypeName(dt) : "var";
wr.WriteLine($"{receiverType} _this = this;");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
var receiverType = member.EnclosingClass is DatatypeDecl dt ? DtTypeName(dt) : "var";
wr.WriteLine($"{receiverType} _this = this;");
wr.Write(member.EnclosingClass is DatatypeDecl dt ? DtTypeName(dt) : "var");
wr.WriteLine(" _this = this;");

@cpitclaudel cpitclaudel merged commit 933b1f2 into master Jun 8, 2022
@cpitclaudel cpitclaudel deleted the cpitclaudel_2173 branch June 8, 2022 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid C# codegen for tail-recursive member functions that recurse on this
3 participants