File tree Expand file tree Collapse file tree 3 files changed +27
-2
lines changed
compiler/src/dotty/tools/dotc/transform Expand file tree Collapse file tree 3 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -144,7 +144,13 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase =>
144
144
}
145
145
146
146
override def transformIdent (tree : Ident )(implicit ctx : Context ): Tree =
147
- if (tree.isType) toTypeTree(tree) else constToLiteral(tree)
147
+ if (tree.isType) {
148
+ toTypeTree(tree)
149
+ } else if (tree.name != nme.WILDCARD ) {
150
+ // Constant-foldable wildcards can occur in patterns, for instance as `case _: "a"`
151
+ // we avoid constant-folding those as doing so would change the meaning of the pattern
152
+ constToLiteral(tree)
153
+ } else tree
148
154
149
155
override def transformSelect (tree : Select )(implicit ctx : Context ): Tree =
150
156
if (tree.isType) toTypeTree(tree) else constToLiteral(tree)
@@ -156,7 +162,9 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase =>
156
162
constToLiteral(foldCondition(tree))
157
163
158
164
override def transformTyped (tree : Typed )(implicit ctx : Context ): Tree =
159
- constToLiteral(tree)
165
+ // Singleton type cases (such as `case _: "a"`) are constant-foldable
166
+ // we avoid constant-folding those as doing so would change the meaning of the pattern
167
+ if (! ctx.mode.is(Mode .Pattern )) constToLiteral(tree) else tree
160
168
161
169
override def transformBlock (tree : Block )(implicit ctx : Context ): Tree =
162
170
constToLiteral(tree)
Original file line number Diff line number Diff line change
1
+ an `a`
2
+ false
3
+ not `a`
Original file line number Diff line number Diff line change
1
+ object Test {
2
+
3
+ def isAType (arg : String ): Unit = arg match {
4
+ case _ : " a" => println(" an `a`" )
5
+ case _ => println(" not `a`" )
6
+ }
7
+
8
+ def main (args : Array [String ]): Unit = {
9
+ isAType(" a" )
10
+ println(new String (" a" ).isInstanceOf [" a" ])
11
+ isAType(new String (" a" ))
12
+ }
13
+
14
+ }
You can’t perform that action at this time.
0 commit comments