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
Allow name-based extractors to be irrefutable #9343
Conversation
cc27bfc
to
6ac807b
Compare
src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
Supporting exhaustiveness/unused checking over multiple arities of unapplySeq extractors is quite a bit more complicated: if element arity exhaustiveness is checked at all yet, I would appreciate a pointer to it. Otherwise, the ambition level of covering understanding that |
add8a01
to
d1f40cd
Compare
now also fixes scala/bug#12254: allow |
0390431
to
11cc9c1
Compare
This comment has been minimized.
This comment has been minimized.
11cc9c1
to
1f83070
Compare
Oh, I didn't see that the I -update-check'ed that, and for pushed the fixup. Happy for review in either state. Whether the error message should complain about an extractor of |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
what's so weird is that obviously the isEmpty of UnapplySeqWrapper was constant folded, but I wasn't able to construct any other source where isEmpty was getting constant folded. |
See my |
Ah, I see! That's the darndest thing -- and scary too. Maybe it doesn't just apply to this situation. |
580f404
to
a2dc411
Compare
Lukas wondered if the unapply could be constant folded too, so I added a similar test for that and it's not constant folded - so all is good. |
24a14d6
to
af59cc3
Compare
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.
Just some minor suggestions.
src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala
Outdated
Show resolved
Hide resolved
src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala
Outdated
Show resolved
Hide resolved
src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala
Outdated
Show resolved
Hide resolved
src/compiler/scala/tools/nsc/transform/patmat/PatternExpansion.scala
Outdated
Show resolved
Hide resolved
af59cc3
to
a275b61
Compare
🎉 👏 |
Extractors (SLS 2.13 8.1.9) are objects with an
unapply
and/orunapplySeq
methods that can be used as patterns in match statements.Name-based extractors are extractors where the
unapply
method doesn't returnOption
but some other type, with a contract that it has (1) a methodget
returning the extracted thing and (2) a methodisEmpty
returningfalse
if the extractor matches andtrue
if it doesn't.Prior to this change, having the static return type
Some
instead ofOption
forunapply
indicated that the extractor is "irrefutable", meaning the compiler knows the pattern always matches, which it uses for reachability and exhaustiveness analysis. This PR adds two more forms of irrefutable extractors:unapply
returns the singleton typetrue
instead ofBoolean
isEmpty
returns the singleton typefalse
instead ofBoolean
With these changes, using such extractors removes what would otherwise be spurious false positive exhaustivity warnings.
PR implementation notes:
Irrefutable name-based extractors on
isEmpty: false
Check/update specwontfix in this PR. Neither name-based extractors nor irrefutable extractors are specced.Dotty: https://github.com/lampepfl/dotty/blob/cda0a9d496cc8fd6c10cb4c90c0a521a2c6820df/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala#L292-L320 : constant false isEmpty is irrefutable
List special casing: Will still be needed as is, probably won't be needed anymore if we can check if all arities of
unapplySeq
have cases.spun out the added testcases in 12240 to their own bug in scala/bug#12252
Fixes scala/bug#12240
Fixes scala/bug#12254