Skip to content
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

Crash when reporting initialization warning using -Ysafe-init-global flag #18625

Closed
q-ata opened this issue Sep 29, 2023 · 1 comment
Closed

Comments

@q-ata
Copy link
Contributor

q-ata commented Sep 29, 2023

Compiler version

3.4.0-RC1-bin-SNAPSHOT-git-48a7871 (commit 48a7871)

Minimized code

The crash occurs when compiling the following code with -Ysafe-init-global.

object A {
  def a: Int =
    B
      .s.length
}

object B {
  val s: String = A.a + "a"
}

This should report an initialization warning but instead crashes the compiler.

It seems this is caused by the method positionMarker in Trace.scala.

  private def positionMarker(pos: SourcePosition): String =
    val trimmed = pos.lineContent.takeWhile(c => c.isWhitespace).length
    val padding = pos.startColumnPadding.substring(trimmed).nn + "   "
    val carets =
      if (pos.startLine == pos.endLine)
        "^" * math.max(1, pos.endColumn - pos.startColumn)
      else "^"

    s"$padding$carets\n"

Since the line .s.length in the test case has 6 leading spaces, trimmed in positionMarker is 6, but pos.startColumnPadding is only of length 4 since it is computed from the previous line B. As such, trying to take the substring of pos.startColumnPadding starting from index 6 is an error.

Output (click arrow to expand)

The stack trace is as follows:

  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(ImplicitsEnabled)
     library version: version 2.13.12
    compiler version: version 3.4.0-RC1-bin-SNAPSHOT-git-48a7871
            settings: -Ysafe-init-global true

                tree: EmptyTree
       tree position: :<unknown>
           tree type: <notype>
              symbol: val <none>
           call site: package <root> in module class <root>

  == Source file context for tree position ==


Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
        at java.base/java.lang.String.substring(String.java:1841)
        at dotty.tools.dotc.transform.init.Trace$.positionMarker(Trace.scala:81)
        at dotty.tools.dotc.transform.init.Trace$.buildStacktrace$$anonfun$1(Trace.scala:60)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.immutable.Vector.foreach(Vector.scala:2124)
        at dotty.tools.dotc.transform.init.Trace$.buildStacktrace(Trace.scala:69)
        at dotty.tools.dotc.transform.init.Trace$.show(Trace.scala:30)
        at dotty.tools.dotc.transform.init.Objects$.select$$anonfun$3(Objects.scala:796)
        at dotty.tools.dotc.core.Decorators$.toMessage$$anonfun$1(Decorators.scala:70)
        at dotty.tools.dotc.reporting.NoExplanation.msg(Message.scala:431)
        at dotty.tools.dotc.reporting.Message.message$$anonfun$1(Message.scala:375)
        at dotty.tools.dotc.reporting.Message.inMessageContext(Message.scala:361)
        at dotty.tools.dotc.reporting.Message.message(Message.scala:375)
        at dotty.tools.dotc.reporting.Message.isNonSensical(Message.scala:352)
        at dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden(HideNonSensicalMessages.scala:16)
        at dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden$(HideNonSensicalMessages.scala:10)
        at dotty.tools.dotc.reporting.AbstractReporter.isHidden(AbstractReporter.scala:8)
        at dotty.tools.dotc.reporting.Reporter.issueUnconfigured(Reporter.scala:156)
        at dotty.tools.dotc.reporting.Reporter.go$1(Reporter.scala:177)
        at dotty.tools.dotc.reporting.Reporter.issueIfNotSuppressed(Reporter.scala:191)
        at dotty.tools.dotc.reporting.Reporter.report(Reporter.scala:203)
        at dotty.tools.dotc.transform.init.Objects$State$.doCheckObject$$anonfun$1(Objects.scala:277)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.immutable.List.foreach(List.scala:333)
        at dotty.tools.dotc.transform.init.Objects$State$.doCheckObject(Objects.scala:277)
        at dotty.tools.dotc.transform.init.Objects$State$.checkObjectAccess(Objects.scala:302)
        at dotty.tools.dotc.transform.init.Objects$.accessObject(Objects.scala:1003)
        at dotty.tools.dotc.transform.init.Objects$.checkClasses$$anonfun$2(Objects.scala:1016)
        at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:576)
        at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:574)
        at scala.collection.AbstractIterable.foreach(Iterable.scala:933)
        at scala.collection.IterableOps$WithFilter.foreach(Iterable.scala:903)
        at dotty.tools.dotc.transform.init.Objects$.checkClasses(Objects.scala:1016)
        at dotty.tools.dotc.transform.init.Checker.runOn(Checker.scala:43)
        at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:246)
        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:263)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:271)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:280)
        at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:71)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:280)
        at dotty.tools.dotc.Run.compileSources(Run.scala:194)
        at dotty.tools.dotc.Run.compile(Run.scala:179)
        at dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
        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)
@q-ata q-ata added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 29, 2023
@liufengyun liufengyun added area:initialization and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 30, 2023
@liufengyun liufengyun assigned liufengyun and q-ata and unassigned liufengyun Sep 30, 2023
@olhotak
Copy link
Contributor

olhotak commented Oct 10, 2023

Fixed by #18668

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants