Skip to content

Commit 7e745f0

Browse files
committed
Weaken assertion for duplicate attachments
Fixes #13218 The test shows that there are situations where an error is reported in a different context than where the assertion is tested.
1 parent 49d7399 commit 7e745f0

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
144144
/** Actions that need to be performed at the end of the current compilation run */
145145
private var finalizeActions = mutable.ListBuffer[() => Unit]()
146146

147+
/** A variable that records that some error was reported in a globally committable context.
148+
* The error will not necessarlily be emitted, since it could still be that
149+
* the enclosing context will be aborted. The variable is used as a smoke test
150+
* to turn off assertions that might be wrong if the program is erroneous. To
151+
* just test for `ctx.reporter.errorsReported` is not always enough, since it
152+
* could be that the context in which the assertion is tested is a completer context
153+
* that's different from the context where the error was reported. See i13218.scala
154+
* for a test.
155+
*/
156+
var errorsToBeReported = false
157+
147158
def compile(files: List[AbstractFile]): Unit =
148159
try
149160
val sources = files.map(runContext.getSource(_))

compiler/src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ abstract class Reporter extends interfaces.ReporterResult {
157157
case dia: Error =>
158158
errors = dia :: errors
159159
_errorCount += 1
160+
if ctx.typerState.isGlobalCommittable then
161+
ctx.run.errorsToBeReported = true
160162
case dia: Info => // nothing to do here
161163
// match error if d is something else
162164

compiler/src/dotty/tools/dotc/util/Attachment.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ object Attachment {
117117
}
118118

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

tests/neg/i13218.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class TagTest extends AnyFreeSpec:
2+
"a" - {
3+
"b" in {
4+
class TF[F[_]]
5+
meow // error
6+
}
7+
}
8+
9+
trait AnyFreeSpec:
10+
protected class Wrapper(s: String):
11+
def -(f: => Unit): Unit = ???
12+
def in(f: => Unit): Unit = ???
13+
14+
implicit def wrap(s: String): Wrapper = ???

0 commit comments

Comments
 (0)