Skip to content

Support the compilation of Scala 2.13 StringContext #9442

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ class Definitions {
@tu lazy val ClassTagClass: ClassSymbol = requiredClass("scala.reflect.ClassTag")
@tu lazy val ClassTagModule: Symbol = ClassTagClass.companionModule
@tu lazy val ClassTagModule_apply: Symbol = ClassTagModule.requiredMethod(nme.apply)
@tu lazy val ReflectPackageClass: Symbol = requiredPackage("scala.reflect.package").moduleClass


@tu lazy val QuotedExprClass: ClassSymbol = requiredClass("scala.quoted.Expr")
@tu lazy val QuotedExprModule: Symbol = QuotedExprClass.companionModule
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ object SymDenotations {
/** Is this a Scala 2 macro defined */
final def isScala2MacroInScala3(using Context): Boolean =
is(Macro, butNot = Inline) && is(Erased)
// Consider the macros of StringContext as plain Scala 2 macros when
// compiling the standard library with Dotty.
// This should be removed on Scala 3.1
&& owner.ne(defn.StringContextClass)

/** An erased value or an erased inline method or field */
def isEffectivelyErased(using Context): Boolean =
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
checkInferredWellFormed(tree.tpt)
val sym = tree.symbol
if sym.isScala2Macro && !ctx.settings.XignoreScala2Macros.value then
if !sym.owner.unforcedDecls.exists(p => !p.isScala2Macro && p.name == sym.name && p.signature == sym.signature) then
if !sym.owner.unforcedDecls.exists(p => !p.isScala2Macro && p.name == sym.name && p.signature == sym.signature)
// Allow scala.reflect.materializeClassTag to be able to compile scala/reflect/package.scala
// This should be removed on Scala 3.1
&& sym.owner != defn.ReflectPackageClass
then
report.error("No Scala 3 implementation found for this Scala 2 macro.", tree.sourcePos)
case _ =>
processMemberDef(tree)
Expand Down
24 changes: 13 additions & 11 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3222,21 +3222,23 @@ class Typer extends Namer
if ((inlined ne tree) && errorCount == ctx.reporter.errorCount) readaptSimplified(inlined)
else inlined
}
else if tree.symbol.name == nme.f && tree.symbol == defn.StringContext_f then
// To avoid forcing StringContext_f when compiling StingContex
// we test the name before accession symbol StringContext_f.

// As scala.StringContext.f is defined in the standard library which
// we currently do not bootstrap we cannot implement the macro in the library.
// To overcome the current limitation we intercept the call and rewrite it into
// a call to dotty.internal.StringContext.f which we can implement using the new macros.
// As the macro is implemented in the bootstrapped library, it can only be used from the bootstrapped compiler.
val Apply(TypeApply(Select(sc, _), _), args) = tree
val newCall = ref(defn.InternalStringContextMacroModule_f).appliedTo(sc).appliedToArgs(args).withSpan(tree.span)
readaptSimplified(Inliner.inlineCall(newCall))
else if (tree.symbol.isScala2Macro &&
// raw and s are eliminated by the StringInterpolatorOpt phase
tree.symbol != defn.StringContext_raw &&
tree.symbol != defn.StringContext_s)
if (tree.symbol eq defn.StringContext_f) {
// As scala.StringContext.f is defined in the standard library which
// we currently do not bootstrap we cannot implement the macro in the library.
// To overcome the current limitation we intercept the call and rewrite it into
// a call to dotty.internal.StringContext.f which we can implement using the new macros.
// As the macro is implemented in the bootstrapped library, it can only be used from the bootstrapped compiler.
val Apply(TypeApply(Select(sc, _), _), args) = tree
val newCall = ref(defn.InternalStringContextMacroModule_f).appliedTo(sc).appliedToArgs(args).withSpan(tree.span)
readaptSimplified(Inliner.inlineCall(newCall))
}
else if (ctx.settings.XignoreScala2Macros.value) {
if (ctx.settings.XignoreScala2Macros.value) {
report.warning("Scala 2 macro cannot be used in Dotty, this call will crash at runtime. See https://dotty.epfl.ch/docs/reference/dropped-features/macros.html", tree.sourcePos.startPos)
Throw(New(defn.MatchErrorClass.typeRef, Literal(Constant(s"Reached unexpanded Scala 2 macro call to ${tree.symbol.showFullName} compiled with -Xignore-scala2-macros.")) :: Nil))
.withType(tree.tpe)
Expand Down
Loading