Skip to content

Commit fb6b453

Browse files
authored
Merge pull request scala#12799 from dotty-staging/fix-12796
Use `==` for instance tests against string constants
2 parents 2ccf681 + 82b94e9 commit fb6b453

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
963963

964964
/** `tree.isInstanceOf[tp]`, with special treatment of singleton types */
965965
def isInstance(tp: Type)(using Context): Tree = tp.dealias match {
966+
case ConstantType(c) if c.tag == StringTag =>
967+
singleton(tp).equal(tree)
966968
case tp: SingletonType =>
967-
if (tp.widen.derivesFrom(defn.ObjectClass))
969+
if tp.widen.derivesFrom(defn.ObjectClass) then
968970
tree.ensureConforms(defn.ObjectType).select(defn.Object_eq).appliedTo(singleton(tp))
969971
else
970972
singleton(tp).equal(tree)

tests/run/i12796/A_1.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object A:
2+
type Timeframe = "1m" | "2m" | "1H"
3+
4+
def test(input: String) = input.isInstanceOf[Timeframe]

tests/run/i12796/Test_2.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@main def Test =
2+
val x = "1"
3+
assert(A.test(x + "m"))

tests/run/i6996.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
an `a`
2-
false
3-
not `a`
2+
true
3+
an `a`

0 commit comments

Comments
 (0)