Skip to content

Commit 3abb296

Browse files
committed
Set source field of context directly
Inlined sources might not come from a compilation units that's currently compiled.
1 parent c7f2f05 commit 3abb296

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ object Contexts {
178178
_typeComparer
179179
}
180180

181+
/** The current source file */
182+
private[this] var _source: SourceFile = _
183+
protected def source_=(source: SourceFile): Unit = _source = source
184+
def source: SourceFile = _source
185+
181186
/** A map in which more contextual properties can be stored
182187
* Typically used for attributes that are read and written only in special situations.
183188
*/
@@ -418,12 +423,6 @@ object Contexts {
418423
ctx.fresh.setImportInfo(new ImportInfo(implicit ctx => sym, imp.selectors, impNameOpt))
419424
}
420425

421-
/** The current source file; will be derived from current
422-
* compilation unit.
423-
*/
424-
def source: SourceFile =
425-
if (compilationUnit == null) NoSource else compilationUnit.source
426-
427426
/** Does current phase use an erased types interpretation? */
428427
def erasedTypes: Boolean = phase.erasedTypes
429428

@@ -453,6 +452,9 @@ object Contexts {
453452
final def withOwner(owner: Symbol): Context =
454453
if (owner ne this.owner) fresh.setOwner(owner) else this
455454

455+
final def withSource(source: SourceFile): Context =
456+
if (source ne this.source) fresh.setSource(source) else this
457+
456458
final def withProperty[T](key: Key[T], value: Option[T]): Context =
457459
if (property(key) == value) this
458460
else value match {
@@ -521,16 +523,21 @@ object Contexts {
521523
def setGadt(gadt: GADTMap): this.type = { this.gadt = gadt; this }
522524
def setFreshGADTBounds: this.type = setGadt(gadt.fresh)
523525
def setSearchHistory(searchHistory: SearchHistory): this.type = { this.searchHistory = searchHistory; this }
526+
def setSource(source: SourceFile): this.type = { this.source = source; this }
524527
def setTypeComparerFn(tcfn: Context => TypeComparer): this.type = { this.typeComparer = tcfn(this); this }
525528
private def setMoreProperties(moreProperties: Map[Key[Any], Any]): this.type = { this.moreProperties = moreProperties; this }
526529
private def setStore(store: Store): this.type = { this.store = store; this }
527530
def setImplicits(implicits: ContextualImplicits): this.type = { this.implicitsCache = implicits; this }
528531

532+
def setCompilationUnit(compilationUnit: CompilationUnit): this.type = {
533+
setSource(compilationUnit.source)
534+
updateStore(compilationUnitLoc, compilationUnit)
535+
}
536+
529537
def setCompilerCallback(callback: CompilerCallback): this.type = updateStore(compilerCallbackLoc, callback)
530538
def setSbtCallback(callback: AnalysisCallback): this.type = updateStore(sbtCallbackLoc, callback)
531539
def setPrinterFn(printer: Context => Printer): this.type = updateStore(printerFnLoc, printer)
532540
def setSettings(settingsState: SettingsState): this.type = updateStore(settingsStateLoc, settingsState)
533-
def setCompilationUnit(compilationUnit: CompilationUnit): this.type = updateStore(compilationUnitLoc, compilationUnit)
534541
def setRun(run: Run): this.type = updateStore(runLoc, run)
535542
def setProfiler(profiler: Profiler): this.type = updateStore(profilerLoc, profiler)
536543
def setFreshNames(freshNames: FreshNameCreator): this.type = updateStore(freshNamesLoc, freshNames)
@@ -592,6 +599,7 @@ object Contexts {
592599
tree = untpd.EmptyTree
593600
typeAssigner = TypeAssigner
594601
moreProperties = Map.empty
602+
source = NoSource
595603
store = initialStore.updated(settingsStateLoc, settingsGroup.defaultState)
596604
typeComparer = new TypeComparer(this)
597605
searchHistory = new SearchRoot
@@ -659,11 +667,12 @@ object Contexts {
659667
// Symbols state
660668

661669
/** Counters for unique ids */
662-
private[core] var _nextSymId: Int = 0
670+
private[this] var _nextSymId: Int = 0
663671
def nextSymId: Int = { _nextSymId += 1; _nextSymId }
664672

665673
private[core] var nextTreeIdBySource = new mutable.HashMap[SourceFile, Int]
666674
private[core] var sourceOfChunk = mutable.ArrayBuffer[SourceFile](NoSource)
675+
// first element reserved for GlobalTreeIds
667676

668677
/** Sources that were loaded */
669678
val sources: mutable.HashMap[AbstractFile, SourceFile] = new mutable.HashMap[AbstractFile, SourceFile]

0 commit comments

Comments
 (0)