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

Handle sealed prefixes in exh checking #16621

Merged
merged 1 commit into from
Jan 19, 2023
Merged

Conversation

dwijnand
Copy link
Member

@dwijnand dwijnand commented Jan 5, 2023

No description provided.

@dwijnand dwijnand marked this pull request as ready for review January 9, 2023 11:34
@dwijnand dwijnand requested a review from liufengyun January 9, 2023 16:07
@dwijnand dwijnand force-pushed the exh-sealed-path branch 2 times, most recently from 391b433 to 0e52250 Compare January 15, 2023 10:05
@dwijnand dwijnand requested a review from liufengyun January 15, 2023 10:06
case X.Field() =>
case X.Thing =>
case Y.Field() =>
case Y.Thing =>
Copy link
Contributor

Choose a reason for hiding this comment

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

In the example mentioned above, the code is as follows:

  def handle(schema: Schema[String]) =
    schema match // was: match may not be exhaustive
      case Y.Field() =>
      case Y.Thing   =>

Copy link
Member Author

Choose a reason for hiding this comment

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

So, in that case it still does the right thing, though the showType logic isn't handling it very well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I was not clear. If I run the following example with this PR:

// scalac: -Werror
sealed trait Schema[A]

sealed class Outer:
  sealed trait RecordInstances[B]:
    case class Field() extends Schema[B]
    case object Thing extends Schema[B]

object O1 extends Outer
object O2 extends Outer

object X extends O1.RecordInstances[Int]
object Y extends O2.RecordInstances[String]

// Match not exhaustive error! (with fatal warnings :P)
class Test:
  def handle(schema: Schema[String]) =
    schema match // was: match may not be exhaustive
      case Y.Field() =>
      case Y.Thing   =>

I get the following warning:

-- [E029] Pattern Match Exhaustivity Warning: tests/pos/i15029.more.scala:18:4 -
18 |    schema match // was: match may not be exhaustive
   |    ^^^^^^
   |    match may not be exhaustive.
   |
   |    It would fail on pattern case: X.Field(), Thing
   |
   | longer explanation available when compiling with `-explain`

I expect no warnings in this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, my mistake, didn't see the Schema[String] either time.

Copy link
Member Author

Choose a reason for hiding this comment

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

Excellent, found a simplier way to do it, which makes your more complicated version work! ❤️

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

Elegant fix, LGTM 👍

@dwijnand dwijnand self-assigned this Jan 18, 2023
@dwijnand dwijnand enabled auto-merge January 19, 2023 15:21
@dwijnand dwijnand merged commit 13b8d7d into scala:main Jan 19, 2023
@dwijnand dwijnand deleted the exh-sealed-path branch January 23, 2023 10:40
@Kordyjan Kordyjan added this to the 3.3.0 milestone Aug 1, 2023
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.

Incorrect inexhaustivity warning when leafs are defined in an unrelated sealed trait
3 participants