Skip to content

incorrect ScopeException with -Xcheck-macros when few functions is generate slices inside common Quote #14137

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
rssh opened this issue Dec 19, 2021 · 5 comments · Fixed by #14203

Comments

@rssh
Copy link
Contributor

rssh commented Dec 19, 2021

Compiler version

3.1.1-RC1

Minimized code

-Xcheck-macros should be in compiler flags.

file M.scala

package x

import scala.quoted._

object Macro:

   inline def genOp(inline f:Int): Int = ${
      genOpImpl('f)
   }

   def genOpImpl(f: Expr[Int])(using Quotes): Expr[Int] = {

      def firstOp(): Expr[Int] =
      '{
        var x=1
        ${secondOp('x,f)}
      }

      def secondOp(x:Expr[Int], y:Expr[Int]): Expr[Int] =
         '{ $x + $y }

      firstOp()
   }

file X.scala:

package x

object Main:

   def main(args: Array[String]):Unit =
      Macro.genOp(10)

Output

[info] welcome to sbt 1.5.5 (Homebrew Java 17)
[info] loading global plugins from /Users/rssh/.sbt/1.0/plugins
[info] loading project definition from /Users/rssh/tests/dotty/restricted-correct-scope/project
[info] loading settings for project root from build.sbt ...
[info] set current project to test (in build file:/Users/rssh/tests/dotty/restricted-correct-scope/)
[info] Executing in batch mode. For better performance use sbt's shell
[info] compiling 2 Scala sources to /Users/rssh/tests/dotty/restricted-correct-scope/target/scala-3.1.1-RC1/classes ...
[error] -- Error: /Users/rssh/tests/dotty/restricted-correct-scope/src/main/scala/x/X.scala:6:17 
[error] 6 |      Macro.genOp(10)
[error]   |      ^^^^^^^^^^^^^^^
[error]   |Exception occurred while executing macro expansion.
[error]   |scala.quoted.runtime.impl.ScopeException: Expression created in a splice was used outside of that splice.
[error]   |Created in: src/main/scala/x/M.scala:16 at column 9
[error]   |Used in: src/main/scala/x/M.scala:20 at column 13
[error]   |Expr: x
[error]   |
[error]   |
[error]   |Creation stack:
[error]   |	src/main/scala/x/M.scala:16 at column 9
[error]   |	/Users/rssh/tests/dotty/restricted-correct-scope/src/main/scala/x/M.scala:7 at column 42
[error]   |
[error]   |
[error]   |Use stack:
[error]   |	src/main/scala/x/M.scala:20 at column 13
[error]   |	/Users/rssh/tests/dotty/restricted-correct-scope/src/main/scala/x/M.scala:7 at column 42
[error]   |
[error]   |          
[error]   |	at scala.quoted.runtime.impl.ScopeException$.checkInCorrectScope(ScopeException.scala:35)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$.quotedExprToTree(PickledQuotes.scala:43)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:82)
[error]   |	at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1430)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:106)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:96)
[error]   |	at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1396)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:106)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$.spliceTerms(PickledQuotes.scala:121)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$.unpickleTerm(PickledQuotes.scala:60)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl.unpickleExpr(QuotesImpl.scala:2973)
[error]   |	at x.Macro$.secondOp$1(M.scala:20)
[error]   |	at x.Macro$.firstOp$1$$anonfun$1(M.scala:16)
[error]   |	at x.Macro$.firstOp$1$$anonfun$adapted$1(M.scala:16)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:81)
[error]   |	at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1430)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:106)
[error]   |	at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1410)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:106)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$.spliceTerms(PickledQuotes.scala:121)
[error]   |	at dotty.tools.dotc.quoted.PickledQuotes$.unpickleTerm(PickledQuotes.scala:60)
[error]   |	at scala.quoted.runtime.impl.QuotesImpl.unpickleExpr(QuotesImpl.scala:2973)
[error]   |	at x.Macro$.firstOp$1(M.scala:16)
[error]   |	at x.Macro$.genOpImpl(M.scala:23)
[error]   |
[error]   | This location contains code that was inlined from X.scala:6
[error] one error found
[error] one error found

Expectation

should be compiled

@rssh rssh added the itype:bug label Dec 19, 2021
@rssh
Copy link
Contributor Author

rssh commented Dec 19, 2021

(after thinking -- maybe exists way to add something like @nowarn annotation )

rssh added a commit to rssh/jsoniter-scala that referenced this issue Dec 20, 2021
rssh added a commit to rssh/jsoniter-scala that referenced this issue Dec 20, 2021
@cchantep
Copy link
Contributor

Maybe related to #13926

@rssh
Copy link
Contributor Author

rssh commented Dec 23, 2021

Yes, I think this is duplicate

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Jan 3, 2022

The error showed a quote that was created with the wrong Quotes instance. The code example be fixed by tracing the current Quotes in secondOp.

package x

import scala.quoted._

object Macro:

   inline def genOp(inline f:Int): Int = ${
      genOpImpl('f)
   }

   def genOpImpl(f: Expr[Int])(using Quotes): Expr[Int] = {


     def firstOp(): Expr[Int] =
      '{
        var x=1
        ${secondOp('x,f)}
      }
-     def secondOp(x:Expr[Int], y:Expr[Int]): Expr[Int] =
+     def secondOp(x:Expr[Int], y:Expr[Int])(using Quotes): Expr[Int] =
         '{ $x + $y }

      firstOp()
   }

The compiler can still detect and fix the mistake, but it will cost it some more circles to fix it. If this pattern is nested repeatedly it can have an exponential cost.

@nicolasstucki
Copy link
Contributor

What we do need is a better way to hint at how to fix these errors in the error itself.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 3, 2022
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 3, 2022
plokhotnyuk pushed a commit to plokhotnyuk/jsoniter-scala that referenced this issue Jan 9, 2022
plokhotnyuk pushed a commit to plokhotnyuk/jsoniter-scala that referenced this issue Jan 9, 2022
olsdavis pushed a commit to olsdavis/dotty that referenced this issue Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants