Skip to content
Draft
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
20 changes: 19 additions & 1 deletion compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,25 @@ class Inliner(val call: tpd.Tree)(using Context):
case tree1 @ Splice(expr) if level == 0 && !hasInliningErrors && !ctx.usedBestEffortTasty =>
val expanded = expandMacro(expr, tree1.srcPos)
transform.TreeChecker.checkMacroGeneratedTree(tree1, expanded)
typedExpr(expanded) // Inline calls and constant fold code generated by the macro
val res = typedExpr(expanded) // Inline calls and constant fold code generated by the macro

// We dealias opaque types because their aliases might not be visible
// at the expansion site. See `tests/run-macros/opaque-inline`.
val dealiasOpaques = new TypeMap:
def apply(tp: Type): Type = tp match
case tp: TypeRef if tp.typeSymbol.isOpaqueAlias =>
val sym = tp.typeSymbol
apply(sym.opaqueAlias.asSeenFrom(tp.prefix, sym.owner))
case _ =>
mapOver(tp)

val actualTp = dealiasOpaques(res.tpe)
val expectedTp = dealiasOpaques(tree1.tpe)
if !ctx.settings.XcheckMacros.value || (actualTp frozen_<:< expectedTp) then
res
else
errorTree(tree1, em"""Macro expansion has type $actualTp, which does not conform to the expected type $expectedTp""")
Comment thread
mbovel marked this conversation as resolved.

case tree1 => tree1

override def typedMatch(tree: untpd.Match, pt: Type)(using Context): Tree =
Expand Down
9 changes: 9 additions & 0 deletions tests/neg-macros/expr-asInstanceOf/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted.*

object Macro:
inline def foo(): Int =
${fooImpl()}

def fooImpl()(using Quotes): Expr[Int] =
import quotes.reflect.*
Expr("hello").asInstanceOf[Expr[Int]]
2 changes: 2 additions & 0 deletions tests/neg-macros/expr-asInstanceOf/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def test =
println(Macro.foo()) // error
12 changes: 12 additions & 0 deletions tests/neg-macros/monocle-transparent-invariant-neg/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.quoted.*

// Invariant type constructor
final class Inv[A](val value: A)

object Inv:
transparent inline def make: Inv[Tuple] =
${ makeImpl }

def makeImpl(using Quotes): Expr[Inv[Tuple]] =
val e: Expr[Inv[EmptyTuple]] = '{ new Inv[EmptyTuple](EmptyTuple) }
e.asInstanceOf[Expr[Inv[Tuple]]]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val inv: Inv[EmptyTuple] = Inv.make // error
2 changes: 1 addition & 1 deletion tests/pos-macros/i25690/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ object Tracer:

def autoTraceImpl(using Quotes): Expr[Trace] =
import quotes.reflect.*
Literal(StringConstant("loc")).asExprOf[String].asInstanceOf[Expr[Trace]]
'{ ${ Literal(StringConstant("loc")).asExprOf[String] }.asInstanceOf[Trace] }
2 changes: 1 addition & 1 deletion tests/pos-macros/i25692/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package internal.stacktracer:

object Macros:
def autoTraceImpl(using Quotes): Expr[Tracer.instance.Type] =
Expr("trace").asInstanceOf[Expr[Tracer.instance.Type]]
'{ "trace".asInstanceOf[Tracer.instance.Type] }
Comment thread
mbovel marked this conversation as resolved.

package internal.macros:
import zio.*
Expand Down
11 changes: 11 additions & 0 deletions tests/pos-macros/monocle-transparent-invariant-pos/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted.*

// Invariant type constructor
final class Inv[A](val value: A)

object Inv:
transparent inline def make: Inv[? <: Tuple] =
${ makeImpl }

def makeImpl(using Quotes): Expr[Inv[? <: Tuple]] =
'{ new Inv[EmptyTuple](EmptyTuple) }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val inv: Inv[EmptyTuple] = Inv.make
2 changes: 1 addition & 1 deletion tests/run-macros/i7887/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def myMacroImpl(a: quoted.Expr[_])(using qctx: quoted.Quotes) = {
def myMacroImpl[S](a: quoted.Expr[S])(using qctx: quoted.Quotes): quoted.Expr[S] = {
import scala.quoted.quotes.reflect.*
def typed[A] = {
implicit val t: quoted.Type[A] = a.asTerm.tpe.widen.asType.asInstanceOf[quoted.Type[A]]
Expand Down
3 changes: 1 addition & 2 deletions tests/run-macros/i7887/Test_2.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
object Test extends App {
@main def Test =
assert(myMacro(42) == 42)
}
Loading