Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ trait BCodeHelpers extends BCodeIdiomatic {
val edesc = innerClasesStore.typeDescriptor(t.tpe) // the class descriptor of the enumeration class.
val evalue = t.symbol.javaSimpleName // value the actual enumeration value.
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) {
t.symbol.info.finalResultType match
case tr: TermRef => tr.termSymbol.owner.linkedClass.isAllOf(JavaEnum)
case _ => false
} =>
val enumRef = atPhase(erasurePhase)(t.symbol.info.finalResultType.asInstanceOf[TermRef])
val edesc = innerClasesStore.typeDescriptor(enumRef)
val evalue = enumRef.termSymbol.javaSimpleName
av.visitEnum(name, edesc, evalue)
case t: SeqLiteral =>
val arrAnnotV: AnnotationVisitor = av.visitArray(name)
for (arg <- t.elems) { emitArgument(arrAnnotV, null, arg, bcodeStore)(innerClasesStore) }
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2221,10 +2221,15 @@ class Namer { typer: Typer =>
// `default-getter-variance.scala`.
AnnotatedType(defaultTp, Annotation(defn.UncheckedVarianceAnnot, sym.span))
else
inline def isJavaEnumValue = tp match
case tp: TermRef => tp.termSymbol.isAllOf(JavaDefined | Enum)
case _ => false
// don't strip @uncheckedVariance annot for default getters
TypeOps.simplify(tp.widenTermRefExpr,
if defaultTp.exists then TypeOps.SimplifyKeepUnchecked() else null)
match
// For final members pointing to Java enum values, preserve the singleton type. See i24750.
case _ if sym.is(Final) && isJavaEnumValue => tp
case ctp: ConstantType if sym.isInlineVal => ctp
case tp if isTracked => tp
case tp => TypeComparer.widenInferred(tp, pt, Widen.Unions)
Expand Down
14 changes: 14 additions & 0 deletions tests/pos/i24750.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//> using options -Werror
import scala.concurrent.duration.*

object i24750:

def f(duration: FiniteDuration): Unit =
duration.unit match
case NANOSECONDS =>
case MICROSECONDS =>
case MILLISECONDS =>
case SECONDS =>
case MINUTES =>
case HOURS =>
case DAYS =>
2 changes: 1 addition & 1 deletion tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -5650,7 +5650,7 @@ types/Test.Literal.clazzOf. => final val method clazzOf Option[Int]
types/Test.Literal.double. => final val method double 2.0
types/Test.Literal.float. => final val method float 1.0f
types/Test.Literal.int. => final val method int 1
types/Test.Literal.javaEnum. => final val method javaEnum LinkOption
types/Test.Literal.javaEnum. => final val method javaEnum NOFOLLOW_LINKS.type
types/Test.Literal.long. => final val method long 1L
types/Test.Literal.nil. => final val method nil Null
types/Test.Literal.string. => final val method string "a"
Expand Down
8 changes: 8 additions & 0 deletions tests/warn/i24750.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i24750.scala:6:13 ---------------------------------------------
6 | duration.unit match // warn
| ^^^^^^^^^^^^^
| match may not be exhaustive.
|
| It would fail on pattern case: DAYS
|
| longer explanation available when compiling with `-explain`
12 changes: 12 additions & 0 deletions tests/warn/i24750.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.concurrent.duration.*

object i24750:

def f(duration: FiniteDuration): Unit =
duration.unit match // warn
case NANOSECONDS =>
case MICROSECONDS =>
case MILLISECONDS =>
case SECONDS =>
case MINUTES =>
case HOURS =>
Loading