Skip to content

Commit 5df985c

Browse files
Merge pull request #1819 from dotty-staging/fix-#1803
Fix #1803: Infer type parameters of anonymous class parents from expected type
2 parents 5920b74 + 0a07f7f commit 5df985c

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ object Inferencing {
119119
}
120120
}
121121

122+
/** If `tree` has a PolyType, infer its type parameters by comparing with expected type `pt` */
123+
def inferTypeParams(tree: Tree, pt: Type)(implicit ctx: Context): Tree = tree.tpe match {
124+
case poly: PolyType =>
125+
val (poly1, tvars) = constrained(poly, tree)
126+
val tree1 = tree.withType(poly1).appliedToTypeTrees(tvars)
127+
tree1.tpe <:< pt
128+
fullyDefinedType(tree1.tpe, "template parent", tree.pos)
129+
tree1
130+
case _ =>
131+
tree
132+
}
133+
122134
/** The list of uninstantiated type variables bound by some prefix of type `T` which
123135
* occur in at least one formal parameter type of a prefix application.
124136
* Considered prefixes are:

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -852,23 +852,23 @@ class Namer { typer: Typer =>
852852
}
853853
}
854854

855-
/** Typecheck tree during completion, and remember result in typedtree map */
856-
private def typedAheadImpl(tree: Tree, pt: Type)(implicit ctx: Context): tpd.Tree = {
855+
/** Typecheck `tree` during completion using `typed`, and remember result in TypedAhead map */
856+
def typedAheadImpl(tree: Tree, typed: untpd.Tree => tpd.Tree)(implicit ctx: Context): tpd.Tree = {
857857
val xtree = expanded(tree)
858858
xtree.getAttachment(TypedAhead) match {
859859
case Some(ttree) => ttree
860860
case none =>
861-
val ttree = typer.typed(tree, pt)
861+
val ttree = typed(tree)
862862
xtree.putAttachment(TypedAhead, ttree)
863863
ttree
864864
}
865865
}
866866

867867
def typedAheadType(tree: Tree, pt: Type = WildcardType)(implicit ctx: Context): tpd.Tree =
868-
typedAheadImpl(tree, pt)(ctx retractMode Mode.PatternOrType addMode Mode.Type)
868+
typedAheadImpl(tree, typer.typed(_, pt)(ctx retractMode Mode.PatternOrType addMode Mode.Type))
869869

870870
def typedAheadExpr(tree: Tree, pt: Type = WildcardType)(implicit ctx: Context): tpd.Tree =
871-
typedAheadImpl(tree, pt)(ctx retractMode Mode.PatternOrType)
871+
typedAheadImpl(tree, typer.typed(_, pt)(ctx retractMode Mode.PatternOrType))
872872

873873
def typedAheadAnnotation(tree: Tree)(implicit ctx: Context): Symbol = tree match {
874874
case Apply(fn, _) => typedAheadAnnotation(fn)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
454454
tree.tpt match {
455455
case templ: untpd.Template =>
456456
import untpd._
457+
templ.parents foreach {
458+
case parent: RefTree =>
459+
typedAheadImpl(parent, tree => inferTypeParams(typedType(tree), pt))
460+
case _ =>
461+
}
457462
val x = tpnme.ANON_CLASS
458463
val clsDef = TypeDef(x, templ).withFlags(Final)
459464
typed(cpy.Block(tree)(clsDef :: Nil, New(Ident(x), Nil)), pt)

tests/pos/i1803.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class C[T]
2+
3+
object Test {
4+
def f(x: C[Int]) = ???
5+
6+
f(new C {})
7+
}

0 commit comments

Comments
 (0)