Skip to content

Regrssion in automorph-org/automorph + scalatest/scalatest #18407

Closed
@WojciechMazur

Description

@WojciechMazur

Combination of -Ysafe-init, macros and multi source compilation leads to compilation error.
Regression found in automorph-org/automorph, but problem might exist in all projects using Scalatest framework if they define compose common tests comming from different sub-modules

Open CB builds logs

Compiler version

3.3.1-RC5
3.4.0-RC1-bin-20230815-ca6a80e-NIGHTLY-git-ca6a80e
Works in 3.3.0

Bisect points to deb03a7

Minimized code

// macros_0.scala 
object source {
  import scala.quoted._

  class Position()

  object Position {
    def withPosition[T](
        fun: Expr[Position => T]
    )(using quotes: Quotes, typeOfT: Type[T]): Expr[T] = {
      '{
        ${ fun }.apply(new source.Position())
      }
    }
  }
}

trait AnyFreeSpecLike {
  import scala.language.implicitConversions

  protected final class FreeSpecStringWrapper(
      string: String,
      pos: source.Position
  ) {
    def -(fun: => Unit): Unit = fun
  }

  inline implicit def convertToFreeSpecStringWrapper(
      s: String
  ): FreeSpecStringWrapper = {
    ${
      source.Position.withPosition[FreeSpecStringWrapper]('{
        (pos: source.Position) => new FreeSpecStringWrapper(s, pos)
      })
    }
  }
}
// base_0.scala
trait BaseTest extends AnyFreeSpecLike {
  "empty-test" - {}
}

Needs to be compiled in different compilation-unit:

// test_1.scala 
//> using options -Ysafe-init

class MyTest extends BaseTest {
    private val myObject = new {}
    "empty-test" - {}
}

Output

In 3.3.1-RC5 error

-- Error: macros.scala:30:4 ----------------------------------------------------
30 |    ${
   |    ^
   |[Internal error] unexpected tree
   |-> class MyTest extends BaseTest {  [ main.test.scala:3 ]
   |   ^
   |-> implicit inline def convertToFreeSpecStringWrapper(s: String):
   |  AnyFreeSpecLike.this.FreeSpecStringWrapper =
   |  {
   |    ${
   |      {
   |        def $anonfun(using evidence$2: quoted.Quotes):
   |          quoted.Expr[AnyFreeSpecLike.this.FreeSpecStringWrapper] =
   |          source.Position.withPosition[
   |            AnyFreeSpecLike.this.FreeSpecStringWrapper](
   |            '{
   |              {
   |                def $anonfun(pos: source.Position):
   |                  AnyFreeSpecLike.this.FreeSpecStringWrapper =
   |                  {
   |                    new AnyFreeSpecLike.this.FreeSpecStringWrapper(s, pos)
   |                  }
   |                closure($anonfun)
   |              }
   |            }.apply(evidence$2)
   |          )(evidence$2,
   |            quoted.Type.of[AnyFreeSpecLike.this.FreeSpecStringWrapper](
   |              evidence$2)
   |          )
   |        closure($anonfun)
   |      }
   |    }
   |  }:AnyFreeSpecLike.this.FreeSpecStringWrapper
31 |      source.Position.withPosition[FreeSpecStringWrapper]('{
32 |        (pos: source.Position) => new FreeSpecStringWrapper(s, pos)
33 |      })
34 |    }

In nightly warning:

-- Warning: macros.scala:30:4 --------------------------------------------------
30 |    ${
   |    ^
   |    [Internal error] unexpected tree
   |    -> class MyTest extends BaseTest {      [ main.test.scala:3 ]
   |       ^
   |    -> method convertToFreeSpecStringWrapper
31 |      source.Position.withPosition[FreeSpecStringWrapper]('{
32 |        (pos: source.Position) => new FreeSpecStringWrapper(s, pos)
33 |      })
34 |    }

Expectation

Should not throw compilation error when using -Ysafe-init as there is no unsafe inititialization present in scope.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions