Skip to content

Fix #4999: allow pattern matching against by-name arguments #5012

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ object PatternMatcher {
/** Widen type as far as necessary so that it does not refer to a pattern-
* generated variable.
*/
private def sanitize(tp: Type): Type = tp.widenExpr match {
private def sanitize(tp: Type): Type = tp.widenIfUnstable match {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is also widenTermRefExpr that seems to work here. Not sure which one is the most appropriate one. I defer to @odersky or @smarter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah the first commit uses widenTermRefExpr. Maybe @liufengyun can think of examples where widening singletons would make a difference?

Copy link
Member

Choose a reason for hiding this comment

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

What about x match { case _: x.type => ... } ?

Copy link
Contributor

Choose a reason for hiding this comment

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

We concluded with @smarter that there is no need to widen singleton types

case tp: TermRef if refersToInternal(false, tp) => sanitize(tp.underlying)
case tp => tp
}
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i4999.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait Foo
final class Bar extends Foo

class Test {
def test(xs: => Foo) = xs match {
case xs: Bar => 1
case _ => 2
}
}