diff --git a/compiler/src/dotty/tools/dotc/ast/tpd.scala b/compiler/src/dotty/tools/dotc/ast/tpd.scala index 9d786d21c44c..64f56047c181 100644 --- a/compiler/src/dotty/tools/dotc/ast/tpd.scala +++ b/compiler/src/dotty/tools/dotc/ast/tpd.scala @@ -1018,7 +1018,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { */ class MapToUnderlying extends TreeMap { override def transform(tree: Tree)(implicit ctx: Context): Tree = tree match { - case tree: Ident if !tree.symbol.owner.isClass && skipLocal(tree.symbol) => + case tree: Ident if tree.symbol.exists && !tree.symbol.owner.isClass && skipLocal(tree.symbol) => tree.symbol.defTree match { case defTree: ValOrDefDef => transform(defTree.rhs) case _ => tree diff --git a/tests/pos/i6171/Macro_1.scala b/tests/pos/i6171/Macro_1.scala new file mode 100644 index 000000000000..3f0969a1f56e --- /dev/null +++ b/tests/pos/i6171/Macro_1.scala @@ -0,0 +1,13 @@ +import scala.quoted._ +import scala.tasty._ + +object scalatest { + + inline def assert(x: => Any): Unit = ${ assertImpl('x) } + + def assertImpl(x: Expr[Any])(implicit refl: Reflection): Expr[Unit] = { + import refl._ + x.unseal.underlyingArgument + '{ () } + } +} diff --git a/tests/pos/i6171/Test_2.scala b/tests/pos/i6171/Test_2.scala new file mode 100644 index 000000000000..778dd129fcf8 --- /dev/null +++ b/tests/pos/i6171/Test_2.scala @@ -0,0 +1,7 @@ +object Test { + import scalatest._ + + def main(args: Array[String]): Unit = { + assert(new Some(5)) + } +} diff --git a/tests/run-with-compiler/i6171/Macro_1.scala b/tests/run-with-compiler/i6171/Macro_1.scala new file mode 100644 index 000000000000..32af57d0f558 --- /dev/null +++ b/tests/run-with-compiler/i6171/Macro_1.scala @@ -0,0 +1,45 @@ +import scala.quoted._ +import scala.tasty._ + +object scalatest { + + inline def assert(condition: => Boolean): Unit = ${ assertImpl('condition, '{""}) } + + def assertImpl(cond: Expr[Boolean], clue: Expr[Any])(implicit refl: Reflection): Expr[Unit] = { + import refl._ + import util._ + import quoted.Toolbox.Default._ + + def isImplicitMethodType(tp: Type): Boolean = + Type.IsMethodType.unapply(tp).flatMap(tp => if tp.isImplicit then Some(true) else None).nonEmpty + cond.unseal.underlyingArgument match { + case t @ Term.Apply(Term.Select(lhs, op), rhs :: Nil) => + let(lhs) { left => + let(rhs) { right => + val app = Term.Select.overloaded(left, op, Nil, right :: Nil) + let(app) { result => + val l = left.seal[Any] + val r = right.seal[Any] + val b = result.seal[Boolean] + val code = '{ scala.Predef.assert($b) } + code.unseal + } + } + }.seal[Unit] + case Term.Apply(f @ Term.Apply(Term.Select(Term.Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits) + if isImplicitMethodType(f.tpe) => + let(lhs) { left => + let(rhs) { right => + val app = Term.Select.overloaded(Term.Apply(qual, left :: Nil), op, Nil, right :: Nil) + let(Term.Apply(app, implicits)) { result => + val l = left.seal[Any] + val r = right.seal[Any] + val b = result.seal[Boolean] + val code = '{ scala.Predef.assert($b) } + code.unseal + } + } + }.seal[Unit] + } + } +} diff --git a/tests/run-with-compiler/i6171/Test_2.scala b/tests/run-with-compiler/i6171/Test_2.scala new file mode 100644 index 000000000000..2e776cd4e1c7 --- /dev/null +++ b/tests/run-with-compiler/i6171/Test_2.scala @@ -0,0 +1,7 @@ +object Test { + import scalatest._ + + def main(args: Array[String]): Unit = { + assert(new Some(5).get == 5L) + } +}