Skip to content

Commit d407633

Browse files
committed
Check that a Scala 2 macros has a Scala 3 implemetation
We must make sure we have one as the Scala 2 macro implemetations are not entered into the class.
1 parent 86fab8d commit d407633

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
146146
tree match
147147
case tree: ValOrDefDef if !tree.symbol.is(Synthetic) =>
148148
checkInferredWellFormed(tree.tpt)
149+
val sym = tree.symbol
150+
if sym.isScala2Macro then
151+
if !sym.owner.unforcedDecls.exists(p => !p.isScala2Macro && p.name == sym.name && p.signature == sym.signature) then
152+
ctx.error("No Scala 3 implementation found for this Scala 2 macro.", tree.sourcePos)
149153
case _ =>
150154
processMemberDef(tree)
151155

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class CompilationTests extends ParallelTesting {
141141
compileFile("tests/neg-custom-args/i3882.scala", allowDeepSubtypes),
142142
compileFile("tests/neg-custom-args/i4372.scala", allowDeepSubtypes),
143143
compileFile("tests/neg-custom-args/i1754.scala", allowDeepSubtypes),
144+
compileFile("tests/neg-custom-args/scala2-macro-compat-no-scala3-implementation.scala", defaultOptions.and("-Yscala2-experimental-compat")),
144145
compileFile("tests/neg-custom-args/interop-polytypes.scala", allowDeepSubtypes.and("-Yexplicit-nulls")),
145146
compileFile("tests/neg-custom-args/conditionalWarnings.scala", allowDeepSubtypes.and("-deprecation").and("-Xfatal-warnings")),
146147
compileFilesInDir("tests/neg-custom-args/isInstanceOf", allowDeepSubtypes and "-Xfatal-warnings"),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
object Macro1 {
4+
import scala.language.experimental.macros
5+
def lineNumber: Int = macro LineNumberMacro2.thisLineNumberImpl // error: No Scala 3 implementation found for this Scala 2 macro.
6+
}
7+
8+
object LineNumberMacro2 {
9+
class Context: // Dummy scala.reflect.macros.Context
10+
type Expr[+T]
11+
12+
def thisLineNumberImpl(context: Context): context.Expr[Int] = {
13+
// val lineNumber = context.enclosingPosition.line
14+
// context.literal(lineNumber)
15+
???
16+
}
17+
}

tests/pos-custom-args/scala2-macro-compat-2.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ class Context: // Dummy scala.reflect.macros.Context
66
object Macros {
77

88
def foo1(x: Int): Int = macro foo1Impl
9+
def foo1(x: Int): Int = ???
10+
911
def foo2(x: Int, y: String): Int = macro foo2Impl
12+
def foo2(x: Int, y: String): Int = ???
1013

1114
def foo1Impl(context: Context)(x: context.Expr[Int]): context.Expr[Int] = ???
1215
def foo2Impl(context: Context)(x: context.Expr[Int], y: context.Expr[String]): context.Expr[Int] = ???

0 commit comments

Comments
 (0)