Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform Matchable check only if type test is needed #16824

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1258,8 +1258,6 @@ trait Applications extends Compatibility {
def typedUnApply(tree: untpd.Apply, selType: Type)(using Context): Tree = {
record("typedUnApply")
val Apply(qual, args) = tree
if !ctx.mode.is(Mode.InTypeTest) then
checkMatchable(selType, tree.srcPos, pattern = true)

def notAnExtractor(tree: Tree): Tree =
// prefer inner errors
Expand Down Expand Up @@ -1398,12 +1396,13 @@ trait Applications extends Compatibility {
val unapplyArgType = mt.paramInfos.head
unapp.println(i"unapp arg tpe = $unapplyArgType, pt = $selType")
val ownType =
if (selType <:< unapplyArgType) {
if selType <:< unapplyArgType then
unapp.println(i"case 1 $unapplyArgType ${ctx.typerState.constraint}")
fullyDefinedType(unapplyArgType, "pattern selector", tree.srcPos)
selType.dropAnnot(defn.UncheckedAnnot) // need to drop @unchecked. Just because the selector is @unchecked, the pattern isn't.
}
else {
else
if !ctx.mode.is(Mode.InTypeTest) then
checkMatchable(selType, tree.srcPos, pattern = true)
// We ignore whether constraining the pattern succeeded.
// Constraining only fails if the pattern cannot possibly match,
// but useless pattern checks detect more such cases, so we simply rely on them instead.
Expand All @@ -1412,7 +1411,7 @@ trait Applications extends Compatibility {
if (patternBound.nonEmpty) unapplyFn = addBinders(unapplyFn, patternBound)
unapp.println(i"case 2 $unapplyArgType ${ctx.typerState.constraint}")
unapplyArgType
}

val dummyArg = dummyTreeOfType(ownType)
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
def unapplyImplicits(unapp: Tree): List[Tree] = {
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,6 @@ trait Checking {

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

/** Check that there is an implicit capability to throw a checked exception
Expand Down
4 changes: 1 addition & 3 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ class CompilationTests {
compileFilesInDir("tests/pos", defaultOptions.and("-Ysafe-init", "-Ylegacy-lazy-vals", "-Ycheck-constraint-deps"), FileFilter.include(TestSources.posLazyValsAllowlist)),
compileFilesInDir("tests/pos-deep-subtype", allowDeepSubtypes),
compileFilesInDir("tests/pos-custom-args/no-experimental", defaultOptions.and("-Yno-experimental")),
compileFilesInDir("tests/pos-custom-args/strict", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
compileDir("tests/pos-special/java-param-names", defaultOptions.withJavacOnlyOptions("-parameters")),
compileFile(
// succeeds despite -Xfatal-warnings because of -nowarn
"tests/neg-custom-args/fatal-warnings/xfatalWarnings.scala",
defaultOptions.and("-nowarn", "-Xfatal-warnings")
),
compileFile("tests/pos-special/typeclass-scaling.scala", defaultOptions.and("-Xmax-inlines", "40")),
compileFile("tests/pos-special/i7296.scala", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
compileDir("tests/pos-special/adhoc-extension", defaultOptions.and("-source", "future", "-feature", "-Xfatal-warnings")),
compileFile("tests/pos-special/i7575.scala", defaultOptions.andLanguageFeature("dynamics")),
compileFile("tests/pos-special/kind-projector.scala", defaultOptions.and("-Ykind-projector")),
compileFile("tests/pos-special/kind-projector-underscores.scala", defaultOptions.and("-Ykind-projector:underscores")),
Expand All @@ -65,7 +64,6 @@ class CompilationTests {
compileFile("tests/pos-custom-args/i9267.scala", defaultOptions.and("-Ystop-after:erasure")),
compileFile("tests/pos-special/extend-java-enum.scala", defaultOptions.and("-source", "3.0-migration")),
compileFile("tests/pos-custom-args/help.scala", defaultOptions.and("-help", "-V", "-W", "-X", "-Y")),
compileFile("tests/pos-custom-args/i10383.scala", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
compileFile("tests/pos-custom-args/i13044.scala", defaultOptions.and("-Xmax-inlines:33")),
compileFile("tests/pos-custom-args/jdk-8-app.scala", defaultOptions.and("-release:8")),
).checkCompile()
Expand Down
2 changes: 2 additions & 0 deletions tests/pos-custom-args/strict/i16808.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def collectKeys[A, B, C](xs: Map[A, B])(f: PartialFunction[A, C]): Map[C, B] =
xs.collect{ case (f(c) , b) => (c, b) }
File renamed without changes.