Skip to content

Commit e93f8ab

Browse files
committed
Fix #6286: Fix ElimOpaque transformation
SymTransformers used to transform only SymDenotations. But that does not work for ElimOpaque, where we also have to transform SingleDenotations that refer to opaque helpers.
1 parent 9421ff0 commit e93f8ab

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
407407
ref(defn.DottyArraysModule).select(defn.newArrayMethod).withSpan(span)
408408

409409
if (!ctx.erasedTypes) {
410-
assert(!TypeErasure.isGeneric(elemTpe)) //needs to be done during typer. See Applications.convertNewGenericArray
410+
assert(!TypeErasure.isGeneric(elemTpe), elemTpe) //needs to be done during typer. See Applications.convertNewGenericArray
411411
newArr.appliedToTypeTrees(TypeTree(returnTpe) :: Nil).appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withSpan(span)
412412
} else // after erasure
413413
newArr.appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withSpan(span)

compiler/src/dotty/tools/dotc/core/DenotTransformers.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ object DenotTransformers {
6060
/** A transformer that only transforms SymDenotations */
6161
trait SymTransformer extends DenotTransformer {
6262

63+
/** Tramsform the info of a denotation that is not a Symdenotation */
64+
def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context): Type = tp
65+
6366
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation
6467

6568
def transform(ref: SingleDenotation)(implicit ctx: Context): SingleDenotation = ref match {
6669
case ref: SymDenotation => transformSym(ref)
67-
case _ => ref
70+
case _ => ref.derivedSingleDenotation(ref.symbol, transformInfo(ref.info, ref.symbol))
6871
}
6972
}
7073

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

+3-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import reporting.diagnostic.Message
1919
import reporting.diagnostic.messages.BadSymbolicReference
2020
import reporting.trace
2121
import collection.mutable
22+
import transform.TypeUtils._
2223

2324
import scala.annotation.internal.sharable
2425

@@ -1082,16 +1083,10 @@ object SymDenotations {
10821083
* containing object.
10831084
*/
10841085
def opaqueAlias(implicit ctx: Context): Type = {
1085-
if (isOpaqueHelper) {
1086+
if (isOpaqueHelper)
10861087
owner.asClass.classInfo.selfType match {
1087-
case RefinedType(_, _, TypeBounds(lo, _)) =>
1088-
def extractAlias(tp: Type): Type = tp match {
1089-
case OrType(alias, _) => alias
1090-
case tp: HKTypeLambda => tp.derivedLambdaType(resType = extractAlias(tp.resType))
1091-
}
1092-
extractAlias(lo)
1088+
case RefinedType(_, _, bounds) => bounds.extractOpaqueAlias
10931089
}
1094-
}
10951090
else NoType
10961091
}
10971092

compiler/src/dotty/tools/dotc/transform/ElimOpaque.scala

+4-11
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,15 @@ package dotty.tools.dotc
22
package transform
33

44
import core._
5-
import Names._
65
import dotty.tools.dotc.transform.MegaPhase._
7-
import ast.Trees._
8-
import ast.untpd
96
import Flags._
107
import Types._
11-
import Constants.Constant
128
import Contexts.Context
139
import Symbols._
1410
import Decorators._
15-
import Annotations._
16-
import Annotations.ConcreteAnnotation
17-
import Denotations.SingleDenotation
1811
import SymDenotations.SymDenotation
19-
import scala.collection.mutable
2012
import DenotTransformers._
21-
import NameOps._
22-
import NameKinds.OuterSelectName
23-
import StdNames._
13+
import TypeUtils._
2414

2515
object ElimOpaque {
2616
val name: String = "elimOpaque"
@@ -37,6 +27,9 @@ class ElimOpaque extends MiniPhase with SymTransformer {
3727
// base types of opaque aliases change
3828
override def changesBaseTypes = true
3929

30+
override def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context): Type =
31+
if (sym.isOpaqueHelper) TypeAlias(tp.extractOpaqueAlias) else tp
32+
4033
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation =
4134
if (sym.isOpaqueHelper) {
4235
sym.copySymDenotation(

compiler/src/dotty/tools/dotc/transform/TypeUtils.scala

+12
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,17 @@ object TypeUtils {
5151
/** The `*:` equivalent of an instance of a Tuple class */
5252
def toNestedPairs(implicit ctx: Context): Type =
5353
TypeOps.nestedPairs(tupleElementTypes)
54+
55+
/** Extract opaque alias from TypeBounds type that combines it with the reference
56+
* to the opaque type itself
57+
*/
58+
def extractOpaqueAlias(implicit ctx: Context): Type = self match {
59+
case TypeBounds(lo, _) =>
60+
def extractAlias(tp: Type): Type = tp match {
61+
case OrType(alias, _) => alias
62+
case self: HKTypeLambda => self.derivedLambdaType(resType = extractAlias(self.resType))
63+
}
64+
extractAlias(lo)
65+
}
5466
}
5567
}

tests/pos/i6286.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object A {
2+
opaque type T33 = Int
3+
object T33 {
4+
val a = new Array[T33](3)
5+
}
6+
}

0 commit comments

Comments
 (0)