Skip to content

Commit a455861

Browse files
authored
Merge pull request scala#6216 from retronym/ticket/sd455
Revert "Type Alternative patterns as the lub of their branches."
2 parents 652c75b + eba7c78 commit a455861

File tree

7 files changed

+24
-56
lines changed

7 files changed

+24
-56
lines changed

spec/08-pattern-matching.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,11 @@ shorthand for the constructor or extractor pattern $\mathit{op}(p, q_1
280280
Pattern ::= Pattern1 { ‘|’ Pattern1 }
281281
```
282282

283-
A _pattern alternative_ `$p_1$ | $\ldots$ | $p_n$` consists of a number of
284-
_alternative patterns_ $p_i$. All alternative patterns are type checked with
285-
the expected type of the pattern. The type of the pattern alternative is the
286-
[weak least upper bound](03-types.html#weak-conformance) of the types of
287-
$p_1 , \ldots , p_n$.
288-
289-
Alternative patterns may not bind variables other than wildcards.
290-
The alternative pattern matches a value $v$ if at least one its alternatives
291-
matches $v$.
283+
A _pattern alternative_ `$p_1$ | $\ldots$ | $p_n$`
284+
consists of a number of alternative patterns $p_i$. All alternative
285+
patterns are type checked with the expected type of the pattern. They
286+
may not bind variables other than wildcards. The alternative pattern
287+
matches a value $v$ if at least one its alternatives matches $v$.
292288

293289
### XML Patterns
294290

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,15 +1185,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
11851185
} else tree.tpe match {
11861186
case atp @ AnnotatedType(_, _) if canAdaptAnnotations(tree, this, mode, pt) => // (-1)
11871187
adaptAnnotations(tree, this, mode, pt)
1188-
case ct @ ConstantType(value) if mode.inNone(TYPEmode | FUNmode | PATTERNmode) && (ct <:< pt) && canAdaptConstantTypeToLiteral => // (0)
1189-
/* We don't adapt in PATTERNmode because patmat checks for duplicated
1190-
* alternatives later on in MatchOptimizations, so that this desugars
1191-
* to a single check. Moreover, as we don't adaptConstant now, we are
1192-
* able to issue a "duplicated alternative" warning at the same time;
1193-
* see scala/bug#7290 for context. Theoretically I could warn earlier
1194-
* in typedAlternative, but there are other checks done in patmat, so
1195-
* it's easier just to wait until then.
1196-
*/
1188+
case ct @ ConstantType(value) if mode.inNone(TYPEmode | FUNmode) && (ct <:< pt) && canAdaptConstantTypeToLiteral => // (0)
11971189
adaptConstant(value)
11981190
case OverloadedType(pre, alts) if !mode.inFunMode => // (1)
11991191
inferExprAlternative(tree, pt)
@@ -5262,11 +5254,9 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
52625254
}
52635255

52645256
def typedAlternative(alt: Alternative) = {
5265-
context withinPatAlternative {
5266-
val trees1 = alt.trees mapConserve (alt => typed(alt, mode, pt))
5267-
val tpe = if (settings.isScala213) weakLub(trees1 map (_.tpe)) else pt
5268-
treeCopy.Alternative(tree, trees1) setType tpe
5269-
}
5257+
context withinPatAlternative (
5258+
treeCopy.Alternative(tree, alt.trees mapConserve (alt => typed(alt, mode, pt))) setType pt
5259+
)
52705260
}
52715261
def typedStar(tree: Star) = {
52725262
if (!context.starPatterns && !isPastTyper)

src/reflect/scala/reflect/internal/tpe/GlbLubs.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private[internal] trait GlbLubs {
219219
def sameWeakLubAsLub(tps: List[Type]) = tps match {
220220
case Nil => true
221221
case tp :: Nil => !typeHasAnnotations(tp)
222-
case tps => !(tps exists typeHasAnnotations) && !shouldUseNumericLub(tps)
222+
case tps => !(tps exists typeHasAnnotations) && !(tps forall isNumericValueType)
223223
}
224224

225225
/** If the arguments are all numeric value types, the numeric
@@ -231,17 +231,14 @@ private[internal] trait GlbLubs {
231231
def weakLub(tps: List[Type]): Type = (
232232
if (tps.isEmpty)
233233
NothingTpe
234-
else if (shouldUseNumericLub(tps))
234+
else if (tps forall isNumericValueType)
235235
numericLub(tps)
236236
else if (tps exists typeHasAnnotations)
237237
annotationsLub(lub(tps map (_.withoutAnnotations)), tps)
238238
else
239239
lub(tps)
240240
)
241241

242-
private def shouldUseNumericLub(tps: List[Type]) =
243-
tps forall (tp => isNumericValueType(tp.widen))
244-
245242
def numericLub(ts: List[Type]) =
246243
ts reduceLeft ((t1, t2) =>
247244
if (isNumericSubType(t1, t2)) t2

test/files/pos/alternative-lubs.flags

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/files/pos/alternative-lubs.scala

Lines changed: 0 additions & 27 deletions
This file was deleted.

test/files/run/sd455.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
true
2+
false
3+
default

test/files/run/sd455.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Const { final val VAL = 1 ; final val VAR = 2 }
2+
import Const._
3+
object Test {
4+
def test(i: Int) = i match { case v @ (VAR | VAL) => v == VAR case _ => "default" }
5+
def main(args: Array[String]) {
6+
println(test(VAR))
7+
println(test(VAL))
8+
println(test(-1))
9+
}
10+
}

0 commit comments

Comments
 (0)