Skip to content

Commit 8a7d076

Browse files
committed
Fix #9342: Use semantic name for inline bindings
1 parent 5cdfd31 commit 8a7d076

10 files changed

+45
-23
lines changed

compiler/src/dotty/tools/dotc/core/NameKinds.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ object NameKinds {
295295
val DocArtifactName: UniqueNameKind = new UniqueNameKind("$doc")
296296
val UniqueInlineName: UniqueNameKind = new UniqueNameKind("$i")
297297
val InlineScrutineeName: UniqueNameKind = new UniqueNameKind("$scrutinee")
298-
val InlineBinderName: UniqueNameKind = new UniqueNameKind("$elem")
298+
val InlineBinderName: UniqueNameKind = new UniqueNameKind("$")
299299

300300
/** A kind of unique extension methods; Unlike other unique names, these can be
301301
* unmangled.

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
421421
val (bindingFlags, bindingType) =
422422
if (isByName) (inlineFlags, ExprType(argtpe.widen))
423423
else (inlineFlags, argtpe.widen)
424-
val boundSym = newSym(name, bindingFlags, bindingType).asTerm
424+
val boundSym = newSym(InlineBinderName.fresh(name.asTermName), bindingFlags, bindingType).asTerm
425425
val binding = {
426426
if (isByName) DefDef(boundSym, arg.changeOwner(ctx.owner, boundSym))
427427
else ValDef(boundSym, arg)

tests/pos/i9342a.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
inline def label(x: Int, inline g: Int => String): String = g(x)
2+
def f: Int => String = ???
3+
def label2(g: Int) = label(g, f)

tests/pos/i9342b.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait Label[A]:
2+
def apply(v: A): String
3+
4+
given [A] as Label[A] = _.toString
5+
6+
extension [A](x: A) inline def label(using inline l: Label[A]): String = l(x)
7+
8+
def label1[A](v: A) = v.label
9+
10+
def label2[A](l: A) = l.label

tests/pos/i9342c.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
trait Label[A]:
2+
def apply(v: A): String
3+
4+
def g[A]: Label[A] = _.toString
5+
6+
inline def label[A](x: A, inline l: Label[A]): String = l(x)
7+
8+
def label1[A](v: A) = label(v, g)
9+
def label2[A](l: A) = label(l, g)

tests/run-macros/quote-inline-function.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ Normal function
44
val j: scala.Int = 5
55
while (i.<(j)) {
66
val x$1: scala.Int = i
7-
f.apply(x$1)
7+
f$1.apply(x$1)
88
i = i.+(1)
99
}
1010
while ({
1111
val x$2: scala.Int = i
12-
f.apply(x$2)
12+
f$1.apply(x$2)
1313
i = i.+(1)
1414
i.<(j)
1515
}) ()
@@ -21,12 +21,12 @@ By name function
2121
val j: scala.Int = 5
2222
while (i.<(j)) {
2323
val x$3: scala.Int = i
24-
f.apply(x$3)
24+
f$2.apply(x$3)
2525
i = i.+(1)
2626
}
2727
while ({
2828
val x$4: scala.Int = i
29-
f.apply(x$4)
29+
f$2.apply(x$4)
3030
i = i.+(1)
3131
i.<(j)
3232
}) ()

tests/run-macros/quote-matching-optimize-1.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ Result: ()
1818

1919
Original: scala.List.apply[scala.Int](1, 2, 3).map[scala.Int](((a: scala.Int) => a.*(2))).map[java.lang.String](((b: scala.Int) => b.toString()))
2020
Optimized: scala.List.apply[scala.Int](1, 2, 3).map[java.lang.String](((x: scala.Int) => {
21-
val x$1: scala.Int = x.*(2)
22-
x$1.toString()
21+
val x$6: scala.Int = x.*(2)
22+
x$6.toString()
2323
}))
2424
Result: List(2, 4, 6)
2525

2626
Original: scala.List.apply[scala.Int](55, 67, 87).map[scala.Char](((a: scala.Int) => a.toChar)).map[java.lang.String](((b: scala.Char) => b.toString()))
2727
Optimized: scala.List.apply[scala.Int](55, 67, 87).map[java.lang.String](((x: scala.Int) => {
28-
val x$2: scala.Char = x.toChar
29-
x$2.toString()
28+
val x$8: scala.Char = x.toChar
29+
x$8.toString()
3030
}))
3131
Result: List(7, C, W)
3232

tests/run-macros/quote-matching-optimize-2.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ Result: ()
1818

1919
Original: ls.map[scala.Int](((a: scala.Int) => a.*(2))).map[java.lang.String](((b: scala.Int) => b.toString()))
2020
Optimized: ls.map[java.lang.String](((x: scala.Int) => {
21-
val x$1: scala.Int = x.*(2)
22-
x$1.toString()
21+
val x$6: scala.Int = x.*(2)
22+
x$6.toString()
2323
}))
2424
Result: List(2, 4, 6)
2525

2626
Original: ls.map[scala.Char](((a: scala.Int) => a.toChar)).map[java.lang.String](((b: scala.Char) => b.toString()))
2727
Optimized: ls.map[java.lang.String](((x: scala.Int) => {
28-
val x$2: scala.Char = x.toChar
29-
x$2.toString()
28+
val x$8: scala.Char = x.toChar
29+
x$8.toString()
3030
}))
3131
Result: List(, , )
3232

tests/run-macros/quoted-matching-docs.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
6
33
10.+(Test.a)
44
15
5-
args.sum[scala.Int](scala.math.Numeric.IntIsIntegral)
5+
args$5.sum[scala.Int](scala.math.Numeric.IntIsIntegral)
66
9

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tree deref. vals: Literal(Constant(3))
55
tree: Inlined(None, Nil, Ident("v"))
66
tree deref. vals: Literal(Constant(1))
77

8-
tree: Inlined(None, Nil, Ident("x"))
8+
tree: Inlined(None, Nil, Ident("x$1"))
99
tree deref. vals: Literal(Constant(2))
1010

1111
tree: Inlined(None, Nil, Ident("l"))
@@ -14,29 +14,29 @@ tree deref. vals: Literal(Constant(3))
1414
tree: Inlined(None, Nil, Ident("a"))
1515
tree deref. vals: Ident("a")
1616

17-
tree: Inlined(None, Nil, Ident("x"))
17+
tree: Inlined(None, Nil, Ident("x$2"))
1818
tree deref. vals: Ident("b")
1919

20-
tree: Inlined(None, Nil, Ident("x"))
20+
tree: Inlined(None, Nil, Ident("x$3"))
2121
tree deref. vals: Apply(Ident("d2"), Nil)
2222

23-
tree: Inlined(None, Nil, Ident("x"))
23+
tree: Inlined(None, Nil, Ident("x$4"))
2424
tree deref. vals: Apply(Ident("d3"), List(Literal(Constant(3))))
2525

26-
tree: Inlined(None, Nil, Ident("x"))
26+
tree: Inlined(None, Nil, Ident("x$5"))
2727
tree deref. vals: TypeApply(Ident("d4"), List(TypeIdent("Int")))
2828

2929
tree: Inlined(None, Nil, Ident("vv"))
3030
tree deref. vals: Literal(Constant(1))
3131

32-
tree: Inlined(None, Nil, Ident("x"))
32+
tree: Inlined(None, Nil, Ident("x$6"))
3333
tree deref. vals: Literal(Constant(1))
3434

3535
tree: Inlined(None, Nil, Ident("vd"))
3636
tree deref. vals: Literal(Constant(2))
3737

38-
tree: Inlined(None, Nil, Ident("x"))
38+
tree: Inlined(None, Nil, Ident("x$7"))
3939
tree deref. vals: Literal(Constant(2))
4040

41-
tree: Inlined(None, Nil, Ident("x"))
41+
tree: Inlined(None, Nil, Ident("x$8"))
4242
tree deref. vals: Apply(TypeApply(Select(Ident("Tuple2"), "apply"), List(Inferred(), Inferred())), List(Literal(Constant(1)), Literal(Constant(2))))

0 commit comments

Comments
 (0)