Skip to content

Commit e4735d2

Browse files
Merge pull request #5189 from dotty-staging/fix-#5119
Fix #5119: Get underlying argument for synthetic local values
2 parents 3379e4b + bf4faef commit e4735d2

File tree

9 files changed

+64
-9
lines changed

9 files changed

+64
-9
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -942,15 +942,17 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
942942
}
943943
}
944944

945-
/** Map Inlined nodes and InlineProxy references to underlying arguments */
945+
/** Map Inlined nodes, InlineProxy references and Synthetic val references to underlying arguments */
946946
object mapToUnderlying extends TreeMap {
947947
override def transform(tree: Tree)(implicit ctx: Context): Tree = tree match {
948-
case tree: Ident if tree.symbol.is(InlineProxy) =>
949-
tree.symbol.defTree.asInstanceOf[ValOrDefDef].rhs.underlyingArgument
950-
case Inlined(_, _, arg) =>
951-
arg.underlyingArgument
952-
case tree =>
953-
super.transform(tree)
948+
case tree: Ident if tree.symbol.is(InlineProxy) || (tree.symbol.is(Synthetic) && !tree.symbol.owner.isClass) =>
949+
tree.symbol.defTree match {
950+
case defTree: ValOrDefDef => transform(defTree.rhs)
951+
case _ => tree
952+
}
953+
case Inlined(_, _, arg) => transform(arg)
954+
case NamedArg(_, arg) => transform(arg)
955+
case tree => super.transform(tree)
954956
}
955957
}
956958

compiler/test/dotc/run-test-pickling.blacklist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ i4803d
2020
i4803e
2121
i4803f
2222
i4947b
23+
i5119
24+
i5119b
2325
inline-varargs-1
2426
implicitShortcut
2527
lazy-implicit-lists.scala

tests/run/i5119.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Term.Select(Term.Apply(Term.Select(Term.New(TypeTree.TypeIdent("StringContextOps")), "<init>", Some(Signature(List(scala.Function0), Macro$.StringContextOps))), List(Term.Apply(Term.Select(Term.Select(Term.Select(Term.Ident("_root_"), "scala", None), "StringContext", None), "apply", Some(Signature(List(scala.collection.Seq), scala.StringContext))), List(Term.Typed(Term.Repeated(List(Term.Literal(Constant.String("Hello World ")), Term.Literal(Constant.String("!")))), TypeTree.Synthetic()))))), "inline$sc", Some(Signature(Nil, scala.StringContext)))
2+
Term.Typed(Term.Repeated(List(Term.Literal(Constant.Int(1)))), TypeTree.Synthetic())

tests/run/i5119/Macro_1.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted._
2+
import scala.tasty.Tasty
3+
4+
object Macro {
5+
class StringContextOps(sc: => StringContext) {
6+
inline def ff(args: => Any*): String = ~Macro.impl('(sc), '(args))
7+
}
8+
implicit inline def XmlQuote(sc: => StringContext): StringContextOps = new StringContextOps(sc)
9+
def impl(sc: Expr[StringContext], args: Expr[Seq[Any]])(implicit tasty: Tasty): Expr[String] = {
10+
import tasty._
11+
(sc.toTasty.underlyingArgument.show + "\n" + args.toTasty.underlyingArgument.show).toExpr
12+
}
13+
}

tests/run/i5119/Main_2.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
object Test {
3+
import Macro._
4+
5+
def main(args: Array[String]): Unit = {
6+
println(ff"Hello World ${1}!")
7+
}
8+
}

tests/run/i5119b.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Term.Apply(Term.Ident("foo"), List(Term.Literal(Constant.Int(1))))
2+
Term.Apply(Term.Ident("foo"), List(Term.Literal(Constant.Int(2))))
3+
Term.Apply(Term.Ident("foo"), List(Term.Literal(Constant.Int(4))))
4+
Term.Apply(Term.Ident("foo"), List(Term.Literal(Constant.Int(3))))

tests/run/i5119b/Macro_1.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted._
2+
import scala.tasty.Tasty
3+
4+
object Macro {
5+
6+
inline def ff(arg1: Any, arg2: Any): String = ~Macro.impl('(arg1), '(arg2))
7+
8+
def impl(arg1: Expr[Any], arg2: Expr[Any])(implicit tasty: Tasty): Expr[String] = {
9+
import tasty._
10+
(arg1.toTasty.underlyingArgument.show + "\n" + arg2.toTasty.underlyingArgument.show).toExpr
11+
}
12+
13+
}

tests/run/i5119b/Main_2.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
object Test {
3+
import Macro._
4+
5+
def main(args: Array[String]): Unit = {
6+
println(ff(arg1 = foo(1), arg2 = foo(2)))
7+
println(ff(arg2 = foo(3), arg1 = foo(4)))
8+
}
9+
10+
def foo(x: Any): Any = ()
11+
}

tests/run/xml-interpolation/Test_2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import XmlQuote._
22

33
object Test {
44
def main(args: Array[String]): Unit = {
5-
// TODO: enable once #5119 is fixed
6-
// assert(xml"Hello Allan!" == Xml("Hello Allan!", Nil))
5+
6+
assert(xml"Hello Allan!" == Xml("Hello Allan!", Nil))
77

88
val name = new Object{}
99
assert(xml"Hello $name!" == Xml("Hello ??!", List(name)))

0 commit comments

Comments
 (0)