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
Make extractor patterns null safe #6485
Conversation
2dc5ec1
to
b6a007d
Compare
See also: https://github.com/scala/scala/pull/6485/files#diff-352de7f9c77a7e21df6940d6a1bcdc7eL115, where a type test is potentially emitted before an extractor call, which could also rule out null args. |
Note that Dotty already has the behavior implemented by this PR. |
} else { | ||
val nonNullTest = NonNullTestTreeMaker(typeTest.nextBinder, paramType, pos) | ||
val unappBinder = nonNullTest.nextBinder | ||
(typeTest :: nonNullTest :: treeMakers(unappBinder, pos), unappBinder) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adriaanm Instead of doing null check in ExtractorTreeMaker
(like I had in b6a007d), I've created a new tree maker NonNullTestTreeMaker
. This is positioned after typeTest
. If binderKnowNonNull
is true
, I am skipping the test.
I've removed null checking in ProductExtractorTreeMaker
and passing binderKnowNonNull
around since it's now redundant.
cee9f3a
to
f08f7b5
Compare
Rebased and squashed. |
f08f7b5
to
df7fc06
Compare
df7fc06
to
1f0c8a7
Compare
Rebased and slightly reworded. |
Until now, the spec did not say anything about `null` for extractor pattern, so that: - The pattern matcher would happily pass `null` into your extractor. - One could write a `null`-matching extractor `MyNull`. - But all extractor authors must consider `null` as a possible argument value. No more! The pattern matcher inserts a non-`null` check before invoking an extractor, so that you don't have to. This is a general fix for scala/bug#2241, scala/bug#8787. See scala/bug#4364.
1f0c8a7
to
a60eaeb
Compare
List((1,2),(3,4),(5,6)) == z | ||
} | ||
|
||
def run(): Unit = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated test for procedure syntax deprecation.
Ping |
LGTM -- checked diff for |
Ref scala/bug#4364
This is a general fix for scala/bug#2241 and scala/bug#8787.
scala/bug#4364 raises the question of what should the pattern matcher do when encountering a
null
and an extractor pattern. As it stands, SLS does not say anything aboutnull
for extractor pattern, which leads:null
into your extractor.null
-matching extractor MyNull.null
if they would like to access the passed in argumentx
.There are potentially two things we can do.
null
.null
is treated as no match (empty).I've already sent option 1 as #6374.
This implements option 2.