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 #5067: handle scrutinee of bottom type in pattern matcher #5075

Merged
merged 2 commits into from
Feb 15, 2019

Conversation

abeln
Copy link
Contributor

@abeln abeln commented Sep 3, 2018

When desugaring pattern matching code for expressions where the
matched value has type Null or Nothing, we used to generate code
that's type-incorrect.

Example:

val Some(x) = null

got desugared into

val x: Nothing =
      matchResult1[Nothing]:
        {
          case val x1: Null @unchecked = null: Null @unchecked
          if x1.ne(null) then
            {
              case val x: Nothing = x1.value.asInstanceOf[Nothing]
              return[matchResult1] x: Nothing
            }
           else ()
          return[matchResult1] throw new MatchError(x1)
        }

There were two problems here:

  1. x1.ne(null)
  2. x1.value

In both cases, we're trying to invoke methods that don't exist for type
Nothing (and #2 doesn't exist for Null).

This commits changes the desugaring so that

  1. is solved by adding an ascription, if needed: (x1: AnyRef).ne(null)
  2. is added by generating throw-away but type-correct code that never
    executes: throw null

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.

Adding a new kind of plan to the pattern matcher is a fairly big change/complication. To do this "just" to make the compiler not crash for a non-sensical case might go too far. I have a tendency to do what scalac does and disallow these patterns.

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.

@abeln Can you change the PR to outlaw these situations instead? Thanks!

@sjrd
Copy link
Member

sjrd commented Jan 12, 2019

Here is another suggestion that does not require another plan:

  • Keep generating the TestPlan(NonNullTest, ...), but in the onSuccess use a ResultPlan(tpd.Throw(tpd.Null())), with a comment explaining that this is dead code anyway.
  • In testNonNull, insert an ascription (_: AnyRef) before calling eq if the receiver's type is Null or Nothing.

@odersky
Copy link
Contributor

odersky commented Jan 12, 2019

@sjrd I like that approach. @abeln Are you happy to continue the implementation?

@abeln
Copy link
Contributor Author

abeln commented Feb 7, 2019

@odersky yes, I'll take a look. Got busy preparing the proposal for explicit nulls!

When desugaring pattern matching code for expressions where the
matched value has type `Null` or `Nothing`, we used to generate code
that's type-incorrect.

Example:
```
val Some(x) = null
```

got desugared into
```
val x: Nothing =
      matchResult1[Nothing]:
        {
          case val x1: Null @unchecked = null: Null @unchecked
          if x1.ne(null) then
            {
              case val x: Nothing = x1.value.asInstanceOf[Nothing]
              return[matchResult1] x: Nothing
            }
           else ()
          return[matchResult1] throw new MatchError(x1)
        }
```

There were two problems here:
1) `x1.ne(null)`
2) `x1.value`

In both cases, we're trying to invoke methods that don't exist for type
`Nothing` (and #2 doesn't exist for `Null`).

This commits changes the desugaring so that
1) is solved by adding an ascription, if needed: (x1: AnyRef).ne(null)
2) is added by generating throw-away but type-correct code that never
executes: `throw null`
@abeln abeln changed the title Fix #5067: Ycheck failure in pattern matching against a value of type… Fix #5067: handle scrutinee of bottom type in pattern matcher Feb 12, 2019
@abeln
Copy link
Contributor Author

abeln commented Feb 12, 2019

@odersky PTAL
Implemented the approach suggested by @sjrd and added the test suggested by @Blaisorblade

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.

My main concern is the duplication in isSubtypeOfBottom.

@odersky odersky assigned abeln and unassigned odersky Feb 14, 2019
Flip some conditions.
Remove isSubtypeOfBottom.
Add new tests.
@abeln
Copy link
Contributor Author

abeln commented Feb 14, 2019

@odersky PTAL

@abeln abeln assigned odersky and unassigned odersky and abeln Feb 14, 2019
abeln added a commit to abeln/dotty that referenced this pull request Feb 14, 2019
To desugar constructs like
```
val Some(_) = null
```

this uses the approach in scala#5075
as opposed to the older approach that used NoOpPlans.
@odersky
Copy link
Contributor

odersky commented Feb 15, 2019

@abeln Looks good now.

@odersky odersky merged commit 4c3e170 into scala:master Feb 15, 2019
@ghost ghost removed the stat:needs review label Feb 15, 2019
@abeln abeln deleted the pattern-match-nothing branch February 15, 2019 19:13
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.

None yet

4 participants