Skip to content

Commit 411fad2

Browse files
committed
Add CompilationUnitInfo to ClassSymbol
Use it to store the `associatedFile` allow for later expansion.
1 parent 5bb6f0d commit 411fad2

File tree

11 files changed

+80
-48
lines changed

11 files changed

+80
-48
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dotty.tools.dotc.core
2+
3+
import dotty.tools.io.AbstractFile
4+
5+
/** Information about the compilation unit of a class symbol.
6+
*
7+
* @param associatedFile The source or class file from which this class or
8+
* the class containing this symbol was generated,
9+
* null if not applicable.
10+
*/
11+
class CompilationUnitInfo(
12+
val associatedFile: AbstractFile,
13+
) {
14+
override def toString(): String =
15+
s"CompilationUnitInfo($associatedFile)"
16+
}
17+
18+
object CompilationUnitInfo:
19+
def apply(assocFile: AbstractFile | Null): CompilationUnitInfo | Null =
20+
if assocFile == null then null
21+
else new CompilationUnitInfo(assocFile)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ class Definitions {
14091409
),
14101410
privateWithin = patch.privateWithin,
14111411
coord = denot.symbol.coord,
1412-
assocFile = denot.symbol.associatedFile
1412+
compUnitInfo = denot.symbol.compilationUnitInfo
14131413
)
14141414

14151415
def makeNonClassSymbol(patch: Symbol) =

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,13 @@ object Denotations {
298298
name: Name,
299299
site: Denotation = NoDenotation,
300300
args: List[Type] = Nil,
301-
source: AbstractFile | Null = null,
302301
generateStubs: Boolean = true)
303302
(p: Symbol => Boolean)
304303
(using Context): Symbol =
305304
disambiguate(p) match {
306305
case m @ MissingRef(ownerd, name) if generateStubs =>
307306
if ctx.settings.YdebugMissingRefs.value then m.ex.printStackTrace()
308-
newStubSymbol(ownerd.symbol, name, source)
307+
newStubSymbol(ownerd.symbol, name)
309308
case NoDenotation | _: NoQualifyingRef | _: MissingRef =>
310309
def argStr = if (args.isEmpty) "" else i" matching ($args%, %)"
311310
val msg =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ object NamerOps:
141141
ConstructorCompanionFlags, ConstructorCompanionFlags,
142142
constructorCompanionCompleter(cls),
143143
coord = cls.coord,
144-
assocFile = cls.assocFile)
144+
compUnitInfo = cls.compUnitInfo)
145145
companion.moduleClass.registerCompanion(cls)
146146
cls.registerCompanion(companion.moduleClass)
147147
companion
@@ -150,7 +150,7 @@ object NamerOps:
150150
newSymbol(tsym.owner, tsym.name.toTermName,
151151
ConstructorCompanionFlags | StableRealizable | Method, ExprType(prefix.select(proxy)), coord = tsym.coord)
152152

