Skip to content

Commit

Permalink
Add TASTyInfo abstraction
Browse files Browse the repository at this point in the history
This abstraction makes it clearer which fields of the `CompilationUnitInfo`
are set when the symbol is loaded from TASTy. It also makes it trivial to
add new attributes without the need to change the `CompilationUnitInfo`
and possibly the `ClassSymbol` helper methods.
  • Loading branch information
nicolasstucki committed Nov 27, 2023
1 parent f0fc92a commit 759e518
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 52 deletions.
29 changes: 8 additions & 21 deletions compiler/src/dotty/tools/dotc/core/CompilationUnitInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,15 @@ import dotty.tools.tasty.TastyVersion
* @param associatedFile The source or class file from which this class or
* the class containing this symbol was generated,
* null if not applicable.
* @param tastyVersion The TASTy version (major, minor, experimental)
* @param explicitNulls This compilation unit has explicit nulls enabled?
* @param tastyInfo Information about the TASTy from which this class was loaded.
* None if not loaded from TASTy,
*/
class CompilationUnitInfo(
val associatedFile: AbstractFile,
val tastyVersion: Option[TastyVersion],
val explicitNulls: Boolean,
val captureChecked: Boolean,
val withPureFuns: Boolean,
) {

override def toString(): String =
s"CompilationUnitInfo($associatedFile, $tastyVersion, explicitNulls = $explicitNulls, captureChecked = $captureChecked, withPureFuns = $withPureFuns)"
}
case class CompilationUnitInfo(
associatedFile: AbstractFile,
tastyInfo: Option[TastyInfo],
)

object CompilationUnitInfo:
def apply(assocFile: AbstractFile | Null, explicitNulls: Boolean = false, captureChecked: Boolean = false, withPureFuns: Boolean = false): CompilationUnitInfo | Null =
def apply(assocFile: AbstractFile | Null): CompilationUnitInfo | Null =
if assocFile == null then null
else new CompilationUnitInfo(
assocFile,
tastyVersion = None,
explicitNulls = explicitNulls,
captureChecked = captureChecked,
withPureFuns = withPureFuns,
)
else new CompilationUnitInfo(assocFile, tastyInfo = None)
5 changes: 1 addition & 4 deletions compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,7 @@ class TastyLoader(val tastyFile: AbstractFile) extends SymbolLoader {
val attributes = unpickler.tastyAttributes
new CompilationUnitInfo(
tastyFile,
tastyVersion = Some(tastyVersion),
explicitNulls = attributes.explicitNulls,
captureChecked = attributes.captureChecked,
withPureFuns = attributes.withPureFuns,
tastyInfo = Some(TastyInfo(tastyVersion, attributes)),
)

def description(using Context): String = "TASTy file " + tastyFile.toString
Expand Down
21 changes: 3 additions & 18 deletions compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,26 +282,11 @@ object Symbols {
def compilationUnitInfo(using Context): CompilationUnitInfo | Null =
lastDenot.topLevelClass.compilationUnitInfo

/** The version of TASTy from which the symbol was loaded, None if not applicable. */
def tastyVersion(using Context): Option[TastyVersion] =
/** The info of the TASTy from which this symbol was loaded, None if not applicable. */
def tastyInfo(using Context): Option[TastyInfo] =
val compUnitInfo = compilationUnitInfo
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

/** If this class is capture checked */
def captureChecked(using Context): Boolean =
val compUnitInfo = compilationUnitInfo
compUnitInfo != null && compUnitInfo.captureChecked

/** If this class is uses pure functions */
def withPureFuns(using Context): Boolean =
val compUnitInfo = compilationUnitInfo
compUnitInfo != null && compUnitInfo.withPureFuns
else compUnitInfo.tastyInfo

/** The class file from which this class was generated, null if not applicable. */
final def binaryFile(using Context): AbstractFile | Null = {
Expand Down
11 changes: 11 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TastyInfo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dotty.tools.dotc.core

import dotty.tools.io.AbstractFile
import dotty.tools.tasty.TastyVersion

/** Information about the TASTy of a class symbol.
*
* @param version The TASTy version (major, minor, experimental)
* @param attributes Attributes of in the TASTy attributes section
*/
case class TastyInfo(version: TastyVersion, attributes: tasty.Attributes)
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,8 @@ trait Checking {
tree.op match {
case id @ Ident(name: Name) =>
def methCompiledBeforeDeprecation =
meth.tastyVersion match
case Some(version) => version.major == 28 && version.minor < 4 // compiled before 3.4
meth.tastyInfo match
case Some(info) => info.version.major == 28 && info.version.minor < 4 // compiled before 3.4
case _ => false // compiled with the current compiler
name.toTermName match {
case name: SimpleName
Expand Down
8 changes: 1 addition & 7 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,10 @@ class Namer { typer: Typer =>
case tree: TypeDef if tree.isClassDef =>
val flags = checkFlags(tree.mods.flags)
val name = checkNoConflict(tree.name, flags.is(Private), tree.span).asTypeName
val compilationUnitInfo = CompilationUnitInfo(
ctx.source.file,
explicitNulls = ctx.explicitNulls,
captureChecked = Feature.ccEnabled,
withPureFuns = Feature.pureFunsEnabled,
)
val cls =
createOrRefine[ClassSymbol](tree, name, flags, ctx.owner,
cls => adjustIfModule(new ClassCompleter(cls, tree)(ctx), tree),
newClassSymbol(ctx.owner, name, _, _, _, tree.nameSpan, compilationUnitInfo))
newClassSymbol(ctx.owner, name, _, _, _, tree.nameSpan, CompilationUnitInfo(ctx.source.file)))
cls.completer.asInstanceOf[ClassCompleter].init()
cls
case tree: MemberDef =>
Expand Down

0 comments on commit 759e518

Please sign in to comment.