scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.83k stars 1.05k forks source link

Macro-related crash with `-Ysafe-init` for test class hierarchy #19649

Closed martin-ockajak closed 4 months ago

martin-ockajak commented 7 months ago

Compiler version

The problem does not occur in the following compiler versions:

Compiler flags

-Ysafe-init

Minimized code

build.sbt

lazy val root = project.in(file(".")).settings(
  scalaVersion := "3.3.2-RC2",
  scalacOptions += "-Ysafe-init",
  libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17"
)

AB.scala

trait A extends AnyFreeSpecLike:
  "" in {}

trait B extends A:
  val test = ()

C.scala (test)

class C extends B

Output (click arrow to expand)

[Internal error] unexpected tree

```scala [error] /home/owner/safe-init-error/dotty/freespec/target/scala-3/src_managed/main/org/scalatest/freespec/AnyFreeSpecLike.scala: [Internal error] unexpected tree [error] -> class C extends B [ C.scala:1 ] [error] ^^^^^^^^^^^^^^^^^ [error] -> implicit inline def convertToFreeSpecStringWrapper(s: String): [error] AnyFreeSpecLike.this.FreeSpecStringWrapper = [error] { [error] ${ [error] { [error] def $anonfun(using evidence$3: quoted.Quotes): [error] quoted.Expr[AnyFreeSpecLike.this.FreeSpecStringWrapper] = [error] org.scalactic.source.Position.withPosition[ [error] AnyFreeSpecLike.this.FreeSpecStringWrapper]( [error] '{ [error] { [error] def $anonfun(pos: org.scalactic.source.Position): [error] AnyFreeSpecLike.this.FreeSpecStringWrapper = [error] { [error] new AnyFreeSpecLike.this.FreeSpecStringWrapper(s, pos) [error] } [error] closure($anonfun) [error] } [error] }.apply(evidence$3) [error] )(evidence$3, [error] quoted.Type.of[AnyFreeSpecLike.this.FreeSpecStringWrapper]( [error] evidence$3) [error] ) [error] closure($anonfun) [error] } [error] } [error] }:AnyFreeSpecLike.this.FreeSpecStringWrapper [error] one error found ```

Reproduce

Unpack the attached archive and run the tests:

tar xfvz safe-init-error.tar.gz
cd safe-init-error
sbt test

safe-init-error.tar.gz

Note

Thanks for making a great compiler 😄 .

liufengyun commented 7 months ago

This should have been fixed in https://github.com/lampepfl/dotty/pull/18848. Can you try the nightly or a recent release of Scala 3, such as 3.4.0-RC4.

Maybe the fix should be back-ported as well.

Related: #18407

martin-ockajak commented 7 months ago

@liufengyun I can confirm that the problem does not occur in the following compiler versions:

And indeed was this problem was previously detected by the community build in #18407 👍 and fixed in #18848.

It would be great to backport this fix to the upcoming 3.3.2 or 3.3.3 because as a library author I need to use the compiler version 3.3 (LTS) and safe initialization checking is a helpful feature which I would like to keep using. Thanks.

liufengyun commented 7 months ago

I'm not familiar with the process for back-porting. Could you please adivse? /cc: @Kordyjan

Kordyjan commented 7 months ago

I'm not that familiar with -Ysafe-init. I thought it was an unstable feature of the compiler and could only be used with the nightly releases. If I was mistaken then it means that we should backport that fix to 3.3.3.

martin-ockajak commented 7 months ago

The compiler options documentation mentions that it's a private setting with limited support. If this means it should not be considered an official part of the compiler user interface then I can just stop using it and nothing needs to be done.

Kordyjan commented 7 months ago

I asked people around and now I can confidently say that -Y settings are indeed private and come without any guarantees.

There is ongoing discussion how should they be treated in LTS (supported, disabled, emitting warning or left as they are right now). I'm leaning on the side of supporting them. I'll update this comment when there will be a decision.

martin-ockajak commented 7 months ago

@Kordyjan Thanks for looking into it 😄

liufengyun commented 4 months ago

Given that https://github.com/scala/scala3/pull/20199 is merged, this can be closed.