Skip to content

Synthetic cast must not break tailrec - #26343

Merged
som-snytt merged 1 commit into
scala:mainfrom
som-snytt:issue/21642-tailrec-polyret
Jun 16, 2026
Merged

som-snytt merged 1 commit into
scala:mainfrom
som-snytt:issue/21642-tailrec-polyret

Conversation

@som-snytt

Copy link
Copy Markdown
Contributor

Fixes #21642

Pattern matcher inserted a cast, which is no longer tail recursive.
The fix ignores synthetic select of asInstanceOf for tail position.

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

Not at all

How was the solution tested?

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

@sjrd sjrd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code LGTM, but I have a question about the test.

Comment thread tests/pos/i21642.scala

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't this test need a -Werror flag with a using config to be useful?

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.

There's no using...?

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.

I thought I pushed it at the last moment when I created the PR! Maybe I am out of practice! I will make amends by amending.

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.

Oh, that was my other regression test where I did that! Apologies again.

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.

Oh wait, tailrec is a hard error already.

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.

Yeah I think this test is fine as-is without having to be moved to warn/, I also second-guessed myself on tailrec being an error

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.

  |                 Cannot rewrite recursive call: it is not in tail position
1 error found

case tree @ Select(qual, name) =>
cpy.Select(tree)(noTailTransform(qual), name)
val qual1 =
if tree.symbol == defn.Any_asInstanceOf && tree.span.isSynthetic then

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.

It would be nice to explain why so that one doesn't have to git blame to understand.

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.

I thought of you when I almost commented the line.

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.

I recommend git blame in case there are puns in the commit text.

@som-snytt
som-snytt force-pushed the issue/21642-tailrec-polyret branch from 587635c to 5be0d6e Compare June 16, 2026 13:21
@som-snytt
som-snytt enabled auto-merge (squash) June 16, 2026 13:25
@som-snytt
som-snytt merged commit 9a878fd into scala:main Jun 16, 2026
44 of 45 checks passed
@som-snytt
som-snytt deleted the issue/21642-tailrec-polyret branch June 16, 2026 20:06
@WojciechMazur WojciechMazur added this to the 3.10.0 milestone Aug 26, 2026
tgodzik pushed a commit to scala/scala3-lts that referenced this pull request Oct 9, 2026
Fixes scala#21642 

Pattern matcher inserted a cast, which is no longer tail recursive.
The fix ignores synthetic select of asInstanceOf for tail position.

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

Not at all

## How was the solution tested?

New automated tests (including the issue's reproducer, if applicable)
[Cherry-picked 9a878fd]
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.

@tailrec false positive

4 participants