Skip to content

Fix #9294: Harden pushAttachment #9320

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

Merged
merged 1 commit into from
Jul 8, 2020
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
10 changes: 6 additions & 4 deletions compiler/src/dotty/tools/dotc/util/Attachment.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dotty.tools.dotc.util

import dotty.tools.dotc.core.Contexts.Context

/** A class inheriting from Attachment.Container supports
* adding, removing and lookup of attachments. Attachments are typed key/value pairs.
*
Expand Down Expand Up @@ -103,19 +105,19 @@ object Attachment {
final def withAttachmentsFrom(container: Container): this.type = {
var current: Link[?] = container.next
while (current != null) {
if (current.key.isInstanceOf[StickyKey[?]]) pushAttachment(current.key, current.value)
if (current.key.isInstanceOf[StickyKey[?]]) putAttachment(current.key, current.value)
current = current.next
}
this
}

def withAttachment[V](key: Key[V], value: V): this.type = {
pushAttachment(key, value)
putAttachment(key, value)
this
}

final def pushAttachment[V](key: Key[V], value: V): Unit = {
assert(!hasAttachment(key), s"duplicate attachment for key $key")
final def pushAttachment[V](key: Key[V], value: V)(using ctx: Context): Unit = {
assert(!hasAttachment(key) || ctx.reporter.errorsReported, s"duplicate attachment for key $key")
next = new Link(key, value, next)
}

Expand Down
1 change: 1 addition & 0 deletions tests/neg/i9294.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@foo class foo[X] // error // error