Skip to content

Commit 78ebe0a

Browse files
committed
Fix scala#8869: Make $isInstanceOf$ and $asInstanceOf$ semantic names
1 parent e4bb383 commit 78ebe0a

File tree

10 files changed

+26
-8
lines changed

10 files changed

+26
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
881881
tree.select(defn.Any_typeCast).appliedToType(AndType(tree.tpe, tpnn))
882882

883883
def unapply(tree: tpd.TypeApply)(using Context): Option[tpd.Tree] = tree match
884-
case TypeApply(Select(qual: RefTree, nme.asInstanceOfPM), arg :: Nil) =>
884+
case TypeApply(Select(qual: RefTree, _), arg :: Nil) if tree.symbol == defn.Any_typeCast =>
885885
arg.tpe match
886886
case AndType(ref, _) if qual.tpe eq ref => Some(qual)
887887
case _ => None

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ class Definitions {
276276
@tu lazy val Any_## : TermSymbol = enterMethod(AnyClass, nme.HASHHASH, ExprType(IntType), Final)
277277
@tu lazy val Any_isInstanceOf: TermSymbol = enterT1ParameterlessMethod(AnyClass, nme.isInstanceOf_, _ => BooleanType, Final)
278278
@tu lazy val Any_asInstanceOf: TermSymbol = enterT1ParameterlessMethod(AnyClass, nme.asInstanceOf_, _.paramRefs(0), Final)
279-
@tu lazy val Any_typeTest: TermSymbol = enterT1ParameterlessMethod(AnyClass, nme.isInstanceOfPM, _ => BooleanType, Final | Synthetic | Artifact)
280-
@tu lazy val Any_typeCast: TermSymbol = enterT1ParameterlessMethod(AnyClass, nme.asInstanceOfPM, _.paramRefs(0), Final | Synthetic | Artifact | StableRealizable)
279+
@tu lazy val Any_typeTest: TermSymbol = enterT1ParameterlessMethod(AnyClass, NameKinds.IsInstanceOf, _ => BooleanType, Final | Synthetic | Artifact)
280+
@tu lazy val Any_typeCast: TermSymbol = enterT1ParameterlessMethod(AnyClass, NameKinds.AsInstanceOf, _.paramRefs(0), Final | Synthetic | Artifact | StableRealizable)
281281
// generated by pattern matcher and explicit nulls, eliminated by erasure
282282

283283
/** def getClass[A >: this.type](): Class[? <: A] */

compiler/src/dotty/tools/dotc/core/NameKinds.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ object NameKinds {
363363
val ImplMethName: SuffixNameKind = new SuffixNameKind(IMPLMETH, "$")
364364
val AdaptedClosureName: SuffixNameKind = new SuffixNameKind(ADAPTEDCLOSURE, "$adapted") { override def definesNewName = true }
365365

366+
val InstanceOf: SuffixNameKind = new SuffixNameKind(INSTANCEOF, "InstanceOf$")
367+
val IsInstanceOf: TermName = InstanceOf(nme.isInstanceOfPM)
368+
val AsInstanceOf: TermName = InstanceOf(nme.asInstanceOfPM)
369+
366370
/** A name together with a signature. Used in Tasty trees. */
367371
object SignedName extends NameKind(SIGNED) {
368372

compiler/src/dotty/tools/dotc/core/NameTags.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ object NameTags extends TastyFormat.NameTags {
3636
case EXPANDED => "EXPANDED"
3737
case EXPANDPREFIX => "EXPANDPREFIX"
3838
case TRAITSETTER => "TRAITSETTER"
39+
case INSTANCEOF => "INSTANCEOF"
3940
case UNIQUE => "UNIQUE"
4041
case DEFAULTGETTER => "DEFAULTGETTER"
4142
case OUTERSELECT => "OUTERSELECT"

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ object StdNames {
420420
val asType: N = "asType"
421421
val asClass: N = "asClass"
422422
val asInstanceOf_ : N = "asInstanceOf"
423-
val asInstanceOfPM: N = "$asInstanceOf$"
423+
val asInstanceOfPM: N = "$as"
424424
val assert_ : N = "assert"
425425
val assume_ : N = "assume"
426426
val box: N = "box"
@@ -499,7 +499,7 @@ object StdNames {
499499
val isDefined: N = "isDefined"
500500
val isEmpty: N = "isEmpty"
501501
val isInstanceOf_ : N = "isInstanceOf"
502-
val isInstanceOfPM: N = "$isInstanceOf$"
502+
val isInstanceOfPM: N = "$is"
503503
val java: N = "java"
504504
val key: N = "key"
505505
val lang: N = "lang"

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ object Splicer {
248248
assert(args.isEmpty)
249249
interpretedStaticFieldAccess(fn.symbol)
250250
else if (fn.qualifier.symbol.is(Module) && fn.qualifier.symbol.isStatic)
251-
if (fn.name == nme.asInstanceOfPM)
251+
if fn.name == NameKinds.AsInstanceOf then
252252
interpretModuleAccess(fn.qualifier.symbol)
253253
else {
254254
val staticMethodCall = interpretedStaticMethodCall(fn.qualifier.symbol.moduleClass, fn.symbol)

compiler/src/dotty/tools/dotc/typer/Dynamic.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dotty.tools.dotc.ast.untpd
88
import dotty.tools.dotc.core.Constants.Constant
99
import dotty.tools.dotc.core.Contexts.Context
1010
import dotty.tools.dotc.core.Names.{Name, TermName}
11+
import dotty.tools.dotc.core.NameKinds
1112
import dotty.tools.dotc.core.StdNames._
1213
import dotty.tools.dotc.core.Types._
1314
import dotty.tools.dotc.core.Decorators._
@@ -23,8 +24,8 @@ object Dynamic {
2324
}
2425

2526
object DynamicUnapply {
26-
def unapply(tree: tpd.Tree): Option[List[tpd.Tree]] = tree match
27-
case TypeApply(Select(qual, name), _) if name == nme.asInstanceOfPM =>
27+
def unapply(tree: tpd.Tree)(using Context): Option[List[tpd.Tree]] = tree match
28+
case TypeApply(Select(qual, _), _) if tree.symbol == defn.Any_typeCast =>
2829
unapply(qual)
2930
case Apply(Apply(Select(selectable, fname), Literal(Constant(name)) :: ctag :: Nil), _ :: implicits)
3031
if fname == nme.applyDynamic && (name == "unapply" || name == "unapplySeq") =>

tasty/src/dotty/tools/tasty/TastyFormat.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ object TastyFormat {
263263
final val EXPANDPREFIX = 4 // An expansion prefix `<prefix>$<suffix>`,
264264
// used by Scala-2 for private names.
265265

266+
final val INSTANCEOF = 7 // Type test of cast on Any `$isInsctanceOf$` and `$asInsctanceOf$`
267+
266268
final val UNIQUE = 10 // A unique name `<name>$<num>` where `<num>`
267269
// is used only once for each `<name>`.
268270

tests/neg/i8869.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- [E008] Not Found Error: tests/neg/i8869.scala:1:15 ------------------------------------------------------------------
2+
1 |val test1 = "".$asInstanceOf$[Int] // error
3+
| ^^^^^^^^^^^^^^^^^
4+
| value $asInstanceOf$ is not a member of String - did you mean ("" : String).asInstanceOf?
5+
-- [E008] Not Found Error: tests/neg/i8869.scala:2:15 ------------------------------------------------------------------
6+
2 |val test2 = "".$isInstanceOf$[Int] // error
7+
| ^^^^^^^^^^^^^^^^^
8+
| value $isInstanceOf$ is not a member of String - did you mean ("" : String).isInstanceOf?

tests/neg/i8869.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val test1 = "".$asInstanceOf$[Int] // error
2+
val test2 = "".$isInstanceOf$[Int] // error

0 commit comments

Comments
 (0)