Skip to content

Commit 759e518

Browse files
committed
Add TASTyInfo abstraction
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.
1 parent f0fc92a commit 759e518

File tree

6 files changed

+26
-52
lines changed

6 files changed

+26
-52
lines changed

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

+8-21
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,15 @@ import dotty.tools.tasty.TastyVersion
88
* @param associatedFile The source or class file from which this class or
99
* the class containing this symbol was generated,
1010
* null if not applicable.
11-
* @param tastyVersion The TASTy version (major, minor, experimental)
12-
* @param explicitNulls This compilation unit has explicit nulls enabled?
11+
* @param tastyInfo Information about the TASTy from which this class was loaded.
12+
* None if not loaded from TASTy,
1313
*/
14-
class CompilationUnitInfo(
15-
val associatedFile: AbstractFile,
16-
val tastyVersion: Option[TastyVersion],
17-
val explicitNulls: Boolean,
18-
val captureChecked: Boolean,
19-
val withPureFuns: Boolean,
20-
) {
21-
22-
override def toString(): String =
23-
s"CompilationUnitInfo($associatedFile, $tastyVersion, explicitNulls = $explicitNulls, captureChecked = $captureChecked, withPureFuns = $withPureFuns)"
24-
}
14+
case class CompilationUnitInfo(
15+
associatedFile: AbstractFile,
16+
tastyInfo: Option[TastyInfo],
17+
)
2518

2619
object CompilationUnitInfo:
27-
def apply(assocFile: AbstractFile | Null, explicitNulls: Boolean = false, captureChecked: Boolean = false, withPureFuns: Boolean = false): CompilationUnitInfo | Null =
20+
def apply(assocFile: AbstractFile | Null): CompilationUnitInfo | Null =
2821
if assocFile == null then null
29-
else new CompilationUnitInfo(
30-
assocFile,
31-
tastyVersion = None,
32-
explicitNulls = explicitNulls,
33-
captureChecked = captureChecked,
34-
withPureFuns = withPureFuns,
35-
)
22+
else new CompilationUnitInfo(assocFile, tastyInfo = None)

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,7 @@ class TastyLoader(val tastyFile: AbstractFile) extends SymbolLoader {
432432
val attributes = unpickler.tastyAttributes
433433
new CompilationUnitInfo(
434434
tastyFile,
435-
tastyVersion = Some(tastyVersion),
436-
explicitNulls = attributes.explicitNulls,
437-
captureChecked = attributes.captureChecked,
438-
withPureFuns = attributes.withPureFuns,
435+
tastyInfo = Some(TastyInfo(tastyVersion, attributes)),
439436
)
440437

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

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

+3-18
Original file line numberDiff line numberDiff line change
@@ -282,26 +282,11 @@ object Symbols {
282282
def compilationUnitInfo(using Context): CompilationUnitInfo | Null =
283283
lastDenot.topLevelClass.compilationUnitInfo
284284

285-
/** The version of TASTy from which the symbol was loaded, None if not applicable. */
286-
def tastyVersion(using Context): Option[TastyVersion] =
285+
/** The info of the TASTy from which this symbol was loaded, None if not applicable. */
286+
def tastyInfo(using Context): Option[TastyInfo] =
287287
val compUnitInfo = compilationUnitInfo
288288
if compUnitInfo == null then None
289-
else compUnitInfo.tastyVersion
290-
291-
/** If this class has explicit nulls enabled */
292-
def explicitNulls(using Context): Boolean =
293-
val compUnitInfo = compilationUnitInfo
294-
compUnitInfo != null && compUnitInfo.explicitNulls
295-
296-
/** If this class is capture checked */
297-
def captureChecked(using Context): Boolean =
298-
val compUnitInfo = compilationUnitInfo
299-
compUnitInfo != null && compUnitInfo.captureChecked
300-
301-
/** If this class is uses pure functions */
302-
def withPureFuns(using Context): Boolean =
303-
val compUnitInfo = compilationUnitInfo
304-
compUnitInfo != null && compUnitInfo.withPureFuns
289+
else compUnitInfo.tastyInfo
305290

306291
/** The class file from which this class was generated, null if not applicable. */
307292
final def binaryFile(using Context): AbstractFile | Null = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dotty.tools.dotc.core
2+
3+
import dotty.tools.io.AbstractFile
4+
import dotty.tools.tasty.TastyVersion
5+
6+
/** Information about the TASTy of a class symbol.
7+
*
8+
* @param version The TASTy version (major, minor, experimental)
9+
* @param attributes Attributes of in the TASTy attributes section
10+
*/
11+
case class TastyInfo(version: TastyVersion, attributes: tasty.Attributes)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1080,8 +1080,8 @@ trait Checking {
10801080
tree.op match {
10811081
case id @ Ident(name: Name) =>
10821082
def methCompiledBeforeDeprecation =
1083-
meth.tastyVersion match
1084-
case Some(version) => version.major == 28 && version.minor < 4 // compiled before 3.4
1083+
meth.tastyInfo match
1084+
case Some(info) => info.version.major == 28 && info.version.minor < 4 // compiled before 3.4
10851085
case _ => false // compiled with the current compiler
10861086
name.toTermName match {
10871087
case name: SimpleName

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,10 @@ class Namer { typer: Typer =>
247247
case tree: TypeDef if tree.isClassDef =>
248248
val flags = checkFlags(tree.mods.flags)
249249
val name = checkNoConflict(tree.name, flags.is(Private), tree.span).asTypeName
250-
val compilationUnitInfo = CompilationUnitInfo(
251-
ctx.source.file,
252-
explicitNulls = ctx.explicitNulls,
253-
captureChecked = Feature.ccEnabled,
254-
withPureFuns = Feature.pureFunsEnabled,
255-
)
256250
val cls =
257251
createOrRefine[ClassSymbol](tree, name, flags, ctx.owner,
258252
cls => adjustIfModule(new ClassCompleter(cls, tree)(ctx), tree),
259-
newClassSymbol(ctx.owner, name, _, _, _, tree.nameSpan, compilationUnitInfo))
253+
newClassSymbol(ctx.owner, name, _, _, _, tree.nameSpan, CompilationUnitInfo(ctx.source.file)))
260254
cls.completer.asInstanceOf[ClassCompleter].init()
261255
cls
262256
case tree: MemberDef =>

0 commit comments

Comments
 (0)