Skip to content

Commit 81c8e83

Browse files
Merge pull request #12842 from dotty-staging/fix-12829
Don't forget side effects in prefixes of inlined function calls
2 parents 515cb9f + c082e2d commit 81c8e83

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,10 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
546546
for ((level, selfSym) <- sortedProxies) {
547547
lazy val rhsClsSym = selfSym.info.widenDealias.classSymbol
548548
val rhs = selfSym.info.dealias match
549-
case info: TermRef if info.isStable =>
549+
case info: TermRef
550+
if info.isStable && (lastSelf.exists || isPureExpr(inlineCallPrefix)) =>
551+
// If this is the first proxy, optimize to `ref(info)` only if call prefix is pure.
552+
// Otherwise we might forget side effects. See run/i12829.scala.
550553
ref(info)
551554
case info =>
552555
val rhsClsSym = info.widenDealias.classSymbol

tests/run/i12829.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
abc
2+
abc

tests/run/i12829.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object Test:
2+
3+
class Foo:
4+
var buf = ""
5+
6+
def add(v: String): Unit = buf += v
7+
8+
inline def += (inline v: String): this.type = {add(v); this }
9+
10+
11+
def main(sa: Array[String]): Unit =
12+
13+
var fooVar = new Foo
14+
fooVar += "a" += "b" += "c"
15+
println(fooVar.buf) // Prints: abc
16+
17+
val fooVal = new Foo
18+
fooVal += "a" += "b" += "c"
19+
println(fooVal.buf) // Printed: c // Expected abc

0 commit comments

Comments
 (0)