Skip to content

Don't forget side effects in prefixes of inlined function calls #12842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
for ((level, selfSym) <- sortedProxies) {
lazy val rhsClsSym = selfSym.info.widenDealias.classSymbol
val rhs = selfSym.info.dealias match
case info: TermRef if info.isStable =>
case info: TermRef
if info.isStable && (lastSelf.exists || isPureExpr(inlineCallPrefix)) =>
// If this is the first proxy, optimize to `ref(info)` only if call prefix is pure.
// Otherwise we might forget side effects. See run/i12829.scala.
ref(info)
case info =>
val rhsClsSym = info.widenDealias.classSymbol
Expand Down
2 changes: 2 additions & 0 deletions tests/run/i12829.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
abc
abc
19 changes: 19 additions & 0 deletions tests/run/i12829.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object Test:

class Foo:
var buf = ""

def add(v: String): Unit = buf += v

inline def += (inline v: String): this.type = {add(v); this }


def main(sa: Array[String]): Unit =

var fooVar = new Foo
fooVar += "a" += "b" += "c"
println(fooVar.buf) // Prints: abc

val fooVal = new Foo
fooVal += "a" += "b" += "c"
println(fooVal.buf) // Printed: c // Expected abc