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
*/

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import scala.annotation.internal.sharable
4343
import scala.annotation.threadUnsafe
4444

4545
import dotty.tools.dotc.transform.SymUtils._
46+
import Symbols.TypeTests.given
4647

4748
object Types {
4849

@@ -2719,7 +2720,7 @@ object Types {
27192720
if (prefix eq this.prefix) this
27202721
else if !NamedType.validPrefix(prefix) then UnspecifiedErrorType
27212722
else if (lastDenotation == null) NamedType(prefix, designator)
2722-
else designator match {
2723+
else (designator: @unchecked) match { // !!! dotty opaque exhaustivity problem
27232724
case sym: Symbol =>
27242725
if (infoDependsOnPrefix(sym, prefix) && !prefix.isArgPrefixOf(sym)) {
27252726
val candidate = reload()
@@ -2899,7 +2900,7 @@ object Types {
28992900
}
29002901

29012902
object NamedType {
2902-
def isType(desig: Designator)(using Context): Boolean = desig match {
2903+
def isType(desig: Designator)(using Context): Boolean = (desig: @unchecked) match { // !!! dotty opaque exhaustivity problem
29032904
case sym: Symbol => sym.isType
29042905
case name: Name => name.isTypeName
29052906
}
@@ -5036,7 +5037,7 @@ object Types {
50365037
TypeAlias(
50375038
withMode(Mode.CheckCyclic)(
50385039
LazyRef.of(force))))
5039-
cinfo.selfInfo match
5040+
(cinfo.selfInfo: @unchecked) match // !!! dotty opaque exhaustivity problem
50405041
case self: Type =>
50415042
cinfo.derivedClassInfo(
50425043
selfInfo = refineSelfType(self.orElse(defn.AnyType)))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package core
44
import Types._, Contexts._, Flags._, Symbols._, Annotations._
55
import TypeApplications.TypeParamInfo
66
import Decorators._
7+
import Symbols.TypeTests.given
78

89
object Variances {
910

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import scala.annotation.switch
2323
import typer.Checking.checkNonCyclic
2424
import io.{AbstractFile, ZipArchive}
2525
import scala.util.control.NonFatal
26+
import Symbols.TypeTests.given
2627

2728
object ClassfileParser {
2829
/** Marker trait for unpicklers that can be embedded in classfiles. */

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import config.Config
1919
import collection.mutable
2020
import reporting.{Profile, NoProfile}
2121
import dotty.tools.tasty.TastyFormat.ASTsSection
22+
import core.Symbols.TypeTests.given
2223

2324
object TreePickler:
2425
class StackSizeExceeded(val mdef: tpd.MemberDef) extends Exception
@@ -212,7 +213,7 @@ class TreePickler(pickler: TastyPickler) {
212213
report.error(em"pickling reference to as yet undefined $tpe with symbol ${sym}", sym.srcPos)
213214
pickleSymRef(sym)
214215
}
215-
else tpe.designator match {
216+
else (tpe.designator: @unchecked) match { // !!! dotty opaque exhaustivity problem
216217
case name: Name =>
217218
writeByte(if (tpe.isType) TYPEREF else TERMREF)
218219
pickleName(name); pickleType(tpe.prefix)
@@ -598,7 +599,7 @@ class TreePickler(pickler: TastyPickler) {
598599
else {
599600
if (!tree.self.isEmpty) registerTreeAddr(tree.self)
600601
pickleType {
601-
selfInfo match {
602+
(selfInfo: @unchecked) match { // !!! dotty opaque exhaustivity problem
602603
case sym: Symbol => sym.info
603604
case tp: Type => tp
604605
}

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import dotty.tools.tasty.TastyFormat._
4646

4747
import scala.annotation.constructorOnly
4848
import scala.annotation.internal.sharable
49+
import Symbols.TypeTests.given
4950

5051
/** Unpickler for typed trees
5152
* @param reader the reader from which to unpickle

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Erasure.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package unpickleScala2
66
import Symbols._, Types._, Contexts._, Flags._, Names._, StdNames._, Phases._
77
import Decorators._
88
import scala.collection.mutable.ListBuffer
9+
import Symbols.TypeTests.given
910

1011
/** Erasure logic specific to Scala 2 symbols. */
1112
object Scala2Erasure:
@@ -161,7 +162,7 @@ object Scala2Erasure:
161162
* pseudo-symbol, if its upper-bound is a sub of that pseudo-symbol.
162163
*/
163164
def goUpperBound(psym: Symbol | StructuralRef): Boolean =
164-
val info = psym match
165+
val info = (psym: @unchecked) match // !!! dotty opaque exhaustivity problem
165166
case sym: Symbol => sym.info
166167
case tp: StructuralRef => tp.info
167168
info match
@@ -178,7 +179,7 @@ object Scala2Erasure:
178179
// is an abstract type upper-bounded by this refinement, since each
179180
// textual appearance of a refinement will have its own class symbol.
180181
!that.isInstanceOf[Scala2RefinedType] &&
181-
psym.match
182+
(psym: @unchecked).match // !!! dotty opaque exhaustivity problem
182183
case sym1: Symbol => that match
183184
case sym2: Symbol =>
184185
if sym1.isClass && sym2.isClass then

compiler/src/dotty/tools/dotc/fromtasty/ReadTasty.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Denotations.staticRef
1111
import NameOps._
1212
import ast.Trees.Tree
1313
import Phases.Phase
14+
import Symbols.TypeTests.given
1415

1516

1617
/** Load trees from TASTY files */

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import scala.util.control.NonFatal
1616
import scala.annotation.switch
1717
import config.{Config, Feature}
1818
import cc.{CapturingType, EventuallyCapturingType, CaptureSet, isBoxed}
19+
import Symbols.TypeTests.given
1920

2021
class PlainPrinter(_ctx: Context) extends Printer {
2122

compiler/src/dotty/tools/dotc/reporting/Message.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import config.SourceVersion
1212
import scala.language.unsafeNulls
1313

1414
import scala.annotation.threadUnsafe
15+
import Symbols.TypeTests.given
1516

1617
/** ## Tips for error message generation
1718
*
@@ -154,7 +155,7 @@ object Message:
154155
/** Produce a where clause with explanations for recorded iterms.
155156
*/
156157
def explanations(using Context): String =
157-
def needsExplanation(entry: Recorded) = entry match {
158+
def needsExplanation(entry: Recorded) = (entry: @unchecked) match { // !!! dotty opaque exhaustivity problem
158159
case param: TypeParamRef => ctx.typerState.constraint.contains(param)
159160
case param: ParamRef => false
160161
case skolem: SkolemType => true

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import scala.annotation.{ threadUnsafe => tu, tailrec }
2424
import scala.PartialFunction.condOpt
2525

2626
import dotty.tools.dotc.{semanticdb => s}
27+
import Symbols.TypeTests.given
2728

2829
/** Extract symbol references and uses to semanticdb files.
2930
* See https://scalameta.org/docs/semanticdb/specification.html#symbol-1

compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import core.StdNames.nme
1212
import SymbolInformation.{Kind => k}
1313
import dotty.tools.dotc.util.SourceFile
1414
import dotty.tools.dotc.util.Spans.Span
15+
import core.Symbols.TypeTests.given
1516

1617
import java.lang.Character.{isJavaIdentifierPart, isJavaIdentifierStart}
1718

@@ -44,7 +45,7 @@ object Scala3:
4445
else (span.start, span.end)
4546
val nameInSource = content.slice(start, end).mkString
4647
// for secondary constructors `this`
47-
desig match
48+
(desig: @unchecked) match // !!! dotty opaque exhaustivity problem
4849
case sym: Symbol =>
4950
if sym.isConstructor && nameInSource == nme.THISkw.toString then
5051
true
@@ -76,15 +77,15 @@ object Scala3:
7677

7778
given SemanticSymbolOps : AnyRef with
7879
extension (sym: SemanticSymbol)
79-
def name(using Context): Name = sym match
80+
def name(using Context): Name = (sym: @unchecked) match // !!! dotty opaque exhaustivity problem
8081
case s: Symbol => core.Symbols.name(s)
8182
case s: WildcardTypeSymbol => nme.WILDCARD
8283
case s: TermParamRefSymbol => s.name
8384
case s: TypeParamRefSymbol => s.name
8485
case s: RefinementSymbol => s.name
8586

8687
def symbolName(using builder: SemanticSymbolBuilder)(using Context): String =
87-
sym match
88+
(sym: @unchecked) match // !!! dotty opaque exhaustivity problem
8889
case s: Symbol => builder.symbolName(s)
8990
case s: FakeSymbol =>
9091
s.sname.getOrElse {
@@ -94,7 +95,7 @@ object Scala3:
9495
}
9596

9697
def symbolInfo(symkinds: Set[SymbolKind])(using LinkMode, TypeOps, SemanticSymbolBuilder, Context): SymbolInformation =
97-
sym match
98+
(sym: @unchecked) match // !!! dotty opaque exhaustivity problem
9899
case s: Symbol =>
99100
val kind = s.symbolKind(symkinds)
100101
val sname = sym.symbolName

compiler/src/dotty/tools/dotc/semanticdb/TypeOps.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import core.StdNames.tpnme
1212
import scala.util.chaining.scalaUtilChainingOps
1313

1414
import collection.mutable
15+
import core.Symbols.TypeTests.given
1516

1617
import dotty.tools.dotc.{semanticdb => s}
1718
import Scala3.{FakeSymbol, SemanticSymbol, WildcardTypeSymbol, TypeParamRefSymbol, TermParamRefSymbol, RefinementSymbol}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Symbols._, StdNames._, Trees._
1010
import TypeErasure.ErasedValueType, ValueClasses._
1111
import reporting._
1212
import NameKinds.SuperAccessorName
13+
import Symbols.TypeTests.given
1314

1415
object ElimErasedValueType {
1516
val name: String = "elimErasedValueType"

0 commit comments

Comments
 (0)