From 8c0f444ba550dbd2aa7071cf840aec7b6ada03cb Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 3 Jun 2013 23:48:35 +0200 Subject: SI-5022 Retain precise existentials through pattern matching From the dawn of scalac's existentials, the typer widens existentials pt-s by substituting wildcard types in places of existential quantifiers. In this example: class ForSomeVsUnapply { def test { def makeWrap: Wrap = ??? def useRep[e](rep: (e, X[e])) = () val rep = makeWrap match { case Wrap(r) => r }; useRep(rep) // error } } the type of `r` is the result of typechecking: Apply( fun = TypeTree( tpe = (rep#12037: (e#12038, X#7041[e#12038]) forSome { type e#12038 }) args = Bind(r @ _) :: Nil } This descends to type the `Bind` with: pt = (e#12038, X#7041[e#12038]) forSome { type e#12038 } `dropExistential` clobbers that type to `Tuple2#1540[?, X#7041[?]]`, which doesn't express any relationship between the two instances of the wildcard type. `typedIdent` sort of reverses this with a call to `makeFullyDefined`, but only ends up with: pt = (Any#3330, X#7041[_1#12227]) forSome { type _1#12227; type e#12038 } I suspect that this existential dropping only makes sense outside of typechecking patterns. In pattern mode, type information flows from the expected type onwards to the body of the case; we must not lose precision in the types. For SIP-18 friendly existentials, one `dropExistential` is invertable with `makeFullyDefined`, so this hasn't been such a big problem. The error message improvement conferred by SI-4515 took a hit. That might be a good example to consider when reviewing this change: Does it tell us anything interesting about this `dropExistential` business? --- .../scala/tools/nsc/typechecker/Typers.scala | 9 +++++++-- test/files/neg/t4515.check | 4 ++-- test/files/pos/t2613.scala | 11 +++++++++++ test/files/pos/t5022.scala | 22 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 test/files/pos/t2613.scala create mode 100644 test/files/pos/t5022.scala diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 238727d2e9..53eafd956d 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -5108,7 +5108,7 @@ trait Typers extends Adaptations with Tags { if (mode.inPatternMode) { val uncheckedTypeExtractor = extractorForUncheckedType(tpt.pos, tptTyped.tpe) // make fully defined to avoid bounded wildcard types that may be in pt from calling dropExistential (SI-2038) - val ptDefined = ensureFullyDefined(pt) + val ptDefined = ensureFullyDefined(pt) // FIXME this is probably redundant now that we don't dropExistenial in pattern mode. val ownType = inferTypedPattern(tptTyped, tptTyped.tpe, ptDefined, canRemedy = uncheckedTypeExtractor.nonEmpty) treeTyped setType ownType @@ -5342,7 +5342,12 @@ trait Typers extends Adaptations with Tags { "context.owner" -> context.owner ) ) - typed1(tree, mode, dropExistential(ptPlugins)) + val ptWild = if (mode.inPatternMode) + ptPlugins // SI-5022 don't widen pt for patterns as types flow from it to the case body. + else + dropExistential(ptPlugins) // FIXME: document why this is done. + + typed1(tree, mode, ptWild) } // Can happen during erroneous compilation - error(s) have been // reported, but we need to avoid causing an NPE with this tree diff --git a/test/files/neg/t4515.check b/test/files/neg/t4515.check index a60d16295f..64e7cc1ca7 100644 --- a/test/files/neg/t4515.check +++ b/test/files/neg/t4515.check @@ -1,6 +1,6 @@ t4515.scala:37: error: type mismatch; - found : _0(in value $anonfun) where type _0(in value $anonfun) - required: (some other)_0(in value $anonfun) + found : _$1 where type _$1 + required: _$2 handler.onEvent(target, ctx.getEvent, node, ctx) ^ one error found diff --git a/test/files/pos/t2613.scala b/test/files/pos/t2613.scala new file mode 100644 index 0000000000..3a64dbc282 --- /dev/null +++ b/test/files/pos/t2613.scala @@ -0,0 +1,11 @@ +import language.existentials + +object Test { + class Row + + abstract class MyRelation [R <: Row, +Relation <: MyRelation[R, Relation]] + + type M = MyRelation[R, Relation] forSome {type R <: Row; type Relation <: MyRelation[R, Relation]} + + var (x,y): (String, M) = null +} diff --git a/test/files/pos/t5022.scala b/test/files/pos/t5022.scala new file mode 100644 index 0000000000..b9a085fb35 --- /dev/null +++ b/test/files/pos/t5022.scala @@ -0,0 +1,22 @@ +class ForSomeVsUnapply { + def test { + def makeWrap: Wrap = ??? + def useRep[e](rep: (e, X[e])) = () + + val repUnapply = Wrap.unapply(makeWrap).get + useRep(repUnapply) // okay + + val Wrap(rep0) = makeWrap + useRep(rep0) // error + + val rep = makeWrap match { + case Wrap(r) => r + }; + + useRep(rep) // error + } +} + +class X[e] + +case class Wrap(rep: (e, X[e]) forSome { type e }) -- cgit v1.2.3