Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capture checking attributes to TASTy #19033

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,6 @@ class Definitions {
@tu lazy val UncheckedVarianceAnnot: ClassSymbol = requiredClass("scala.annotation.unchecked.uncheckedVariance")
@tu lazy val UncheckedCapturesAnnot: ClassSymbol = requiredClass("scala.annotation.unchecked.uncheckedCaptures")
@tu lazy val VolatileAnnot: ClassSymbol = requiredClass("scala.volatile")
@tu lazy val WithPureFunsAnnot: ClassSymbol = requiredClass("scala.annotation.internal.WithPureFuns")
@tu lazy val CaptureCheckedAnnot: ClassSymbol = requiredClass("scala.annotation.internal.CaptureChecked")
@tu lazy val BeanGetterMetaAnnot: ClassSymbol = requiredClass("scala.annotation.meta.beanGetter")
@tu lazy val BeanSetterMetaAnnot: ClassSymbol = requiredClass("scala.annotation.meta.beanSetter")
@tu lazy val FieldMetaAnnot: ClassSymbol = requiredClass("scala.annotation.meta.field")
Expand Down Expand Up @@ -2004,7 +2002,7 @@ class Definitions {
@tu lazy val ccExperimental: Set[Symbol] = Set(
CapsModule, CapsModule.moduleClass, PureClass,
CapabilityAnnot, RequiresCapabilityAnnot,
RetainsAnnot, RetainsByNameAnnot, WithPureFunsAnnot)
RetainsAnnot, RetainsByNameAnnot)

// ----- primitive value class machinery ------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/core/tasty/Attributes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ class Attributes private[tasty](
) {
def scala2StandardLibrary: Boolean = booleanTags(SCALA2STANDARDLIBRARYattr)
def explicitNulls: Boolean = booleanTags(EXPLICITNULLSattr)
def captureChecked: Boolean = booleanTags(CAPTURECHECKEDattr)
def withPureFuns: Boolean = booleanTags(WITHPUREFUNSattr)
}

object Attributes:
def apply(
scala2StandardLibrary: Boolean,
explicitNulls: Boolean,
captureChecked: Boolean,
withPureFuns: Boolean,
): Attributes =
val booleanTags = BitSet.newBuilder
if scala2StandardLibrary then booleanTags += SCALA2STANDARDLIBRARYattr
if explicitNulls then booleanTags += EXPLICITNULLSattr
if captureChecked then booleanTags += CAPTURECHECKEDattr
if withPureFuns then booleanTags += WITHPUREFUNSattr
new Attributes(booleanTags.result())
end apply

Expand Down
15 changes: 4 additions & 11 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ class TreeUnpickler(reader: TastyReader,
/** The root owner tree. See `OwnerTree` class definition. Set by `enterTopLevel`. */
private var ownerTree: OwnerTree = uninitialized

/** Was unpickled class compiled with pureFunctions? */
private var withPureFuns: Boolean = false

/** Was unpickled class compiled with capture checks? */
private var withCaptureChecks: Boolean = false
private val withCaptureChecks: Boolean =
attributeUnpicklerOpt.exists(_.attributes.captureChecked)

private val unpicklingScala2Library =
attributeUnpicklerOpt.exists(_.attributes.scala2StandardLibrary)
Expand Down Expand Up @@ -655,13 +653,8 @@ class TreeUnpickler(reader: TastyReader,
}
registerSym(start, sym)
if (isClass) {
if sym.owner.is(Package) then
if annots.exists(_.hasSymbol(defn.CaptureCheckedAnnot)) then
sym.setFlag(CaptureChecked)
withCaptureChecks = true
withPureFuns = true
else if annots.exists(_.hasSymbol(defn.WithPureFunsAnnot)) then
withPureFuns = true
if sym.owner.is(Package) && withCaptureChecks then
sym.setFlag(CaptureChecked)
sym.completer.withDecls(newScope)
forkAt(templateStart).indexTemplateParams()(using localContext(sym))
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/Pickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Contexts.*
import Decorators.*
import tasty.*
import config.Printers.{noPrinter, pickling}
import config.Feature
import java.io.PrintStream
import Periods.*
import Phases.*
Expand Down Expand Up @@ -111,6 +112,8 @@ class Pickler extends Phase {
val attributes = Attributes(
scala2StandardLibrary = ctx.settings.YcompileScala2Library.value,
explicitNulls = ctx.settings.YexplicitNulls.value,
captureChecked = Feature.ccEnabled,
withPureFuns = Feature.pureFunsEnabled,
)
AttributePickler.pickleAttributes(attributes, pickler, scratch.attributeBuffer)

Expand Down
5 changes: 0 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,6 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
val reference = ctx.settings.sourceroot.value
val relativePath = util.SourceFile.relativePath(ctx.compilationUnit.source, reference)
sym.addAnnotation(Annotation.makeSourceFile(relativePath, tree.span))
if sym != defn.WithPureFunsAnnot && sym != defn.CaptureCheckedAnnot then
if Feature.ccEnabled then
sym.addAnnotation(Annotation(defn.CaptureCheckedAnnot, tree.span))
else if Feature.pureFunsEnabled then
sym.addAnnotation(Annotation(defn.WithPureFunsAnnot, tree.span))
else
if !sym.is(Param) && !sym.owner.isOneOf(AbstractOrTrait) then
Checking.checkGoodBounds(tree.symbol)
Expand Down
6 changes: 6 additions & 0 deletions tasty/src/dotty/tools/tasty/TastyFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ Standard Section: "Attributes" Attribute*
```none
Attribute = SCALA2STANDARDLIBRARYattr
EXPLICITNULLSattr
CAPTURECHECKEDattr
WITHPUREFUNSattr
```

**************************************************************************************/
Expand Down Expand Up @@ -611,6 +613,8 @@ object TastyFormat {

final val SCALA2STANDARDLIBRARYattr = 1
final val EXPLICITNULLSattr = 2
final val CAPTURECHECKEDattr = 3
final val WITHPUREFUNSattr = 4

/** Useful for debugging */
def isLegalTag(tag: Int): Boolean =
Expand Down Expand Up @@ -830,6 +834,8 @@ object TastyFormat {
def attributeTagToString(tag: Int): String = tag match {
case SCALA2STANDARDLIBRARYattr => "SCALA2STANDARDLIBRARYattr"
case EXPLICITNULLSattr => "EXPLICITNULLSattr"
case CAPTURECHECKEDattr => "CAPTURECHECKEDattr"
case WITHPUREFUNSattr => "WITHPUREFUNSattr"
}

/** @return If non-negative, the number of leading references (represented as nats) of a length/trees entry.
Expand Down
Loading