Skip to content

Fix issue with reading legal java cyclic signatures - #26775

Merged
jchyb merged 1 commit into
scala:mainfrom
dotty-staging:fix-26646-cycle-methodhandle
Aug 12, 2026
Merged

jchyb merged 1 commit into
scala:mainfrom
dotty-staging:fix-26646-cycle-methodhandle

Conversation

@jchyb

@jchyb jchyb commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

This includes things like:

  • class CyclicSignature<S extends CyclicSignature<S>.Nested>
  • class CyclicSignature<S extends CyclicSignature<S>.Nested.Deeper> Type parameters with self referential prefixes in bounds like these are impossible to define in scala, but can appear in java, with a reference to java.lang.invoke.ClassSpecializer in stdlib's MethodHandle specifically causing issues.

This is fixed both in JavaParser (the fix is in Checking.scala file) and ClassfileParser. The behavior for analogous scala code is unchanged (it will still error out). Interestingly, this wasn't an issue in scala 2, however both parsers are quite different there, so moving solutions from scala 2 wasn't possible.

Both java and scala 2 is able to read even more java signatures, like class Foo<T extends List<Foo<T>.Inner>>, as both seem to omit having any cyclicity checks this early.
However, as far as I'm aware, actually defining an instance of such type seems impossible (even in java), so perhaps failing early is fine, and we hopefully shouldn't find such java code in the wild, since this would be more difficult to support.

Fixes #26646

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

Yes, and I checked the output by reading and comprehending the code (comments are mine)

How was the solution tested?

New automated tests (including the issue's reproducer, if applicable)
Unfortunately, the reproducer is different, since the original requires a newer idk that we don't use for compilation tests yet. Hovewer, the underlying issue is still tested, and I manually checked that the original reproducer also successfully compiles now.

@SolalPirelli SolalPirelli 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.

Looks good, one small nit

else if tpe.typeSymbol.isContainedIn(classRoot.symbol) then
// classRoot is completed - using .info is safe
// e.g. case: `class CyclicSignature<S extends CyclicSignature<S>.Nested.Deeper>`
val member = tpe.typeSymbol.asClass.info.decls.lookup(name)

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.

Suggested change
val member = tpe.typeSymbol.asClass.info.decls.lookup(name)
val member = tpe.typeSymbol.info.decls.lookup(name)

since info is on all denotations

This includes things like:
* `class CyclicSignature<S extends CyclicSignature<S>.Nested>`
* `class CyclicSignature<S extends CyclicSignature<S>.Nested.Deeper>`
Type parameters with self referential prefixes in bounds like these are
impossible to define in scala, but can appear in java, with a
reference to `java.lang.invoke.ClassSpecializer` in stdlib's
MethodHandle specifically causing issues.

This is fixed both in JavaParser and ClassfileParser. The behavior for
analogous scala code is unchanged (it will still error out). Interestingly,
this wasn't an issue in scala 2, however both parsers are quite different
there, so moving solutions from scala 2 wasn't possible.

Both java and scala 2 is able to read even more java signatures, like
`class Foo<T extends List<Foo<T>.Inner>>`, as both seem to omit having
any cyclicity checks this early.
However, as far as I'm aware, actually defining an
instance of such type seems impossible (even in java), so perhaps
failing early is fine, and we hopefully shouldn't find such java code
in the wild, since this would be more difficult to support.
@jchyb
jchyb force-pushed the fix-26646-cycle-methodhandle branch from 8239e5d to 4b2d0e4 Compare August 11, 2026 14:00
@jchyb
jchyb merged commit bc71028 into scala:main Aug 12, 2026
60 checks passed
@jchyb
jchyb deleted the fix-26646-cycle-methodhandle branch August 12, 2026 08:49
@WojciechMazur WojciechMazur added the backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. label Aug 12, 2026
@WojciechMazur WojciechMazur added this to the 3.9.0 milestone Aug 17, 2026
@WojciechMazur WojciechMazur added backport:accepted This PR needs to be backported, once it's been backported replace this tag by "backport:done" and removed backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. labels Aug 17, 2026
WojciechMazur added a commit that referenced this pull request Aug 19, 2026
….0 (#26831)

Backports #26775 to the 3.9.0-RC6.

PR submitted by the release tooling.
[skip ci]
@WojciechMazur WojciechMazur added backport:done This PR was successfully backported. and removed backport:accepted This PR needs to be backported, once it's been backported replace this tag by "backport:done" labels Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:done This PR was successfully backported.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash: val _ = <MethodHandle> causes cyclic reference

3 participants