Skip to content

Commit

Permalink
Fix backend assertion when using an exported enum
Browse files Browse the repository at this point in the history
An exported enum is a <static> final method of the enum object.  When
selecting an inner enum case from it, that method will be the prefix.
The method doesn't have a type symbol, we have to widen to method type
and reach for its result type before we can get a handle on the type
symbol to key off of.

I wonder whether this widening and going to the result type is something
that should happen during Erasure's denotation transformation though...

Co-authored-by: Seth Tisue <[email protected]>
  • Loading branch information
2 people authored and odersky committed Feb 12, 2022
1 parent ddac928 commit b0556ee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
def genLoadQualUnlessElidable(): Unit = { if (!qualSafeToElide) { genLoadQualifier(tree) } }

// receiverClass is used in the bytecode to access the field. using sym.owner may lead to IllegalAccessError
def receiverClass = qualifier.tpe.typeSymbol
def receiverClass = qualifier.tpe.widenTermRefExpr.finalResultType.typeSymbol
if (sym.is(Module)) {
genLoadQualUnlessElidable()
genLoadModule(tree)
Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i13490.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
object MyApi {
enum MyEnum(a: Int) {
case A extends MyEnum(1)
}
case class Foo(a: MyEnum)
}

object Test {
export MyApi.*
import MyEnum.*
Foo(MyEnum.A) match {
case Foo(a) =>
a match {
case A =>
}
}
}
13 changes: 13 additions & 0 deletions tests/run/i13490.min.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
object MyTypes:
enum MyEnum:
case Foo
case Bar

object MyApi:
export MyTypes.*

object MyUse:
import MyApi.MyEnum.Foo
def foo = Foo

@main def Test = assert(MyUse.foo.toString == "Foo")

0 comments on commit b0556ee

Please sign in to comment.