Skip to content

Commit 981a218

Browse files
committed
Merge pull request #292 from dotty-staging/fix/#290-type-bind
Fix/#290 type bind
2 parents 78547d1 + a928d99 commit 981a218

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ object Trees {
594594
case class Bind[-T >: Untyped] private[ast] (name: Name, body: Tree[T])
595595
extends NameTree[T] with DefTree[T] with PatternTree[T] {
596596
type ThisTree[-T >: Untyped] = Bind[T]
597+
override def isType = name.isTypeName
598+
override def isTerm = name.isTermName
597599
override def envelope: Position = pos union initialPos
598600
}
599601

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
@@ -777,7 +777,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
777777

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

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

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)