From b00bb3007e8b25beae6d90f0306dda51b09d890c Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 22 Nov 2023 08:10:05 +0100 Subject: [PATCH] Set explicit nulls in CompilationUnitInfo symbols from source --- .../src/dotty/tools/dotc/core/CompilationUnitInfo.scala | 8 ++++---- compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala | 2 +- compiler/src/dotty/tools/dotc/core/Symbols.scala | 5 +++++ compiler/src/dotty/tools/dotc/typer/Namer.scala | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/CompilationUnitInfo.scala b/compiler/src/dotty/tools/dotc/core/CompilationUnitInfo.scala index 954aca953ab7..50b1d0bc5fcd 100644 --- a/compiler/src/dotty/tools/dotc/core/CompilationUnitInfo.scala +++ b/compiler/src/dotty/tools/dotc/core/CompilationUnitInfo.scala @@ -9,12 +9,12 @@ import dotty.tools.tasty.TastyVersion * the class containing this symbol was generated, * null if not applicable. * @param tastyVersion The TASTy version (major, minor, experimental) - * @param tastyExplicitNulls Was this compilation unit compiled with explicit nulls? + * @param explicitNulls This compilation unit has explicit nulls enabled? */ class CompilationUnitInfo( val associatedFile: AbstractFile, val tastyVersion: Option[TastyVersion], - val tastyExplicitNulls: Boolean + val explicitNulls: Boolean ) { override def toString(): String = @@ -22,10 +22,10 @@ class CompilationUnitInfo( } object CompilationUnitInfo: - def apply(assocFile: AbstractFile | Null): CompilationUnitInfo | Null = + def apply(assocFile: AbstractFile | Null, explicitNulls: Boolean = false): CompilationUnitInfo | Null = if assocFile == null then null else new CompilationUnitInfo( assocFile, tastyVersion = None, - tastyExplicitNulls = false // TODO track explicit nulls for current compilation units (not only TASTy) + explicitNulls = explicitNulls, ) diff --git a/compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala b/compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala index 4bc970915d68..a4a9b973dc62 100644 --- a/compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala +++ b/compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala @@ -433,7 +433,7 @@ class TastyLoader(val tastyFile: AbstractFile) extends SymbolLoader { new CompilationUnitInfo( tastyFile, tastyVersion = Some(tastyVersion), - tastyExplicitNulls = attributes.explicitNulls, + explicitNulls = attributes.explicitNulls, ) def description(using Context): String = "TASTy file " + tastyFile.toString diff --git a/compiler/src/dotty/tools/dotc/core/Symbols.scala b/compiler/src/dotty/tools/dotc/core/Symbols.scala index ccc65289f511..7a67e676ae1a 100644 --- a/compiler/src/dotty/tools/dotc/core/Symbols.scala +++ b/compiler/src/dotty/tools/dotc/core/Symbols.scala @@ -288,6 +288,11 @@ object Symbols { if compUnitInfo == null then None else compUnitInfo.tastyVersion + /** If this class has explicit nulls enabled */ + def explicitNulls(using Context): Boolean = + val compUnitInfo = compilationUnitInfo + compUnitInfo != null && compUnitInfo.explicitNulls + /** The class file from which this class was generated, null if not applicable. */ final def binaryFile(using Context): AbstractFile | Null = { val file = associatedFile diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 7735104bc8ee..357f0d7b41e3 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -250,7 +250,7 @@ class Namer { typer: Typer => val cls = createOrRefine[ClassSymbol](tree, name, flags, ctx.owner, cls => adjustIfModule(new ClassCompleter(cls, tree)(ctx), tree), - newClassSymbol(ctx.owner, name, _, _, _, tree.nameSpan, CompilationUnitInfo(ctx.source.file))) + newClassSymbol(ctx.owner, name, _, _, _, tree.nameSpan, CompilationUnitInfo(ctx.source.file, explicitNulls = ctx.explicitNulls))) cls.completer.asInstanceOf[ClassCompleter].init() cls case tree: MemberDef =>