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

Unexhaustive match in function literal disregarded when implicitly converting to PartialFunction #20196

Open
cornerman opened this issue Apr 15, 2024 · 3 comments

Comments

@cornerman
Copy link
Contributor

Just stumbled upon a behavior with implicit conversion to PartialFunction that I did not expect.

I have an unexhaustive match inside a body of a function that is implicitly converted to PartialFunction - which results in an "empty" PartialFunction. But when using PartialFunction.fromFunction or extracting the code out, I get a MatchError.

Compiler version

3.4.1 (and 2.13.13)

Minimized code

val f0: PartialFunction[Int, String] = _ => 1 match { case 2 => "never" }
println(f0.lift(10))

Also see: https://scastie.scala-lang.org/gvGk2zh5QLWwuuYdKrI8SQ

Output

None

Expectation

It should throw a MatchError (and warn about an unexhaustive match).

Potentially relevant spec https://www.scala-lang.org/files/archive/spec/3.4/06-expressions.html (thanks to the discord help for finding this):

When a PartialFunction is required, an additional member isDefinedAt is synthesized, which simply returns true. However, if the function literal has the shape x => x match { $...$ }, then isDefinedAt is derived from the pattern match in the following way: each case from the match expression evaluates to true, and if there is no default case, a default case is added that evaluates to false. For more details on how that is implemented see "Pattern Matching Anonymous Functions".

@cornerman cornerman added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 15, 2024
@som-snytt
Copy link
Contributor

Thanks, I was reminded to open the next ticket, which links to the Scala 2 ticket with a long discussion about what is "clearly a bug", "actually kind of a feature", and "plausibly desirable". Just to demonstrate that it remains a dark corner.

@cornerman
Copy link
Contributor Author

I am interested in that discussion. I came across code using it like x => x.someMember match { /* unexhaustive case */ }, which is kind of nice and short.

Though, I found the whole conversion suprising and it took me some time when i realized it today. I never really noticed before.

@Gedochao Gedochao added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 16, 2024
@Gedochao
Copy link

May be connected to #20197 (which is probably the ticket @som-snytt mentioned)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants