Skip to content

Commit 19f9ae7

Browse files
committed
Use == for instance tests against string constants
We used `eq` before, as for other objetc singletons. But this means we are subject to the vagaries of string hashing. Fixes scala#12796
1 parent fcd837a commit 19f9ae7

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
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"))

0 commit comments

Comments
 (0)