Skip to content

Commit c07bd13

Browse files
committed
Add backup TypeTests for Symbol and ClassSymbol
These are needed once we move to opaque types
1 parent e9f60a8 commit c07bd13

35 files changed

+92
-53
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Types._, Contexts._, Flags._
77
import Symbols._, Annotations._, Trees._, Symbols._, Constants.Constant
88
import Decorators._
99
import dotty.tools.dotc.transform.SymUtils._
10+
import Symbols.TypeTests.given
1011

1112
/** A map that applies three functions and a substitution together to a tree and
1213
* makes sure they are coordinated so that the result is well-typed. The functions are

compiler/src/dotty/tools/dotc/cc/CaptureOps.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import config.Printers.capt
1010
import util.Property.Key
1111
import tpd.*
1212
import config.Feature
13+
import Symbols.TypeTests.given
1314

1415
private val Captures: Key[CaptureSet] = Key()
1516
private val BoxedType: Key[BoxedTypeCache] = Key()

compiler/src/dotty/tools/dotc/cc/Setup.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import transform.Recheck.*
1212
import CaptureSet.IdentityCaptRefMap
1313
import Synthetics.isExcluded
1414
import util.Property
15+
import Symbols.TypeTests.given
1516

1617
/** A tree traverser that prepares a compilation unit to be capture checked.
1718
* It does the following:

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ast.tpd, tpd.*
77
import util.Spans.Span
88
import printing.{Showable, Printer}
99
import printing.Texts.Text
10+
import Symbols.TypeTests.given
1011

1112
import scala.annotation.internal.sharable
1213

@@ -194,7 +195,7 @@ object Annotations {
194195
object Annotation {
195196

196197
def apply(tree: Tree): ConcreteAnnotation = ConcreteAnnotation(tree)
197-
198+
198199
def apply(cls: ClassSymbol, span: Span)(using Context): Annotation =
199200
apply(cls, Nil, span)
200201

@@ -206,7 +207,7 @@ object Annotations {
206207

207208
def apply(atp: Type, arg: Tree, span: Span)(using Context): Annotation =
208209
apply(atp, arg :: Nil, span)
209-
210+
210211
def apply(atp: Type, args: List[Tree], span: Span)(using Context): Annotation =
211212
apply(New(atp, args).withSpan(span))
212213

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Contexts._, Symbols._, Types._, Flags._
55
import Denotations._, SymDenotations._
66
import Names.Name, StdNames.nme
77
import ast.untpd
8+
import Symbols.TypeTests.given
89

910
/** Extension methods for contexts where we want to keep the ctx.<methodName> syntax */
1011
object ContextOps:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package core
55
import Contexts._, Symbols._, Types._, Flags._, Scopes._, Decorators._, Names._, NameOps._
66
import SymDenotations.{LazyType, SymDenotation}, StdNames.nme
77
import TypeApplications.EtaExpansion
8+
import Symbols.TypeTests.given
89

910
/** Operations that are shared between Namer and TreeUnpickler */
1011
object NamerOps:

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import transform.TypeUtils._
2828
import cc.{CapturingType, derivedCapturingType, Setup, EventuallyCapturingType, isEventuallyCapturingType}
2929

3030
import scala.annotation.internal.sharable
31+
import Symbols.TypeTests.given
3132

3233
object SymDenotations {
3334

@@ -1993,7 +1994,7 @@ object SymDenotations {
19931994
/** The explicitly given self type (self types of modules are assumed to be
19941995
* explcitly given here).
19951996
*/
1996-
def givenSelfType(using Context): Type = classInfo.selfInfo match {
1997+
def givenSelfType(using Context): Type = (classInfo.selfInfo: @unchecked) match { // !!! dotty problem: opaque exhaustive
19971998
case tp: Type => tp
19981999
case self: Symbol => self.info
19992000
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import util.{SourceFile, NoSource, Property, SourcePosition, SrcPos, EqHashMap}
3232
import scala.annotation.internal.sharable
3333
import config.Printers.typr
3434
import annotation.tailrec
35+
import scala.reflect.TypeTest
3536

3637
object Symbols {
3738

@@ -46,6 +47,19 @@ object Symbols {
4647
opaque type ClassSymbl <: Symbl
4748
= ClassDenotation
4849

50+
object TypeTests:
51+
52+
given SymTest: TypeTest[AnyRef, Symbol] with
53+
def unapply(x: AnyRef): Option[x.type & Symbol] = x match
54+
case sd: SymDenotation => Some(sd.asInstanceOf)
55+
case _ => None
56+
57+
given ClsTest: TypeTest[AnyRef, ClassSymbol] with
58+
def unapply(x: AnyRef): Option[x.type & ClassSymbol] = x match
59+
case cd: ClassDenotation => Some(cd.asInstanceOf)
60+
case _ => None
61+
end TypeTests
62+
4963
/** A Symbol represents a Scala definition/declaration or a package.
5064
* @param coord The coordinates of the symbol (a position or an index)
5165
* @param id A unique identifier of the symbol (unique per ContextBase)
@@ -84,7 +98,7 @@ object Symbols {
8498
type TermSymbol = Symbol { type ThisName = TermName }
8599
type TypeSymbol = Symbol { type ThisName = TypeName }
86100

87-
extension (x: AnyRef)
101+
extension (x: Any)
88102
inline def isSymbol: Boolean = x.isInstanceOf[Symbol]
89103
inline def asSymbol: Symbol = x.asInstanceOf[Symbol]
90104

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Names._
1212
import Flags.{Module, Provisional}
1313
import dotty.tools.dotc.config.Config
1414
import cc.boxedUnlessFun
15+
import Symbols.TypeTests.given
1516

1617
object TypeApplications {
1718

@@ -217,7 +218,7 @@ class TypeApplications(val self: Type) extends AnyVal {
217218
/** If `self` is a generic class, its type parameter symbols, otherwise Nil */
218219
final def typeParamSymbols(using Context): List[TypeSymbol] = typeParams match {
219220
case tparams @ (_: Symbol) :: _ =>
220-
assert(tparams.forall(_.isInstanceOf[Symbol]))
221+
assert(tparams.forall(_.isSymbol))
221222
tparams.asInstanceOf[List[TypeSymbol]]
222223
// Note: Two successive calls to typeParams can yield different results here because
223224
// of different completion status. I.e. the first call might produce some symbols,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import typer.Applications.productSelectorTypes
2424
import reporting.trace
2525
import annotation.constructorOnly
2626
import cc.{CapturingType, derivedCapturingType, CaptureSet, stripCapturing, isBoxedCapturing, boxed, boxedUnlessFun, boxedIfTypeParam, isAlwaysPure}
27+
import Symbols.TypeTests.given
2728

2829
/** Provides methods to compare types.
2930
*/

0 commit comments

Comments
 (0)