@@ -4186,7 +4186,7 @@ object Types {
4186
4186
// ----- TypeOf -------------------------------------------------------------------------
4187
4187
4188
4188
/** Type that represents the precise type of a given term.
4189
- * Precision is only kept for Apply, TypeApply, If and Match trees.
4189
+ * Precision is only kept for Apply, TypeApply and If trees.
4190
4190
*
4191
4191
* The idea behind this type is to be able to compute more precise types
4192
4192
* when more information is available.
@@ -4200,7 +4200,6 @@ object Types {
4200
4200
* TypeOf(u, Apply(fun, args)) ~ SuspendedApply(u, fun, args)
4201
4201
* TypeOf(u, TypeApply(fun, args)) ~ SuspendedTypeApply(u, fun, args)
4202
4202
* TypeOf(u, If(cond, thenp, elsep)) ~ SuspendedIf(u, cond, thenp, elsep)
4203
- * TypeOf(u, Match(selector, cases)) ~ SuspendedMatch(u, selector, cases)
4204
4203
*
4205
4204
* Where u is the type that the tree would have had otherwise.
4206
4205
*
@@ -4229,16 +4228,13 @@ object Types {
4229
4228
}
4230
4229
case nil => args2.isEmpty
4231
4230
}
4232
- // FIXME: compareArgs is incorrect for Match. Add compareCaseDef.
4233
4231
(this .tree, that.tree) match {
4234
4232
case (t1 : Apply , t2 : Apply ) =>
4235
4233
(t1.fun.tpe eql t2.fun.tpe) && compareArgs(t1.args, t2.args)
4236
4234
case (t1 : TypeApply , t2 : TypeApply ) =>
4237
4235
(t1.fun.tpe eql t2.fun.tpe) && compareArgs(t1.args, t2.args)
4238
4236
case (t1 : If , t2 : If ) =>
4239
4237
(t1.cond.tpe eql t2.cond.tpe) && (t1.thenp.tpe eql t2.thenp.tpe) && (t1.elsep.tpe eql t2.elsep.tpe)
4240
- case (t1 : Match , t2 : Match ) =>
4241
- (t1.selector.tpe eql t2.selector.tpe) && compareArgs(t1.cases, t2.cases)
4242
4238
case (t1, t2) =>
4243
4239
false
4244
4240
}
@@ -4251,7 +4247,6 @@ object Types {
4251
4247
override def computeHash (bs : Hashable .Binders ) = {
4252
4248
val delta = tree match {
4253
4249
case _ : If => 11
4254
- case _ : Match => 17
4255
4250
case _ : Apply => 23
4256
4251
case _ : TypeApply => 29
4257
4252
}
@@ -4269,6 +4264,7 @@ object Types {
4269
4264
4270
4265
def apply (underlyingTp : Type , tree : untpd.Tree )(implicit ctx : Context ): TypeOf = {
4271
4266
assert(! ctx.erasedTypes)
4267
+ assert(isLegalTopLevelTree(tree.asInstanceOf [Tree ]))
4272
4268
val tree1 = tree.clone.asInstanceOf [Tree ]
4273
4269
// This is a safety net to keep us from touching a TypeOf's tree's type.
4274
4270
// Assuming we never look at this type, it would be safe to simply reuse
@@ -4282,7 +4278,7 @@ object Types {
4282
4278
def unapply (to : TypeOf ): Option [(Type , Tree )] = Some ((to.underlyingTp, to.tree))
4283
4279
4284
4280
def isLegalTopLevelTree (tree : Tree ): Boolean = tree match {
4285
- case _ : TypeApply | _ : Apply | _ : If | _ : Match => true
4281
+ case _ : TypeApply | _ : Apply | _ : If => true
4286
4282
case _ => false
4287
4283
}
4288
4284
@@ -4324,6 +4320,16 @@ object Types {
4324
4320
tp
4325
4321
4326
4322
object If {
4323
+ /** For explicit creation in patmat */
4324
+ def apply (condTp : Type , thenTp : Type , elseTp : Type )(implicit ctx : Context ): TypeOf = {
4325
+ val thenTree = dummyTreeOfType(thenTp)
4326
+ val elseTree = dummyTreeOfType(elseTp)
4327
+ val ifTree = untpd.If (dummyTreeOfType(condTp), thenTree, elseTree)
4328
+ ctx.typeAssigner.assignType(ifTree, thenTree, elseTree)(ctx.enterTypeOf()).tpe
4329
+ .asInstanceOf [TypeOf ]
4330
+ }
4331
+
4332
+ /** For unpickling */
4327
4333
def apply (underlyingTp : Type , condTp : Type , thenTp : Type , elseTp : Type )(implicit ctx : Context ): TypeOf =
4328
4334
TypeOf (underlyingTp, untpd.If (
4329
4335
dummyTreeOfType(condTp),
@@ -4348,32 +4354,35 @@ object Types {
4348
4354
}
4349
4355
4350
4356
object Match {
4351
- def apply (underlyingTp : Type , selectorTp : Type ,
4352
- caseTriples : List [(Type , Type , Type )])(implicit ctx : Context ): TypeOf =
4353
- TypeOf (underlyingTp, untpd.Match (
4354
- dummyTreeOfType(selectorTp),
4355
- caseTriples.map { case (patTp, guardTp, bodyTp) =>
4356
- ast.tpd.CaseDef (
4357
- dummyTreeOfType(patTp),
4358
- if (guardTp.exists) dummyTreeOfType(guardTp) else EmptyTree ,
4359
- dummyTreeOfType(bodyTp))
4360
- }
4361
- ))
4362
-
4363
- def unapply (to : TypeOf ): Option [(Type , List [(Type , Type , Type )])] = to.tree match {
4364
- case Trees .Match (selector, cases) =>
4365
- val caseTriples = cases.map { cse => (cse.pat.tpe, cse.guard.tpe, cse.body.tpe) }
4366
- Some ((selector.tpe, caseTriples))
4367
- case _ => None
4368
- }
4369
-
4370
- def derived (to : TypeOf )(selectorTp : Type , caseTriples : List [(Type , Type , Type )])(implicit ctx : Context ): Type =
4371
- finalizeDerived(to, to.tree match {
4372
- case Trees .Match (selector, cases) =>
4373
- val ctx1 = ctx.enterTypeOf()
4374
- val cases1 = casesWithTpes(cases, caseTriples)(ctx1)
4375
- cpy.Match (to.tree)(treeWithTpe(selector, selectorTp), cases1)(ctx1)
4376
- })
4357
+ def apply (underlyingTp : Type , tree : Match )(implicit ctx : Context ): Type =
4358
+ new transform.PatternMatcher .Translator (NoType , null )(ctx.enterTypeOf())
4359
+ .typeTranslateMatch(tree)
4360
+ // def apply(underlyingTp: Type, selectorTp: Type,
4361
+ // caseTriples: List[(Type, Type, Type)])(implicit ctx: Context): TypeOf =
4362
+ // TypeOf(underlyingTp, untpd.Match(
4363
+ // dummyTreeOfType(selectorTp),
4364
+ // caseTriples.map { case (patTp, guardTp, bodyTp) =>
4365
+ // ast.tpd.CaseDef(
4366
+ // dummyTreeOfType(patTp),
4367
+ // if (guardTp.exists) dummyTreeOfType(guardTp) else EmptyTree,
4368
+ // dummyTreeOfType(bodyTp))
4369
+ // }
4370
+ // ))
4371
+
4372
+ // def unapply(to: TypeOf): Option[(Type, List[(Type, Type, Type)])] = to.tree match {
4373
+ // case Trees.Match(selector, cases) =>
4374
+ // val caseTriples = cases.map { cse => (cse.pat.tpe, cse.guard.tpe, cse.body.tpe) }
4375
+ // Some((selector.tpe, caseTriples))
4376
+ // case _ => None
4377
+ // }
4378
+
4379
+ // def derived(to: TypeOf)(selectorTp: Type, caseTriples: List[(Type, Type, Type)])(implicit ctx: Context): Type =
4380
+ // finalizeDerived(to, to.tree match {
4381
+ // case Trees.Match(selector, cases) =>
4382
+ // val ctx1 = ctx.enterTypeOf()
4383
+ // val cases1 = casesWithTpes(cases, caseTriples)(ctx1)
4384
+ // cpy.Match(to.tree)(treeWithTpe(selector, selectorTp), cases1)(ctx1)
4385
+ // })
4377
4386
}
4378
4387
4379
4388
object Apply {
@@ -4492,9 +4501,6 @@ object Types {
4492
4501
object Generic {
4493
4502
def unapply (to : TypeOf ): Option [List [Type ]] = to.tree match {
4494
4503
case Trees .If (cond, thenb, elseb) => Some (cond.tpe :: thenb.tpe :: elseb.tpe :: Nil )
4495
- case Trees .Match (selector, cases) =>
4496
- val caseTriplesFlattened = cases.flatMap { cse => List (cse.pat.tpe, cse.guard.tpe, cse.body.tpe) }
4497
- Some (selector.tpe :: caseTriplesFlattened)
4498
4504
case Trees .Apply (fn, args) => Some (fn.tpe :: args.map(_.tpe))
4499
4505
case Trees .TypeApply (fn, args) => Some (fn.tpe :: args.map(_.tpe))
4500
4506
}
@@ -4661,9 +4667,6 @@ object Types {
4661
4667
TypeOf .Apply .derived(tp)(this (tree.fun.tpe), tree.args.map(t => this (t.tpe)))
4662
4668
case tree : If =>
4663
4669
TypeOf .If .derived(tp)(this (tree.cond.tpe), this (tree.thenp.tpe), this (tree.elsep.tpe))
4664
- case tree : Match =>
4665
- val caseTriples = tree.cases.map { cse => (this (cse.pat.tpe), this (cse.guard.tpe), this (cse.body.tpe)) }
4666
- TypeOf .Match .derived(tp)(this (tree.selector.tpe), caseTriples)
4667
4670
case tree =>
4668
4671
throw new AssertionError (s " TypeOf shouldn't contain $tree as top-level node. " )
4669
4672
}
0 commit comments