Skip to content

Preserve singleton types for final val aliases to Java enum constants#24980

Merged
zielinsky merged 3 commits intoscala:mainfrom
zielinsky:i24750v3
Jan 19, 2026
Merged

Preserve singleton types for final val aliases to Java enum constants#24980
zielinsky merged 3 commits intoscala:mainfrom
zielinsky:i24750v3

Conversation

@zielinsky
Copy link
Copy Markdown
Member

@zielinsky zielinsky commented Jan 14, 2026

Fixes #24750
Fixes #21881

@som-snytt
Copy link
Copy Markdown
Contributor

som-snytt commented Jan 14, 2026

I mentioned on the ticket that they can't be used in annotations. I tried one approach that was too clever, rewriting them as inline transparent def. Just in case there's an easy fix to the type, since enums are not constant (any more).

Edit: I guess the singleton type will be visible in the back end, at least, which is currently widened!

Comment thread compiler/src/dotty/tools/dotc/typer/Namer.scala Outdated
@zielinsky zielinsky marked this pull request as ready for review January 15, 2026 09:39
@zielinsky
Copy link
Copy Markdown
Member Author

zielinsky commented Jan 15, 2026

I mentioned on the ticket that they can't be used in annotations. I tried one approach that was too clever, rewriting them as inline transparent def. Just in case there's an easy fix to the type, since enums are not constant (any more).

Edit: I guess the singleton type will be visible in the back end, at least, which is currently widened!

@som-snytt now they can! Look at 7fee34d !:> Used exactly your insight - the singleton type is visible in backend. Should I mark this as fixing #21881?

@zielinsky zielinsky requested a review from som-snytt January 15, 2026 13:25
@som-snytt
Copy link
Copy Markdown
Contributor

som-snytt commented Jan 15, 2026

@zielinsky that's amazing, thanks. I took a look yesterday and didn't formulate it correctly.

Yes, I think this is sufficient for the use case in #21881.

Edit: I just deleted my local branch issue/21881-post-zielinsky.

@zielinsky zielinsky requested review from odersky and removed request for som-snytt January 15, 2026 20:45
Comment thread compiler/src/dotty/tools/dotc/typer/Namer.scala
@som-snytt
Copy link
Copy Markdown
Contributor

som-snytt commented Jan 16, 2026

FSR I get the complement in the warn test.

It would fail on pattern case: NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS

I was about to inspect the back end fix.

Clean build (?) has similar warning in Duration.scala.

Edit: the difference is bootstrapped.

sbt:scala3-bootstrapped> scalac tests/pos/i24750.scala
[info] running (fork) dotty.tools.dotc.Main -d /home/amarki/projects/scala3/out/default-last-scalac-out.jar -classpath /home/amarki/projects/scala3/library/target/scala-library-bootstrapped/scala-library-3.8.2-RC1-bin-SNAPSHOT.jar tests/pos/i24750.scala
[success] Total time: 7 s, completed Jan 16, 2026, 5:34:38 PM
sbt:scala3-bootstrapped> project scala3-nonbootstrapped
[info] set current project to scala3-nonbootstrapped (in build file:/home/amarki/projects/scala3/)
sbt:scala3-nonbootstrapped> scalac tests/pos/i24750.scala
[info] running (fork) dotty.tools.dotc.Main -d /home/amarki/projects/scala3/out/default-last-scalac-out.jar -classpath /home/amarki/projects/scala3/library/target/scala-library-nonbootstrapped/scala-library-3.8.2-RC1-bin-SNAPSHOT-nonbootstrapped.jar tests/pos/i24750.scala
-- [E029] Pattern Match Exhaustivity Warning: tests/pos/i24750.scala:7:13 ------
7 |    duration.unit match
  |    ^^^^^^^^^^^^^
  |match may not be exhaustive.
  |
  |It would fail on pattern case: NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS
  |(More unmatched cases are elided)
  |
  | longer explanation available when compiling with `-explain`
1 warning found
[success] Total time: 5 s, completed Jan 16, 2026, 5:35:01 PM

av.visitEnum(name, edesc, evalue)
// Handle final val aliases to Java enum values.
// Check if the symbol's pre-erasure type was a singleton of a Java enum value.
case t: tpd.RefTree if atPhase(erasurePhase) {
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.

I wonder if there is a reason to prefer atPhaseBeforeTransforms or conversely to avoid it.

I see to my chagrin that braceless colon syntax does not work, atPhaseBeforeTransforms:.

Copy link
Copy Markdown
Contributor

@som-snytt som-snytt left a comment

Choose a reason for hiding this comment

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

"Better you than me!"

@zielinsky zielinsky enabled auto-merge (squash) January 19, 2026 13:16
@zielinsky zielinsky merged commit 2f8bbdc into scala:main Jan 19, 2026
56 checks passed
zielinsky added a commit that referenced this pull request Jan 26, 2026
…pped-only. (#25102)

Re-enable non-bootstrapped CI tests (revert
69a9094)
move i24750 tests to bootstrapped-only
(#24980).
@WojciechMazur WojciechMazur added the backport:nominated If we agree to backport this PR, replace this tag with "backport:accepted", otherwise delete it. label Feb 3, 2026
@WojciechMazur WojciechMazur added this to the 3.8.2 milestone Feb 3, 2026
WojciechMazur added a commit that referenced this pull request Feb 4, 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 Feb 4, 2026
WojciechMazur added a commit that referenced this pull request Feb 5, 2026
… constants" to 3.8.2 (#25190)

Backports #24980 to the 3.8.2-RC2.

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 Feb 5, 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.

Match exhaustivity warning when matching scala.concurrent.duration.MILLISECONDS and more Final val of Java enum is not a constant value

5 participants