Skip to content
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/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
case Thicket(stats) :: rest =>
traverse(stats ++ rest)
case stat :: rest =>
buf += typed(stat)(ctx.exprContext(stat, exprOwner))
val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
if (!ctx.isAfterTyper && isPureExpr(stat1))
ctx.warning(em"a pure expression does nothing in statement position", stat.pos)
buf += stat1
traverse(rest)
case nil =>
buf.toList
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class CompilationTests extends ParallelTesting {
compileFile("../tests/neg/customArgs/noimports2.scala", defaultOptions.and("-Yno-imports")) +
compileFile("../tests/neg/customArgs/overloadsOnAbstractTypes.scala", allowDoubleBindings) +
compileFile("../tests/neg/customArgs/xfatalWarnings.scala", defaultOptions.and("-Xfatal-warnings")) +
compileFile("../tests/neg/customArgs/pureStatement.scala", defaultOptions.and("-Xfatal-warnings")) +
compileFile("../tests/neg/customArgs/phantom-overload.scala", allowDoubleBindings) +
compileFile("../tests/neg/tailcall/t1672b.scala", defaultOptions) +
compileFile("../tests/neg/tailcall/t3275.scala", defaultOptions) +
Expand Down
28 changes: 28 additions & 0 deletions tests/neg/customArgs/pureStatement.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class IOCapability

object Test {
"" // error: pure expression does nothing in statement position

locally {
"" // error: pure expression does nothing in statement position

println("")

42 // error: pure expression does nothing in statement position

((x: Int) => println("hi")) // error: pure expression does nothing in statement position

()
}

// Forgot to mark `ev` implicit!
def doSideEffects(x: Int)(ev: IOCapability) = {
println("x: " + x)
}

implicit val cap: IOCapability = new IOCapability

2 // error: pure expression does nothing in statement position

doSideEffects(1) // error: pure expression does nothing in statement position
}