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

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

Closed
WojciechMazur opened this issue Aug 16, 2023 · 2 comments · Fixed by #18848
Closed

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

WojciechMazur opened this issue Aug 16, 2023 · 2 comments · Fixed by #18848
Assignees
Labels
area:initialization area:metaprogramming:quotes Issues related to quotes and splices itype:bug regression This worked in a previous version but doesn't anymore

Comments

@WojciechMazur
Copy link
Contributor

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.

@WojciechMazur WojciechMazur added itype:bug regression This worked in a previous version but doesn't anymore stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 16, 2023
@mbovel mbovel added area:initialization area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 18, 2023
@liufengyun
Copy link
Contributor

Thanks for the minimization @WojciechMazur . I just noticed this one. I'll look into it shortly. Is it blocker?

@nicolasstucki
Copy link
Contributor

@liufengyun we now have the Quote, Splice, QuotePattern, and SplicePattern ASTs. Before that, they were Apply/TypeApply trees. From what I can see dotty.tools.dotc.transform.init.Semantic interpreted them as normal applications. Was this intended? I am unsure if the staging level change would affect the initialization checking algorithm.

sjrd added a commit that referenced this issue Nov 6, 2023
Fix #18407: Ignore Quote/Slice in init checker

We should never be able to encounter them in usage of macros --- because
macros will expand to normal trees without quote and splices.

The particular test case is actually problematic: The macro did not
expand in `base_0.scala`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:initialization area:metaprogramming:quotes Issues related to quotes and splices itype:bug regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants