Skip to content

Even simple macros do not expand with capture checking #18855

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

Closed
reftrans opened this issue Nov 6, 2023 · 1 comment · Fixed by #18915
Closed

Even simple macros do not expand with capture checking #18855

reftrans opened this issue Nov 6, 2023 · 1 comment · Fixed by #18915
Assignees
Labels
area:experimental:cc Capture checking related area:metaprogramming:quotes Issues related to quotes and splices cc-experiment Intended to be merged with cc-experiment branch on origin itype:bug itype:crash

Comments

@reftrans
Copy link

reftrans commented Nov 6, 2023

Compiler version

Scala compiler version 3.4.0-RC1-bin-SNAPSHOT-git-f91e5d8

Minimized code

macro.scala:

import scala.quoted.*
import scala.language.experimental.captureChecking

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

invoc.scala:

import scala.language.experimental.captureChecking
val x = run()

Output (click arrow to expand)

> ../dotty/bin/scalac macro.scala invoc.scala

  exception occurred while typechecking invoc.scala

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/lampepfl/dotty/issues/new/choose

     while compiling: invoc.scala
        during phase: typer
                mode: Mode(ImplicitsEnabled)
     library version: version 2.13.12
    compiler version: version 3.4.0-RC1-bin-SNAPSHOT-git-f91e5d8
            settings:


  Exception while compiling macro.scala, invoc.scala

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/lampepfl/dotty/issues/new/choose

     while compiling: <no file>
        during phase: parser
                mode: Mode()
     library version: version 2.13.12
    compiler version: version 3.4.0-RC1-bin-SNAPSHOT-git-f91e5d8
            settings:

Exception in thread "main" java.lang.IndexOutOfBoundsException: 0 is out of bounds (min 0, max -1)
	at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:107)
	at dotty.tools.dotc.core.Contexts$.comparer(Contexts.scala:832)
	at dotty.tools.dotc.core.TypeComparer$.testSubType(TypeComparer.scala:3058)
	at dotty.tools.dotc.typer.Typer.adaptNoArgsOther$1(Typer.scala:4096)
	at dotty.tools.dotc.typer.Typer.adaptNoArgs$1(Typer.scala:4183)
	at dotty.tools.dotc.typer.Typer.adapt1(Typer.scala:4395)
	at dotty.tools.dotc.typer.Typer.adapt(Typer.scala:3697)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3293)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3297)
	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3409)
	at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2881)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3166)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3216)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3293)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3297)
	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3409)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$1(TyperPhase.scala:45)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at dotty.tools.dotc.core.Phases$Phase.monitor(Phases.scala:468)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck(TyperPhase.scala:51)
	at dotty.tools.dotc.typer.TyperPhase.$anonfun$4(TyperPhase.scala:97)
	at scala.collection.Iterator$$anon$6.hasNext(Iterator.scala:479)
	at scala.collection.Iterator$$anon$9.hasNext(Iterator.scala:583)
	at scala.collection.immutable.List.prependedAll(List.scala:152)
	at scala.collection.immutable.List$.from(List.scala:684)
	at scala.collection.immutable.List$.from(List.scala:681)
	at scala.collection.IterableOps$WithFilter.map(Iterable.scala:898)
	at dotty.tools.dotc.typer.TyperPhase.runOn(TyperPhase.scala:98)
	at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:315)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
	at dotty.tools.dotc.Run.runPhases$1(Run.scala:337)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:348)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:357)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:71)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:357)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:267)
	at dotty.tools.dotc.Driver.finish(Driver.scala:58)
	at dotty.tools.dotc.Driver.doCompile(Driver.scala:38)
	at dotty.tools.dotc.Driver.process(Driver.scala:197)
	at dotty.tools.dotc.Driver.process(Driver.scala:165)
	at dotty.tools.dotc.Driver.process(Driver.scala:177)
	at dotty.tools.dotc.Driver.main(Driver.scala:207)
	at dotty.tools.MainGenericCompiler$.run$1(MainGenericCompiler.scala:162)
	at dotty.tools.MainGenericCompiler$.main(MainGenericCompiler.scala:186)
	at dotty.tools.MainGenericCompiler.main(MainGenericCompiler.scala)
@reftrans reftrans added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 6, 2023
@sjrd sjrd added area:experimental:cc Capture checking related area:metaprogramming:quotes Issues related to quotes and splices labels Nov 6, 2023
@reftrans
Copy link
Author

reftrans commented Nov 6, 2023

As for as I can tell, the error is here:
https://github.com/lampepfl/dotty/blob/main/compiler/src/dotty/tools/dotc/core/Contexts.scala#L836-L844

The line base.comparersInUse += 1 should only appear in the else branch so that it corresponds to the comparers array buffer. I tried changing it and the crash goes away.

@Kordyjan Kordyjan added cc-experiment Intended to be merged with cc-experiment branch on origin and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 7, 2023
Linyxus added a commit to dotty-staging/dotty that referenced this issue Nov 14, 2023
odersky added a commit that referenced this issue Nov 14, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:experimental:cc Capture checking related area:metaprogramming:quotes Issues related to quotes and splices cc-experiment Intended to be merged with cc-experiment branch on origin itype:bug itype:crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants