Skip to content

Commit 1d0717d

Browse files
committed
Add special handling when the selector of a pattern matching has Nothing type
1 parent 7c49506 commit 1d0717d

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,8 @@ trait Applications extends Compatibility {
16861686
if selType <:< unapplyArgType then
16871687
unapp.println(i"case 1 $unapplyArgType ${ctx.typerState.constraint}")
16881688
fullyDefinedType(unapplyArgType, "pattern selector", tree.srcPos)
1689-
selType.dropAnnot(defn.UncheckedAnnot) // need to drop @unchecked. Just because the selector is @unchecked, the pattern isn't.
1689+
if selType.isNothingType then unapplyArgType
1690+
else selType.dropAnnot(defn.UncheckedAnnot) // need to drop @unchecked. Just because the selector is @unchecked, the pattern isn't.
16901691
else
16911692
if !ctx.mode.is(Mode.InTypeTest) then
16921693
checkMatchable(selType, tree.srcPos, pattern = true)
@@ -1708,7 +1709,7 @@ trait Applications extends Compatibility {
17081709
val unapplyPatterns = UnapplyArgs(unapplyApp.tpe, unapplyFn, unadaptedArgs, tree.srcPos)
17091710
.typedPatterns(qual, this)
17101711
val result = assignType(cpy.UnApply(tree)(newUnapplyFn, unapplyImplicits(dummyArg, unapplyApp), unapplyPatterns), ownType)
1711-
if (ownType.stripped eq selType.stripped) || ownType.isError then result
1712+
if (ownType.stripped eq selType.stripped) || selType.isNothingType || ownType.isError then result
17121713
else tryWithTypeTest(Typed(result, TypeTree(ownType)), selType)
17131714
case tp =>
17141715
val unapplyErr = if (tp.isError) unapplyFn else notAnExtractor(unapplyFn)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,6 +2812,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
28122812
if isStableIdentifierOrLiteral || isNamedTuplePattern then pt
28132813
else if isWildcardStarArg(body1)
28142814
|| pt == defn.ImplicitScrutineeTypeRef
2815+
|| pt.isNothingType
28152816
|| body1.tpe <:< pt // There is some strange interaction with gadt matching.
28162817
// and implicit scopes.
28172818
// run/t2755.scala fails to compile if this subtype test is omitted

tests/neg/i7294.check

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
-- [E007] Type Mismatch Error: tests/neg/i7294.scala:7:15 --------------------------------------------------------------
1+
-- [E007] Type Mismatch Error: tests/neg/i7294.scala:7:18 --------------------------------------------------------------
22
7 | case x: T => x.g(10) // error
3-
| ^
4-
| Found: (x : Nothing)
5-
| Required: ?{ g: ? }
6-
| Note that implicit conversions were not tried because the result of an implicit conversion
7-
| must be more specific than ?{ g: [applied to (10) returning T] }
3+
| ^^^^^^^
4+
| Found: Any
5+
| Required: T
6+
|
7+
| where: T is a type in given instance f with bounds <: foo.Foo
88
|
99
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)