Skip to content

Commit defe1c0

Browse files
committed
Fix typechecking rules for Binds of type trees.
1 parent d955733 commit defe1c0

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ trait TypeAssigner {
357357
def assignType(tree: untpd.TypeBoundsTree, lo: Tree, hi: Tree)(implicit ctx: Context) =
358358
tree.withType(TypeBounds(lo.tpe, hi.tpe))
359359

360-
def assignType(tree: untpd.Bind, sym: TermSymbol)(implicit ctx: Context) =
361-
tree.withType(TermRef(NoPrefix, sym))
360+
def assignType(tree: untpd.Bind, sym: Symbol)(implicit ctx: Context) =
361+
tree.withType(NamedType.withFixedSym(NoPrefix, sym))
362362

363363
def assignType(tree: untpd.Alternative, trees: List[Tree])(implicit ctx: Context) =
364364
tree.withType(ctx.typeComparer.lub(trees.tpes))

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
776776

777777
def typedAppliedTypeTree(tree: untpd.AppliedTypeTree)(implicit ctx: Context): AppliedTypeTree = track("typedAppliedTypeTree") {
778778
val tpt1 = typed(tree.tpt)
779-
val args1 = tree.args mapconserve (typed(_))
779+
val argPts =
780+
if (ctx.mode is Mode.Pattern) tpt1.tpe.typeParams.map(_.info)
781+
else tree.args.map(_ => WildcardType)
782+
val args1 = tree.args.zipWithConserve(argPts)(typed(_, _)).asInstanceOf[List[Tree]]
780783
// check that arguments conform to bounds is done in phase FirstTransform
781784
assignType(cpy.AppliedTypeTree(tree)(tpt1, args1), tpt1, args1)
782785
}
@@ -798,7 +801,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
798801
def typedBind(tree: untpd.Bind, pt: Type)(implicit ctx: Context): Bind = track("typedBind") {
799802
val body1 = typed(tree.body, pt)
800803
typr.println(i"typed bind $tree pt = $pt bodytpe = ${body1.tpe}")
801-
val sym = ctx.newSymbol(ctx.owner, tree.name.asTermName, EmptyFlags, body1.tpe, coord = tree.pos)
804+
val sym = ctx.newSymbol(ctx.owner, tree.name, EmptyFlags, body1.tpe, coord = tree.pos)
802805
assignType(cpy.Bind(tree)(tree.name, body1), sym)
803806
}
804807

@@ -1339,7 +1342,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13391342
}
13401343
case _ =>
13411344
if (ctx.mode is Mode.Type)
1342-
if (tree.tpe <:< pt) tree
1345+
if ((ctx.mode is Mode.Pattern) || tree.tpe <:< pt) tree
13431346
else err.typeMismatch(tree, pt)
13441347
else adaptNoArgs(wtp)
13451348
}

tests/pos/i0290-type-bind.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object foo{
2+
val x = List(1,2,3)
3+
x match {
4+
case t: List[tt] => t.head.asInstanceOf[tt]
5+
}
6+
}
7+
8+
object bar {
9+
10+
class C[T <: Seq[_]]
11+
12+
val x: AnyRef = new C
13+
14+
x match {
15+
case x: C[u] =>
16+
def x: u = x
17+
val s: Seq[_] = x
18+
}
19+
}

0 commit comments

Comments
 (0)