Skip to content

Commit ff3fab2

Browse files
committed
Perform Matchable check only if type test is needed
Fixes #16808
1 parent af95ceb commit ff3fab2

File tree

8 files changed

+8
-10
lines changed

8 files changed

+8
-10
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,8 +1258,6 @@ trait Applications extends Compatibility {
12581258
def typedUnApply(tree: untpd.Apply, selType: Type)(using Context): Tree = {
12591259
record("typedUnApply")
12601260
val Apply(qual, args) = tree
1261-
if !ctx.mode.is(Mode.InTypeTest) then
1262-
checkMatchable(selType, tree.srcPos, pattern = true)
12631261

12641262
def notAnExtractor(tree: Tree): Tree =
12651263
// prefer inner errors
@@ -1398,12 +1396,13 @@ trait Applications extends Compatibility {
13981396
val unapplyArgType = mt.paramInfos.head
13991397
unapp.println(i"unapp arg tpe = $unapplyArgType, pt = $selType")
14001398
val ownType =
1401-
if (selType <:< unapplyArgType) {
1399+
if selType <:< unapplyArgType then
14021400
unapp.println(i"case 1 $unapplyArgType ${ctx.typerState.constraint}")
14031401
fullyDefinedType(unapplyArgType, "pattern selector", tree.srcPos)
14041402
selType.dropAnnot(defn.UncheckedAnnot) // need to drop @unchecked. Just because the selector is @unchecked, the pattern isn't.
1405-
}
1406-
else {
1403+
else
1404+
if !ctx.mode.is(Mode.InTypeTest) then
1405+
checkMatchable(selType, tree.srcPos, pattern = true)
14071406
// We ignore whether constraining the pattern succeeded.
14081407
// Constraining only fails if the pattern cannot possibly match,
14091408
// but useless pattern checks detect more such cases, so we simply rely on them instead.
@@ -1412,7 +1411,7 @@ trait Applications extends Compatibility {
14121411
if (patternBound.nonEmpty) unapplyFn = addBinders(unapplyFn, patternBound)
14131412
unapp.println(i"case 2 $unapplyArgType ${ctx.typerState.constraint}")
14141413
unapplyArgType
1415-
}
1414+
14161415
val dummyArg = dummyTreeOfType(ownType)
14171416
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
14181417
def unapplyImplicits(unapp: Tree): List[Tree] = {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,6 @@ trait Checking {
14611461

14621462
def checkMatchable(tp: Type, pos: SrcPos, pattern: Boolean)(using Context): Unit =
14631463
if !tp.derivesFrom(defn.MatchableClass) && sourceVersion.isAtLeast(`future-migration`) then
1464-
val kind = if pattern then "pattern selector" else "value"
14651464
report.warning(MatchableWarning(tp, pattern), pos)
14661465

14671466
/** Check that there is an implicit capability to throw a checked exception

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ class CompilationTests {
4848
compileFilesInDir("tests/pos", defaultOptions.and("-Ysafe-init", "-Ylegacy-lazy-vals", "-Ycheck-constraint-deps"), FileFilter.include(TestSources.posLazyValsAllowlist)),
4949
compileFilesInDir("tests/pos-deep-subtype", allowDeepSubtypes),
5050
compileFilesInDir("tests/pos-custom-args/no-experimental", defaultOptions.and("-Yno-experimental")),
51+
compileFilesInDir("tests/pos-custom-args/strict", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
5152
compileDir("tests/pos-special/java-param-names", defaultOptions.withJavacOnlyOptions("-parameters")),
5253
compileFile(
5354
// succeeds despite -Xfatal-warnings because of -nowarn
5455
"tests/neg-custom-args/fatal-warnings/xfatalWarnings.scala",
5556
defaultOptions.and("-nowarn", "-Xfatal-warnings")
5657
),
5758
compileFile("tests/pos-special/typeclass-scaling.scala", defaultOptions.and("-Xmax-inlines", "40")),
58-
compileFile("tests/pos-special/i7296.scala", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
59-
compileDir("tests/pos-special/adhoc-extension", defaultOptions.and("-source", "future", "-feature", "-Xfatal-warnings")),
6059
compileFile("tests/pos-special/i7575.scala", defaultOptions.andLanguageFeature("dynamics")),
6160
compileFile("tests/pos-special/kind-projector.scala", defaultOptions.and("-Ykind-projector")),
6261
compileFile("tests/pos-special/kind-projector-underscores.scala", defaultOptions.and("-Ykind-projector:underscores")),
@@ -65,7 +64,6 @@ class CompilationTests {
6564
compileFile("tests/pos-custom-args/i9267.scala", defaultOptions.and("-Ystop-after:erasure")),
6665
compileFile("tests/pos-special/extend-java-enum.scala", defaultOptions.and("-source", "3.0-migration")),
6766
compileFile("tests/pos-custom-args/help.scala", defaultOptions.and("-help", "-V", "-W", "-X", "-Y")),
68-
compileFile("tests/pos-custom-args/i10383.scala", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
6967
compileFile("tests/pos-custom-args/i13044.scala", defaultOptions.and("-Xmax-inlines:33")),
7068
compileFile("tests/pos-custom-args/jdk-8-app.scala", defaultOptions.and("-release:8")),
7169
).checkCompile()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def collectKeys[A, B, C](xs: Map[A, B])(f: PartialFunction[A, C]): Map[C, B] =
2+
xs.collect{ case (f(c) , b) => (c, b) }

0 commit comments

Comments
 (0)