Skip to content

Commit f7452c5

Browse files
Merge pull request #6180 from dotty-staging/refactor-map-underlying
Refactor MapUnderlying and add docs
2 parents 1636a25 + d6915a4 commit f7452c5

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
971971
/** Replace Inlined nodes and InlineProxy references to underlying arguments */
972972
def underlyingArgument(implicit ctx: Context): Tree = {
973973
val mapToUnderlying = new MapToUnderlying {
974-
override def skipLocal(sym: Symbol): Boolean =
974+
/** Should get the rhs of this binding
975+
* Returns true if the symbol is a val or def generated by eta-expansion/inline
976+
*/
977+
override protected def skipLocal(sym: Symbol): Boolean =
975978
sym.is(InlineProxy) || sym.is(Synthetic)
976979
}
977980
mapToUnderlying.transform(tree)
@@ -1018,17 +1021,28 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
10181021
*/
10191022
class MapToUnderlying extends TreeMap {
10201023
override def transform(tree: Tree)(implicit ctx: Context): Tree = tree match {
1021-
case tree: Ident if tree.symbol.exists && !tree.symbol.owner.isClass && !tree.symbol.isAnonymousFunction && skipLocal(tree.symbol) =>
1024+
case tree: Ident if isBinding(tree.symbol) && skipLocal(tree.symbol) =>
10221025
tree.symbol.defTree match {
1023-
case defTree: ValOrDefDef if !defTree.rhs.isEmpty => transform(defTree.rhs)
1026+
case defTree: ValOrDefDef =>
1027+
val rhs = defTree.rhs
1028+
assert(!rhs.isEmpty)
1029+
transform(rhs)
10241030
case _ => tree
10251031
}
10261032
case Inlined(_, _, arg) => transform(arg)
10271033
case Block(Nil, arg) => transform(arg)
10281034
case NamedArg(_, arg) => transform(arg)
10291035
case tree => super.transform(tree)
10301036
}
1031-
def skipLocal(sym: Symbol): Boolean = true
1037+
1038+
/** Should get the rhs of this binding */
1039+
protected def skipLocal(sym: Symbol): Boolean = true
1040+
1041+
/** Is this a symbol that of a local val or parameterless def for which we could get the rhs */
1042+
private def isBinding(sym: Symbol)(implicit ctx: Context): Boolean = {
1043+
sym.isTerm && !sym.is(Param) && !sym.owner.isClass &&
1044+
!(sym.is(Method) && sym.info.isInstanceOf[MethodOrPoly]) // if is a method it is parameterless
1045+
}
10321046
}
10331047

10341048
implicit class ListOfTreeDecorator(val xs: List[tpd.Tree]) extends AnyVal {

tests/run/tasty-argument-tree-1.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ tree deref. vals: Term.Ident("a")
1717
tree: Term.Inlined(None, Nil, Term.Ident("x"))
1818
tree deref. vals: Term.Ident("b")
1919

20+
tree: Term.Inlined(None, Nil, Term.Ident("x"))
21+
tree deref. vals: Term.Apply(Term.Ident("d2"), Nil)
22+
23+
tree: Term.Inlined(None, Nil, Term.Ident("x"))
24+
tree deref. vals: Term.Apply(Term.Ident("d3"), List(Term.Literal(Constant.Int(3))))
25+
26+
tree: Term.Inlined(None, Nil, Term.Ident("x"))
27+
tree deref. vals: Term.TypeApply(Term.Ident("d4"), List(TypeTree.Ident("Int")))
28+
2029
tree: Term.Inlined(None, Nil, Term.Ident("vv"))
2130
tree deref. vals: Term.Literal(Constant.Int(1))
2231

tests/run/tasty-argument-tree-1/quoted_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ object Test {
99
val v: Int = 1
1010
def d: Int = 2
1111
lazy val l: Int = 3
12+
def d2(): Int = 2
13+
def d3(a: Int): Int = a
14+
def d4[T]: Int = 2
1215
inspect(3)
1316
inspect(v)
1417
inspect(d)
1518
inspect(l)
1619
inspect(a)
1720
inspect(b)
21+
inspect(d2())
22+
inspect(d3(3))
23+
inspect(d4[Int])
1824

1925
val vv = v
2026
def dv = v

0 commit comments

Comments
 (0)