diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 7623ed9bb634..b30fda7dc1ed 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -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 diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index 71eb03af3f89..7c21ddffef4f 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -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) + diff --git a/tests/neg/customArgs/pureStatement.scala b/tests/neg/customArgs/pureStatement.scala new file mode 100644 index 000000000000..ac1a2026aae1 --- /dev/null +++ b/tests/neg/customArgs/pureStatement.scala @@ -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 +}