-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add attributes section to TASTy and use it for Stdlib TASTy #18599
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dotty.tools.dotc.core.tasty | ||
|
||
import dotty.tools.dotc.ast.{tpd, untpd} | ||
|
||
import dotty.tools.tasty.TastyBuffer | ||
import dotty.tools.tasty.TastyFormat, TastyFormat.AttributesSection | ||
|
||
import java.nio.charset.StandardCharsets | ||
|
||
object AttributePickler: | ||
|
||
def pickleAttributes( | ||
attributes: Attributes, | ||
pickler: TastyPickler, | ||
buf: TastyBuffer | ||
): Unit = | ||
if attributes.scala2StandardLibrary || attributes.explicitNulls then // or any other attribute is set | ||
pickler.newSection(AttributesSection, buf) | ||
// Pickle attributes | ||
if attributes.scala2StandardLibrary then buf.writeNat(TastyFormat.SCALA2STANDARDLIBRARYattr) | ||
if attributes.explicitNulls then buf.writeNat(TastyFormat.EXPLICITNULLSattr) | ||
end if | ||
|
||
end pickleAttributes | ||
|
||
end AttributePickler |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dotty.tools.dotc | ||
package core.tasty | ||
|
||
import scala.language.unsafeNulls | ||
|
||
import dotty.tools.tasty.{TastyFormat, TastyReader, TastyBuffer} | ||
|
||
import java.nio.charset.StandardCharsets | ||
|
||
class AttributeUnpickler(reader: TastyReader): | ||
import reader._ | ||
|
||
lazy val attributeTags: List[Int] = | ||
val listBuilder = List.newBuilder[Int] | ||
while !isAtEnd do listBuilder += readNat() | ||
listBuilder.result() | ||
|
||
lazy val attributes: Attributes = { | ||
var scala2StandardLibrary = false | ||
var explicitNulls = false | ||
for attributeTag <- attributeTags do | ||
attributeTag match | ||
case TastyFormat.SCALA2STANDARDLIBRARYattr => scala2StandardLibrary = true | ||
case TastyFormat.EXPLICITNULLSattr => explicitNulls = true | ||
case attribute => | ||
assert(false, "Unexpected attribute value: " + attribute) | ||
Attributes( | ||
scala2StandardLibrary, | ||
explicitNulls, | ||
) | ||
} | ||
|
||
end AttributeUnpickler |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package dotty.tools.dotc.core.tasty | ||
|
||
class Attributes( | ||
val scala2StandardLibrary: Boolean, | ||
val explicitNulls: Boolean, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ class ExtensionMethods extends MiniPhase with DenotTransformer with FullParamete | |
|
||
// Create extension methods, except if the class comes from Scala 2 | ||
// because it adds extension methods before pickling. | ||
if (!(valueClass.is(Scala2x))) | ||
if !valueClass.is(Scala2x, butNot = Scala2Tasty) then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed this issue in this commit to be able to test the attribute in |
||
for (decl <- valueClass.classInfo.decls) | ||
if isMethodWithExtension(decl) then | ||
enterInModuleClass(createExtensionMethod(decl, moduleClassSym.symbol)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -265,8 +265,15 @@ All elements of a position section are serialized as Ints | |
|
||
Standard Section: "Comments" Comment* | ||
```none | ||
Comment = Length Bytes LongInt // Raw comment's bytes encoded as UTF-8, followed by the comment's coordinates. | ||
Comment = UTF8 LongInt // Raw comment's bytes encoded as UTF-8, followed by the comment's coordinates. | ||
``` | ||
|
||
Standard Section: "Attributes" Attribute* | ||
```none | ||
Attribute = SCALA2STANDARDLIBRARYattr | ||
EXPLICITNULLSattr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering if this is the correct way to define experimental language attributes. We might end up at some point with attributes that are never used in stable code. On the other hand, we make sure that all attributes that we ever use are unambiguous. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicit-nulls is not experimental, at least not in the meaning of |
||
``` | ||
|
||
**************************************************************************************/ | ||
|
||
object TastyFormat { | ||
|
@@ -361,6 +368,7 @@ object TastyFormat { | |
final val ASTsSection = "ASTs" | ||
final val PositionsSection = "Positions" | ||
final val CommentsSection = "Comments" | ||
final val AttributesSection = "Attributes" | ||
|
||
/** Tags used to serialize names, should update [[TastyFormat$.nameTagToString]] if a new constant is added */ | ||
class NameTags { | ||
|
@@ -597,6 +605,12 @@ object TastyFormat { | |
final val firstNatASTTreeTag = IDENT | ||
final val firstLengthTreeTag = PACKAGE | ||
|
||
|
||
// Attributes tags | ||
|
||
final val SCALA2STANDARDLIBRARYattr = 1 | ||
final val EXPLICITNULLSattr = 2 | ||
|
||
/** Useful for debugging */ | ||
def isLegalTag(tag: Int): Boolean = | ||
firstSimpleTreeTag <= tag && tag <= SPLITCLAUSE || | ||
|
@@ -812,6 +826,11 @@ object TastyFormat { | |
case HOLE => "HOLE" | ||
} | ||
|
||
def attributeTagToString(tag: Int): String = tag match { | ||
case SCALA2STANDARDLIBRARYattr => "SCALA2STANDARDLIBRARYattr" | ||
case EXPLICITNULLSattr => "EXPLICITNULLSattr" | ||
} | ||
|
||
/** @return If non-negative, the number of leading references (represented as nats) of a length/trees entry. | ||
* If negative, minus the number of leading non-reference trees. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we tag the top-level class with an annotation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will need help from someone familiar with explicit nulls to finish this side of the implementation. We can do that in another PR. I could also remove this attribute temporarily until we have the full implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What matters for now is that the information gets stored. If it's not used by dotc at the moment, then we don't care.