Skip to content

Commit f5e6985

Browse files
authored
Perform Matchable check only if type test is needed (scala#16824)
Fixes scala#16808
2 parents ef815fd + ff3fab2 commit f5e6985

File tree

8 files changed

+8
-10
lines changed

8 files changed

+8
-10
lines changed

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,6 @@ trait Applications extends Compatibility {
12691269
def typedUnApply(tree: untpd.Apply, selType: Type)(using Context): Tree = {
12701270
record("typedUnApply")
12711271
val Apply(qual, args) = tree
1272-
if !ctx.mode.is(Mode.InTypeTest) then
1273-
checkMatchable(selType, tree.srcPos, pattern = true)
12741272

12751273
def notAnExtractor(tree: Tree): Tree =
12761274
// prefer inner errors
@@ -1409,12 +1407,13 @@ trait Applications extends Compatibility {
14091407
val unapplyArgType = mt.paramInfos.head
14101408
unapp.println(i"unapp arg tpe = $unapplyArgType, pt = $selType")
14111409
val ownType =
1412-
if (selType <:< unapplyArgType) {
1410+
if selType <:< unapplyArgType then
14131411
unapp.println(i"case 1 $unapplyArgType ${ctx.typerState.constraint}")
14141412
fullyDefinedType(unapplyArgType, "pattern selector", tree.srcPos)
14151413
selType.dropAnnot(defn.UncheckedAnnot) // need to drop @unchecked. Just because the selector is @unchecked, the pattern isn't.
1416-
}
1417-
else {
1414+
else
1415+
if !ctx.mode.is(Mode.InTypeTest) then
1416+
checkMatchable(selType, tree.srcPos, pattern = true)
14181417
// We ignore whether constraining the pattern succeeded.
14191418
// Constraining only fails if the pattern cannot possibly match,
14201419
// but useless pattern checks detect more such cases, so we simply rely on them instead.
@@ -1423,7 +1422,7 @@ trait Applications extends Compatibility {
14231422
if (patternBound.nonEmpty) unapplyFn = addBinders(unapplyFn, patternBound)
14241423
unapp.println(i"case 2 $unapplyArgType ${ctx.typerState.constraint}")
14251424
unapplyArgType
1426-
}
1425+
14271426
val dummyArg = dummyTreeOfType(ownType)
14281427
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
14291428
def unapplyImplicits(unapp: Tree): List[Tree] = {

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

-1
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

+1-3
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()
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)