Skip to content

Commit d97fc04

Browse files
committed
Fix #9751: Identify inlined trees and make Inline impure
1 parent ce48f5a commit d97fc04

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,8 +3629,13 @@ class Typer extends Namer
36293629
}
36303630

36313631
private def checkStatementPurity(tree: tpd.Tree)(original: untpd.Tree, exprOwner: Symbol)(using Context): Unit =
3632-
if (!tree.tpe.isErroneous && !ctx.isAfterTyper && isPureExpr(tree) &&
3633-
!tree.tpe.isRef(defn.UnitClass) && !isSelfOrSuperConstrCall(tree))
3632+
if !tree.tpe.isErroneous
3633+
&& !ctx.isAfterTyper
3634+
&& !tree.isInstanceOf[Inlined]
3635+
&& !tree.symbol.isAllOf(Inline | Param)
3636+
&& isPureExpr(tree)
3637+
&& !isSelfOrSuperConstrCall(tree)
3638+
then
36343639
report.warning(PureExpressionInStatementPosition(original, exprOwner), original.srcPos)
36353640

36363641
/** Types the body Scala 2 macro declaration `def f = macro <body>` */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def f(): Unit = {
2+
() // error
3+
()
4+
}
5+
6+
inline def g(): Unit = {
7+
() // error
8+
()
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Test {
2+
extension (x: Int)
3+
inline def times(inline op: Unit): Unit = {
4+
var count = 0
5+
while count < x do
6+
op
7+
count += 1
8+
}
9+
10+
10.times { println("hello") }
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
inline def f(inline x: Boolean): Unit =
3+
inline if x then println()
4+
5+
f(true)
6+
f(false)
7+
}

0 commit comments

Comments
 (0)