Cannot call macro defined in the same source file error
See original GitHub issueCompiler version
3.0.1-RC1
Minimized code
MainMacro.scala
import scala.quoted.*
case class Wrapper[T](t: T) extends MainMacro[T]
trait MainMacro[U] { this: Wrapper[U] =>
inline def showTypeRepr: String = ${ MainMacro.showTypeReprImpl[U]}
}
object MainMacro {
def showTypeReprImpl[U: Type](using Quotes): Expr[String] = {
import quotes.reflect.*
val tpe = TypeRepr.of[U]
Expr(tpe.toString)
}
}
Test1.scala
object TestData {
case class Person(name: String, age: Int)
}
class Test1 {
import TestData.*
// Notice that without inline does not work also
inline def in(fun: => Any): Any = fun
in {
Wrapper(Person("a", 1)).showTypeRepr
}
}
Output
$ mkdir classes
$ scalac -3.0.2-RC1 -d classes MainMacro.scala
$ scalac -3.0.2-RC1 -d classes -cp classes Test1.scala
-- Error: Test1.scala:13:28 ----------------------------------------------------
13 | Wrapper(Person("a", 1)).showTypeRepr
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Cannot call macro class Person defined in the same source file
| This location contains code that was inlined from Test1.scala:13
1 error found
Expectation
It should compile, I guess.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:13 (6 by maintainers)
Top Results From Across the Web
How can I fix a macro not being defined when I link a source ...
I have defined the UNICODE macro within my main file, however when I link the source file( "SythConsole.cpp") and compile. I get an...
Read more >Developers - Cannot call macro defined in the same source file -
Coming soon: A brand new website interface for an even better experience!
Read more >Basic macro for line number - Question - Scala Users
Attempting to write a simple macro in scala-3 that returns the line number at the calling site and am running into some trouble....
Read more >Solving "Apparent Invocation of Macro Not Resolved" Problems
Apparent symbolic reference xxx not resolved. You are trying to resolve a macro variable in the same DATA step as the CALL SYMPUT...
Read more >Chapter 4: The NASM Preprocessor
the preprocessor will be able to handle both types of macro call, by counting the parameters ... and then re-define it later in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
How I fixed it
Cannot call macro class Person defined in the same source file
message in the compiler. Specifically theCannot call macro
substring. I found this error reporting https://github.com/lampepfl/dotty/blob/16ded76a00a993492611faa1e56689aeba0a4aee/compiler/src/dotty/tools/dotc/typer/Inliner.scala#L1856body
and itsdependencies
and found thatPerson
was listed in it.Person
is only used in a type arguments and is therefore erased when compiled. Therfore it should not have been in the dependencies.macroDependencies
and saw that we do not handle theTypeApply
case. This implies that all symbol references in the type argument will be collected as if they were terms.TypeApply
that ignores the type argument and collects references from the prefix of the application. This worked.TypeApply
case and found that we already have anif (level != 1) ... else
making sure that we are at level -1. I noticed thatlevel == -1
was in another guard unnecessarily and removed it.Note: when adding the
case TypeApply(fun, _) =>
case I used by mistakefoldOver(syms, fun)
instead ofapply(syms, fun)
. This made the logic ignoreIdent
prefixes of the application and therefore some dependencies were identified. This made some tests fail because they were expected use suspension of the compilation unit that contained the dependency.@mbore @nicolasstucki @adamw, Fyi I updated the example in https://github.com/cheeseng/scala3-inline-problem to use scala 3.0.2-RC1 but unfortunately it still encounters the same error: