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

Fix (part of) #6323: trust case class unapplies to produce checkable type tests #6551

Merged
merged 2 commits into from
Jun 6, 2019

Conversation

abgruszecki
Copy link
Contributor

If the type test comes from an unapply method of a case class, we trust it to generate a runtime-checkable type test, even if the test doesn't appear checkable. This fixes spurious warnings of the following kind:

sealed trait Foo[A, B]
final case class Bar[X](x: X) extends Foo[X, X]

def foo[A, B](value: Foo[A, B], a: A => Int): B = value match {
    case Bar(x) => a(x); x
}
-- Warning: test.scala:8:9 -----------------------------------------------------
8 |    case Bar() => a
  |         ^^^^^
  |         the type test for G.Bar[A] cannot be checked at runtime
one warning found

The cause of the warning, as can be gleamed from the error message, is that we infer Bar#X = A. Bar[A] is only guaranteed to be a subtype of Foo[A, B] under GADT constraint A = B, but by the time we check type tests, that information is gone.

Note that in the case where the unapply cannot possibly match, exhaustivity checker will still issue an error.

@abgruszecki abgruszecki force-pushed the trust-case-class-unapply branch from 92c23ee to 96a6a40 Compare May 22, 2019 17:16
@abgruszecki abgruszecki requested a review from odersky May 23, 2019 09:24
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM

@odersky odersky assigned abgruszecki and unassigned odersky May 29, 2019
Note that in the case where the unapply cannot match, exhaustivity
checker will issue a warning.
@abgruszecki abgruszecki force-pushed the trust-case-class-unapply branch from 96a6a40 to 94cff4a Compare June 6, 2019 09:41
@abgruszecki abgruszecki merged commit 3cf3dd3 into scala:master Jun 6, 2019
@abgruszecki abgruszecki deleted the trust-case-class-unapply branch June 6, 2019 11:31
odersky added a commit to dotty-staging/dotty that referenced this pull request Jul 13, 2022
scala#6551 introduced an exception where type tests of parameterized case classes were not flagged
as "type test cannot be checked at runtime". The idea was that the parameters would be inferred
by GADT reasoning and would therefore be correct.

But with scala#15356 we now also allow explicit type arguments in extractors. If these are given
we cannot guarantee that a type test for a case class in an unapply will always succeed.
So we need an unchecked warning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants