Skip to content

Clearer error message when SAM conversion is rejected for a sealed or final type - #26639

Open
SolalPirelli wants to merge 2 commits into
scala:mainfrom
dotty-staging:solal/closure-to-sealed-trait
Open

Clearer error message when SAM conversion is rejected for a sealed or final type#26639
SolalPirelli wants to merge 2 commits into
scala:mainfrom
dotty-staging:solal/closure-to-sealed-trait

Conversation

@SolalPirelli

@SolalPirelli SolalPirelli commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #26617

Not sure this is that useful? But it's not too complex to add.

Have you relied on LLM-based tools in this contribution?

No

How was the solution tested?

New automated tests (including the issue's reproducer, if applicable)

@SolalPirelli
SolalPirelli marked this pull request as ready for review July 29, 2026 09:30
@SolalPirelli SolalPirelli changed the title Clearer error message when SAM conversion is rejected for a sealed trait Clearer error message when SAM conversion is rejected for a sealed or final type Jul 29, 2026
@SolalPirelli
SolalPirelli requested a review from odersky July 29, 2026 09:30

@odersky odersky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Otherwise LGTM

unapply(tp, false)

/** Optionally ignores `final` and `sealed`, so that error messages can point to this specific problem. */
def unapply(tp: Type, ignoreFinalOrSealed: Boolean)(using Context): Option[(MethodType, Type)] =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since this is not a real unapply method, we might want to choose anothetr name. "deconstruct" maybe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

end samParent

def samClass(tp: Type)(using Context): Symbol = tp match
private def samClass(tp: Type, ignoreFinalOrSealed: Boolean)(using Context): Symbol = tp match

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can avoid the tiresome repetition of the second parameter by moving samClass into unapply (or deconstruct).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's cleaner indeed, thanks

@odersky odersky assigned SolalPirelli and unassigned odersky Aug 12, 2026
@SolalPirelli
SolalPirelli force-pushed the solal/closure-to-sealed-trait branch from d857b41 to 2448552 Compare August 12, 2026 12:32
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.

Confusing error message when SAM conversion is rejected for a sealed trait

2 participants