Skip to content

Commit

Permalink
Reset comparersInUse to zero in ContextState.reset (#18915)
Browse files Browse the repository at this point in the history
Fixes #18855

The code in #18855 used to crash the compiler with an out-of-bound
access exception. It was because, previously, `ContextState.reset`
clears the `comparers`, the cached pool of `TypeComparer`s, while
keeping the variable `comparersInUse` intact. However, the `comparer`
method of a context assumes `comparers` has a size greater than or equal
to `comparersInUse`. So the invariant is clearly violated here and thus
triggers the crash.

This PR proposes a straightforward fix. Yet, the reason why only under
both macros and cc this code path will be triggered remains to be
investigated.
  • Loading branch information
odersky authored Nov 14, 2023
2 parents 563fab9 + 44c9674 commit 0cd94fc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,7 @@ object Contexts {
sources.clear()
files.clear()
comparers.clear() // forces re-evaluation of top and bottom classes in TypeComparer
comparersInUse = 0

// Test that access is single threaded

Expand Down
3 changes: 3 additions & 0 deletions tests/pos-macros/i18855/invoc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.language.experimental.captureChecking
val x = run()

7 changes: 7 additions & 0 deletions tests/pos-macros/i18855/macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.*
import scala.language.experimental.captureChecking

def impl()(using Quotes): Expr[Unit] = '{()}
inline def run(): Unit = ${impl()}


0 comments on commit 0cd94fc

Please sign in to comment.