153-
/** Add all necesssary constructor proxy symbols for members of class `cls`. This means:
153+
/** Add all necessary constructor proxy symbols for members of class `cls`. This means:
154154
*
155155
* - if a member is a class, or type alias, that needs a constructor companion, add one,
156156
* provided no member with the same name exists.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ object SymDenotations {
25882588
for (sym <- scope.toList.iterator)
25892589
// We need to be careful to not force the denotation of `sym` here,
25902590
// otherwise it will be brought forward to the current run.
2591-
if (sym.defRunId != ctx.runId && sym.isClass && sym.asClass.assocFile == file)
2591+
if (sym.defRunId != ctx.runId && sym.isClass && sym.asClass.compUnitInfo != null && sym.asClass.compUnitInfo.nn.associatedFile == file)
25922592
scope.unlink(sym, sym.lastKnownDenotation.name)
25932593
}
25942594
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object SymbolLoaders {
5252
def enterClass(
5353
owner: Symbol, name: PreName, completer: SymbolLoader,
5454
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope)(using Context): Symbol = {
55-
val cls = newClassSymbol(owner, name.toTypeName.unmangleClassName.decode, flags, completer, assocFile = completer.sourceFileOrNull)
55+
val cls = newClassSymbol(owner, name.toTypeName.unmangleClassName.decode, flags, completer, compUnitInfo = completer.compilationUnitInfo)
5656
enterNew(owner, cls, completer, scope)
5757
}
5858

@@ -64,7 +64,7 @@ object SymbolLoaders {
6464
val module = newModuleSymbol(
6565
owner, name.toTermName.decode, modFlags, clsFlags,
6666
(module, _) => completer.proxy.withDecls(newScope).withSourceModule(module),
67-
assocFile = completer.sourceFileOrNull)
67+
compUnitInfo = completer.compilationUnitInfo)
6868
enterNew(owner, module, completer, scope)
6969
enterNew(owner, module.moduleClass, completer, scope)
7070
}
@@ -213,7 +213,7 @@ object SymbolLoaders {
213213
/** Load contents of a package
214214
*/
215215
class PackageLoader(_sourceModule: TermSymbol, classPath: ClassPath) extends SymbolLoader {
216-
override def sourceFileOrNull: AbstractFile | Null = null
216+
def compilationUnitInfo: CompilationUnitInfo | Null = null
217217
override def sourceModule(using Context): TermSymbol = _sourceModule
218218
def description(using Context): String = "package loader " + sourceModule.fullName
219219

@@ -317,7 +317,7 @@ abstract class SymbolLoader extends LazyType { self =>
317317
/** Load source or class file for `root`, return */
318318
def doComplete(root: SymDenotation)(using Context): Unit
319319

320-
def sourceFileOrNull: AbstractFile | Null
320+
def compilationUnitInfo: CompilationUnitInfo | Null
321321

322322
/** Description of the resource (ClassPath, AbstractFile)
323323
* being processed by this loader
@@ -328,7 +328,7 @@ abstract class SymbolLoader extends LazyType { self =>
328328
* but provides fresh slots for scope/sourceModule/moduleClass
329329
*/
330330
def proxy: SymbolLoader = new SymbolLoader {
331-
export self.{doComplete, sourceFileOrNull}
331+
export self.{doComplete, compilationUnitInfo}
332332
def description(using Context): String = s"proxy to ${self.description}"
333333
}
334334

@@ -405,7 +405,8 @@ abstract class SymbolLoader extends LazyType { self =>
405405

406406
class ClassfileLoader(val classfile: AbstractFile) extends SymbolLoader {
407407

408-
override def sourceFileOrNull: AbstractFile | Null = classfile
408+
def compilationUnitInfo: CompilationUnitInfo | Null = CompilationUnitInfo(classfile)
409+
409410

410411
def description(using Context): String = "class file " + classfile.toString
411412

@@ -417,7 +418,7 @@ class ClassfileLoader(val classfile: AbstractFile) extends SymbolLoader {
417418

418419
class TastyLoader(val tastyFile: AbstractFile) extends SymbolLoader {
419420

420-
override def sourceFileOrNull: AbstractFile | Null = tastyFile
421+
def compilationUnitInfo: CompilationUnitInfo | Null = CompilationUnitInfo(tastyFile)
421422

422423
def description(using Context): String = "TASTy file " + tastyFile.toString
423424

@@ -460,15 +461,15 @@ class TastyLoader(val tastyFile: AbstractFile) extends SymbolLoader {
460461

461462
class SourcefileLoader(val srcfile: AbstractFile) extends SymbolLoader {
462463
def description(using Context): String = "source file " + srcfile.toString
463-
override def sourceFileOrNull: AbstractFile | Null = srcfile
464+
def compilationUnitInfo: CompilationUnitInfo | Null = CompilationUnitInfo(srcfile)
464465
def doComplete(root: SymDenotation)(using Context): Unit =
465466
ctx.run.nn.lateCompile(srcfile, typeCheck = ctx.settings.YretainTrees.value)
466467
}
467468

468469
/** A NoCompleter which is also a SymbolLoader. */
469470
class NoLoader extends SymbolLoader with NoCompleter {
470471
def description(using Context): String = "NoLoader"
471-
override def sourceFileOrNull: AbstractFile | Null = null
472+
def compilationUnitInfo: CompilationUnitInfo | Null = null
472473
override def complete(root: SymDenotation)(using Context): Unit =
473474
super[NoCompleter].complete(root)
474475
def doComplete(root: SymDenotation)(using Context): Unit =

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

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,21 @@ object Symbols {
265265

266266
/** The source or class file from which this class or
267267
* the class containing this symbol was generated, null if not applicable.
268-
* Note that this the returned classfile might be the top-level class
268+
* Note that the returned classfile might be from the top-level class
269269
* containing this symbol instead of the directly enclosing class.
270-
* Overridden in ClassSymbol
271270
*/
272271
def associatedFile(using Context): AbstractFile | Null =
273-
lastDenot.topLevelClass.associatedFile
272+
val compUnitInfo = compilationUnitInfo
273+
if compUnitInfo == null then (null: AbstractFile | Null)
274+
else compUnitInfo.associatedFile
275+
276+
/** The compilation unit info (associated file, tasty versions, ...).
277+
* Note that the returned CompilationUnitInfo might be from the top-level class
278+
* containing this symbol instead of the directly enclosing class.
279+
* Overridden in ClassSymbol
280+
*/
281+
def compilationUnitInfo(using Context): CompilationUnitInfo | Null =
282+
lastDenot.topLevelClass.compilationUnitInfo
274283

275284
/** The class file from which this class was generated, null if not applicable. */
276285
final def binaryFile(using Context): AbstractFile | Null = {
@@ -353,7 +362,7 @@ object Symbols {
353362
def paramRef(using Context): TypeRef = denot.typeRef
354363

355364
/** Copy a symbol, overriding selective fields.
356-
* Note that `coord` and `associatedFile` will be set from the fields in `owner`, not
365+
* Note that `coord` and `compilationUnitInfo` will be set from the fields in `owner`, not
357366
* the fields in `sym`. */
358367
def copy(using Context)(
359368
owner: Symbol = this.owner,
@@ -362,13 +371,14 @@ object Symbols {
362371
info: Type = this.info,
363372
privateWithin: Symbol = this.privateWithin,
364373
coord: Coord = NoCoord, // Can be `= owner.coord` once we bootstrap
365-
associatedFile: AbstractFile | Null = null // Can be `= owner.associatedFile` once we bootstrap
374+
compUnitInfo: CompilationUnitInfo | Null = null // Can be `= owner.associatedFile` once we bootstrap
366375
): Symbol = {
367376
val coord1 = if (coord == NoCoord) owner.coord else coord
368-
val associatedFile1 = if (associatedFile == null) owner.associatedFile else associatedFile
377+
val compilationUnitInfo1 = if (compilationUnitInfo == null) owner.compilationUnitInfo else compilationUnitInfo
378+
369379

370380
if isClass then
371-
newClassSymbol(owner, name.asTypeName, flags, _ => info, privateWithin, coord1, associatedFile1)
381+
newClassSymbol(owner, name.asTypeName, flags, _ => info, privateWithin, coord1, compilationUnitInfo1)
372382
else
373383
newSymbol(owner, name, flags, info, privateWithin, coord1)
374384
}
@@ -396,7 +406,7 @@ object Symbols {
396406
type TermSymbol = Symbol { type ThisName = TermName }
397407
type TypeSymbol = Symbol { type ThisName = TypeName }
398408

399-
class ClassSymbol private[Symbols] (coord: Coord, val assocFile: AbstractFile | Null, id: Int, nestingLevel: Int)
409+
class ClassSymbol private[Symbols] (coord: Coord, val compUnitInfo: CompilationUnitInfo | Null, id: Int, nestingLevel: Int)
400410
extends Symbol(coord, id, nestingLevel) {
401411

402412
type ThisName = TypeName
@@ -456,9 +466,9 @@ object Symbols {
456466
}
457467

458468
/** The source or class file from which this class was generated, null if not applicable. */
459-
override def associatedFile(using Context): AbstractFile | Null =
460-
if assocFile != null || this.is(Package) || this.owner.is(Package) then assocFile
461-
else super.associatedFile
469+
override def compilationUnitInfo(using Context): CompilationUnitInfo | Null =
470+
if compUnitInfo != null || this.is(Package) || this.owner.is(Package) then compUnitInfo
471+
else super.compilationUnitInfo
462472

463473
private var mySource: SourceFile = NoSource
464474

@@ -488,7 +498,7 @@ object Symbols {
488498
}
489499

490500
@sharable object NoSymbol extends Symbol(NoCoord, 0, 0) {
491-
override def associatedFile(using Context): AbstractFile | Null = NoSource.file
501+
override def compilationUnitInfo(using Context): CompilationUnitInfo | Null = CompilationUnitInfo(NoSource.file)
492502
override def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = NoDenotation
493503
}
494504

@@ -537,9 +547,9 @@ object Symbols {
537547
infoFn: ClassSymbol => Type,
538548
privateWithin: Symbol = NoSymbol,
539549
coord: Coord = NoCoord,
540-
assocFile: AbstractFile | Null = null)(using Context): ClassSymbol
550+
compUnitInfo: CompilationUnitInfo | Null = null)(using Context): ClassSymbol
541551
= {
542-
val cls = new ClassSymbol(coord, assocFile, ctx.base.nextSymId, ctx.nestingLevel)
552+
val cls = new ClassSymbol(coord, compUnitInfo, ctx.base.nextSymId, ctx.nestingLevel)
543553
val denot = SymDenotation(cls, owner, name, flags, infoFn(cls), privateWithin)
544554
cls.denot = denot
545555
cls
@@ -555,11 +565,11 @@ object Symbols {
555565
selfInfo: Type = NoType,
556566
privateWithin: Symbol = NoSymbol,
557567
coord: Coord = NoCoord,
558-
assocFile: AbstractFile | Null = null)(using Context): ClassSymbol =
568+
compUnitInfo: CompilationUnitInfo | Null = null)(using Context): ClassSymbol =
559569
newClassSymbol(
560570
owner, name, flags,
561571
ClassInfo(owner.thisType, _, parents, decls, selfInfo),
562-
privateWithin, coord, assocFile)
572+
privateWithin, coord, compUnitInfo)
563573

564574
/** Same as `newCompleteClassSymbol` except that `parents` can be a list of arbitrary
565575
* types which get normalized into type refs and parameter bindings.
@@ -572,15 +582,15 @@ object Symbols {
572582
selfInfo: Type = NoType,
573583
privateWithin: Symbol = NoSymbol,
574584
coord: Coord = NoCoord,
575-
assocFile: AbstractFile | Null = null)(using Context): ClassSymbol = {
585+
compUnitInfo: CompilationUnitInfo | Null = null)(using Context): ClassSymbol = {
576586
def completer = new LazyType {
577587
def complete(denot: SymDenotation)(using Context): Unit = {
578588
val cls = denot.asClass.classSymbol
579589
val decls = newScope
580590
denot.info = ClassInfo(owner.thisType, cls, parentTypes.map(_.dealias), decls, selfInfo)
581591
}
582592
}
583-
newClassSymbol(owner, name, flags, completer, privateWithin, coord, assocFile)
593+
newClassSymbol(owner, name, flags, completer, privateWithin, coord, compUnitInfo)
584594
}
585595

586596
def newRefinedClassSymbol(coord: Coord = NoCoord)(using Context): ClassSymbol =
@@ -598,15 +608,15 @@ object Symbols {
598608
infoFn: (TermSymbol, ClassSymbol) => Type, // typically a ModuleClassCompleterWithDecls
599609
privateWithin: Symbol = NoSymbol,
600610
coord: Coord = NoCoord,
601-
assocFile: AbstractFile | Null = null)(using Context): TermSymbol
611+
compUnitInfo: CompilationUnitInfo | Null = null)(using Context): TermSymbol
602612
= {
603613
val base = owner.thisType
604614
val modclsFlags = clsFlags | ModuleClassCreationFlags
605615
val modclsName = name.toTypeName.adjustIfModuleClass(modclsFlags)
606616
val module = newSymbol(
607617
owner, name, modFlags | ModuleValCreationFlags, NoCompleter, privateWithin, coord)
608618
val modcls = newClassSymbol(
609-
owner, modclsName, modclsFlags, infoFn(module, _), privateWithin, coord, assocFile)
619+
owner, modclsName, modclsFlags, infoFn(module, _), privateWithin, coord, compUnitInfo)
610620
module.info =
611621
if (modcls.isCompleted) TypeRef(owner.thisType, modcls)
612622
else new ModuleCompleter(modcls)
@@ -627,12 +637,12 @@ object Symbols {
627637
decls: Scope,
628638
privateWithin: Symbol = NoSymbol,
629639
coord: Coord = NoCoord,
630-
assocFile: AbstractFile | Null = null)(using Context): TermSymbol =
640+
compUnitInfo: CompilationUnitInfo | Null = null)(using Context): TermSymbol =
631641
newModuleSymbol(
632642
owner, name, modFlags, clsFlags,
633643
(module, modcls) => ClassInfo(
634644
owner.thisType, modcls, parents, decls, TermRef(owner.thisType, module)),
635-
privateWithin, coord, assocFile)
645+
privateWithin, coord, compUnitInfo)
636646

637647
/** Same as `newCompleteModuleSymbol` except that `parents` can be a list of arbitrary
638648
* types which get normalized into type refs and parameter bindings.
@@ -646,7 +656,7 @@ object Symbols {
646656
decls: Scope,
647657
privateWithin: Symbol = NoSymbol,
648658
coord: Coord = NoCoord,
649-
assocFile: AbstractFile | Null = null)(using Context): TermSymbol = {
659+
compUnitInfo: CompilationUnitInfo | Null = null)(using Context): TermSymbol = {
650660
def completer(module: Symbol) = new LazyType {
651661
def complete(denot: SymDenotation)(using Context): Unit = {
652662
val cls = denot.asClass.classSymbol
@@ -657,7 +667,7 @@ object Symbols {
657667
newModuleSymbol(
658668
owner, name, modFlags, clsFlags,
659669
(module, modcls) => completer(module),
660-
privateWithin, coord, assocFile)
670+
privateWithin, coord, compUnitInfo)
661671
}
662672

663673
/** Create a package symbol with associated package class
@@ -697,17 +707,17 @@ object Symbols {
697707
/** Create a stub symbol that will issue a missing reference error
698708
* when attempted to be completed.
699709
*/
700-
def newStubSymbol(owner: Symbol, name: Name, file: AbstractFile | Null = null)(using Context): Symbol = {
710+
def newStubSymbol(owner: Symbol, name: Name, compUnitInfo: CompilationUnitInfo | Null = null)(using Context): Symbol = {
701711
def stubCompleter = new StubInfo()
702712
val normalizedOwner = if (owner.is(ModuleVal)) owner.moduleClass else owner
703-
typr.println(s"creating stub for ${name.show}, owner = ${normalizedOwner.denot.debugString}, file = $file")
713+
typr.println(s"creating stub for ${name.show}, owner = ${normalizedOwner.denot.debugString}, compilation unit = $compUnitInfo")
704714
typr.println(s"decls = ${normalizedOwner.unforcedDecls.toList.map(_.debugString).mkString("\n ")}") // !!! DEBUG
705715
//if (base.settings.debug.value) throw new Error()
706716
val stub = name match {
707717
case name: TermName =>
708-
newModuleSymbol(normalizedOwner, name, EmptyFlags, EmptyFlags, stubCompleter, assocFile = file)
718+
newModuleSymbol(normalizedOwner, name, EmptyFlags, EmptyFlags, stubCompleter, compUnitInfo = compUnitInfo)
709719
case name: TypeName =>
710-
newClassSymbol(normalizedOwner, name, EmptyFlags, stubCompleter, assocFile = file)
720+
newClassSymbol(normalizedOwner, name, EmptyFlags, stubCompleter, compUnitInfo = compUnitInfo)
711721
}
712722
stub
713723
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
426426
if (slowSearch(name).exists)
427427
System.err.println(i"**** slow search found: ${slowSearch(name)}")
428428
if (ctx.settings.YdebugMissingRefs.value) Thread.dumpStack()
429-
newStubSymbol(owner, name, source)
429+
newStubSymbol(owner, name, CompilationUnitInfo(source))
430430
}
431431
}
432432
}

compiler/src/dotty/tools/dotc/transform/sjs/JUnitBootstrappers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class JUnitBootstrappers extends MiniPhase {
156156
val moduleSym = newCompleteModuleSymbol(owner, bootstrapperName,
157157
Synthetic, Synthetic,
158158
List(defn.ObjectType, junitdefn.BootstrapperType), newScope,
159-
coord = testClass.span, assocFile = testClass.assocFile).entered
159+
coord = testClass.span, compUnitInfo = testClass.compUnitInfo).entered
160160
val classSym = moduleSym.moduleClass.asClass
161161

162162
val constr = genConstructor(classSym)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class Namer { typer: Typer =>
250250
val cls =
251251
createOrRefine[ClassSymbol](tree, name, flags, ctx.owner,
252252
cls => adjustIfModule(new ClassCompleter(cls, tree)(ctx), tree),
253-
newClassSymbol(ctx.owner, name, _, _, _, tree.nameSpan, ctx.source.file))
253+
newClassSymbol(ctx.owner, name, _, _, _, tree.nameSpan, CompilationUnitInfo(ctx.source.file)))
254254
cls.completer.asInstanceOf[ClassCompleter].init()
255255
cls
256256
case tree: MemberDef =>

0 commit comments

Comments
 (0)