-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Make singleton typecase patterns and type checks consistent #7027
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,7 +144,14 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase => | |
} | ||
|
||
override def transformIdent(tree: Ident)(implicit ctx: Context): Tree = | ||
if (tree.isType) toTypeTree(tree) else constToLiteral(tree) | ||
if (tree.isType) { | ||
toTypeTree(tree) | ||
} else if (tree.name != nme.WILDCARD) { | ||
// We want to constant-fold _some_ idents here - for instance, @switch needs to see literals in patterns. | ||
// However, constant-foldable wildcards can occur in patterns, for instance as `case _: "a"`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why we need to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I guess because the latter is desugared to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Argh, I see where the comment is hilariously unclear. Give me a second. |
||
// we avoid constant-folding those as doing so would change the meaning of the pattern. | ||
constToLiteral(tree) | ||
} else tree | ||
|
||
override def transformSelect(tree: Select)(implicit ctx: Context): Tree = | ||
if (tree.isType) toTypeTree(tree) else constToLiteral(tree) | ||
|
@@ -156,7 +163,9 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase => | |
constToLiteral(foldCondition(tree)) | ||
|
||
override def transformTyped(tree: Typed)(implicit ctx: Context): Tree = | ||
constToLiteral(tree) | ||
// Singleton type cases (such as `case _: "a"`) are constant-foldable | ||
// we avoid constant-folding those as doing so would change the meaning of the pattern | ||
if (!ctx.mode.is(Mode.Pattern)) constToLiteral(tree) else tree | ||
|
||
override def transformBlock(tree: Block)(implicit ctx: Context): Tree = | ||
constToLiteral(tree) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
an `a` | ||
false | ||
not `a` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
object Test { | ||
|
||
def isAType(arg: String): Unit = arg match { | ||
case _ : "a" => println("an `a`") | ||
case _ => println("not `a`") | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
isAType("a") | ||
println(new String("a").isInstanceOf["a"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I argue in #6996 that this should return true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regardless of the result of that discussion, I think we can merge this PR. It's unlikely that we want |
||
isAType(new String("a")) | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.