Skip to content

Commit aa2da96

Browse files
committed
Refined fix
Widening methods need to be given derived versions of the type parameters of the original enum class instead of the case class. This required some new infrastructure to define a deribed type tree from a symbol instead of an original TypeDef.
1 parent 07e727b commit aa2da96

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ object desugar {
9292
cpy.TypeDef(tdef)(
9393
rhs = new DerivedFromParamTree() withPos tdef.rhs.pos watching tdef)
9494

95+
/** A derived type definition watching `sym` */
96+
def derivedTypeParam(sym: TypeSymbol)(implicit ctx: Context): TypeDef =
97+
TypeDef(sym.name, new DerivedFromParamTree().watching(sym)).withFlags(TypeParam)
98+
9599
/** A value definition copied from `vdef` with a tpt typetree derived from it */
96100
def derivedTermParam(vdef: ValDef) =
97101
cpy.ValDef(vdef)(
@@ -462,8 +466,10 @@ object desugar {
462466
(TypeTree(), Nil)
463467
else if (parents.isEmpty || enumClass.typeParams.isEmpty)
464468
(enumClassTypeRef, Nil)
465-
else
466-
enumApplyResult(cdef, parents, derivedTparams, appliedRef(enumClassRef, derivedTparams))
469+
else {
470+
val tparams = enumClass.typeParams.map(derivedTypeParam)
471+
enumApplyResult(cdef, parents, tparams, appliedRef(enumClassRef, tparams))
472+
}
467473

468474
val parent =
469475
if (constrTparams.nonEmpty ||

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
227227
this
228228
}
229229

230+
/** Install the derived type tree as a dependency on `sym` */
231+
def watching(sym: Symbol): this.type = {
232+
pushAttachment(OriginalSymbol, sym)
233+
this
234+
}
235+
230236
/** A hook to ensure that all necessary symbols are completed so that
231237
* OriginalSymbol attachments are propagated to this tree
232238
*/
@@ -240,7 +246,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
240246
* from the symbol in this type. These type trees have marker trees
241247
* TypeRefOfSym or InfoOfSym as their originals.
242248
*/
243-
val References = new Property.Key[List[Tree]]
249+
val References = new Property.Key[List[DerivedTypeTree]]
244250

245251
/** Property key for TypeTrees marked with TypeRefOfSym or InfoOfSym
246252
* which contains the symbol of the original tree from which this

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,7 @@ class Namer { typer: Typer =>
228228

229229
/** Record `sym` as the symbol defined by `tree` */
230230
def recordSym(sym: Symbol, tree: Tree)(implicit ctx: Context): Symbol = {
231-
val refs = tree.attachmentOrElse(References, Nil)
232-
if (refs.nonEmpty) {
233-
tree.removeAttachment(References)
234-
refs foreach (_.pushAttachment(OriginalSymbol, sym))
235-
}
231+
for (refs <- tree.removeAttachment(References); ref <- refs) ref.watching(sym)
236232
tree.pushAttachment(SymOfTree, sym)
237233
sym
238234
}

tests/pos/i2663.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ enum Foo[T](x: T) {
33
case Bar[T](y: T) extends Foo(y)
44
case Bas[T](y: Int) extends Foo(y)
55
case Bam[T](y: String) extends Foo(y) with Tr
6+
case Baz[S, T](y: String) extends Foo(y) with Tr
67
}
78
object Test {
89
import Foo._
910
val bar: Foo[Boolean] = Bar(true)
1011
val bas: Foo[Int] = Bas(1)
1112
val bam: Foo[String] & Tr = Bam("")
13+
val baz: Foo[String] & Tr = Baz("")
1214
}
1315

1416

0 commit comments

Comments
 (0)