Skip to content

fix: #24765 SAM conversion for extension method from self-based types#25637

Open
halotukozak wants to merge 1 commit intoscala:mainfrom
halotukozak:fix/i24765-sam-extension-method
Open

fix: #24765 SAM conversion for extension method from self-based types#25637
halotukozak wants to merge 1 commit intoscala:mainfrom
halotukozak:fix/i24765-sam-extension-method

Conversation

@halotukozak
Copy link
Copy Markdown

Fixes #24765

Enter type members into the anonymous class scope before creating term forwarders, so allOverriddenSymbols can resolve type aliases (like Self = Int) when matching method signatures.

How much have you relied on LLM-based tools in this contribution?

Claude helped me find the root cause.

How was the solution tested?

i24765.scala added

Additional notes

Copilot AI review requested due to automatic review settings March 27, 2026 11:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes SAM conversion for extension methods in self-based type classes by ensuring type members are entered into the anonymous class scope early enough for override-resolution to see type aliases (e.g., type Self = Int), and adds a regression test for #24765.

Changes:

  • Adjust anonymous class generation to enter synthetic type members before creating term forwarders (so allOverriddenSymbols can match signatures through aliases).
  • Add tests/pos/i24765.scala regression test covering given Int is B = x => x where B uses a Self type member.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/pos/i24765.scala Adds a positive regression test for SAM conversion with Self-based extension methods.
compiler/src/dotty/tools/dotc/ast/tpd.scala Reorders construction to enter type members before generating forwarder methods.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -398,8 +398,8 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
if !overridden.is(Deferred) then fwdMeth.setFlag(Override)
DefDef(fwdMeth, ref(fn).appliedToArgss(_))
}
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

This code relies on evaluation order: typeMembers are entered via .entered before calling fwdMeth.allOverriddenSymbols in forwarder, otherwise overridden-signature matching can miss type aliases (as in #24765). Please add a short comment (or refactor to make the ordering/side effect explicit) so a future cleanup doesn’t accidentally revert the fix by inlining/reordering the maps.

Suggested change
}
}
// Important: `typeMembers` must be entered before calling `allOverriddenSymbols` in `forwarder`,
// otherwise overridden-signature matching can miss type aliases (see scala/bug#24765).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I am not sure of the convention in this repo. I will commit this suggestion if the human reviewer asks for it

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.

SAM conversion fails with "cannot override an extension method" for Self-based type classes

2 participants