Skip to content

Commit

Permalink
Ensure no duplicated and ordered TASTy attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Dec 1, 2023
1 parent e777aed commit e942cd0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ object AttributePickler:
): Unit =
pickler.newSection(AttributesSection, buf)

var lastTag = -1
def assertTagOrder(tag: Int): Unit =
assert(tag != lastTag, s"duplicate attribute tag: $tag")
assert(tag > lastTag, s"attribute tags are not ordered: $tag after $lastTag")
lastTag = tag

for tag <- attributes.booleanTags do
assert(isBooleanAttrTag(tag), "Not a boolean attribute tag: " + tag)
assertTagOrder(tag)
buf.writeByte(tag)

assert(attributes.stringTagValues.exists(_._1 == SOURCEFILEattr))
for (tag, value) <- attributes.stringTagValues do
assert(isStringAttrTag(tag), "Not a string attribute tag: " + tag)
assertTagOrder(tag)
val utf8Ref = pickler.nameBuffer.utf8Index(value)
buf.writeByte(tag)
buf.writeNat(utf8Ref.index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AttributeUnpickler(reader: TastyReader, nameAtRef: NameTable):
val booleanTags = BitSet.newBuilder
val stringTagValue = List.newBuilder[(Int, String)]

var lastTag = -1
while !isAtEnd do
val tag = readByte()
if isBooleanAttrTag(tag) then
Expand All @@ -26,6 +27,11 @@ class AttributeUnpickler(reader: TastyReader, nameAtRef: NameTable):
else
assert(false, "unknown attribute tag: " + tag)

assert(tag != lastTag, s"duplicate attribute tag: $tag")
assert(tag > lastTag, s"attribute tags are not ordered: $tag after $lastTag")
lastTag = tag
end while

new Attributes(booleanTags.result(), stringTagValue.result())
}

Expand Down
1 change: 1 addition & 0 deletions tasty/src/dotty/tools/tasty/TastyFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ Standard Section: "Attributes" Attribute*
OUTLINEattr
SOURCEFILEattr Utf8Ref
```
Attribute tags cannot be repeated in an attribute section. Attributes are ordered by the tag ordinal.
Note: Attribute tags are grouped into categories that determine what follows, and thus allow to compute the size of the tagged tree in a generic way.
Unassigned categories can be used to extend and existing category or to add new kinds of attributes
Expand Down

0 comments on commit e942cd0

Please sign in to comment.