File tree Expand file tree Collapse file tree 3 files changed +19
-1
lines changed
src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -1707,7 +1707,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
17071707 case Thicket (stats) :: rest =>
17081708 traverse(stats ++ rest)
17091709 case stat :: rest =>
1710- buf += typed(stat)(ctx.exprContext(stat, exprOwner))
1710+ val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
1711+ if ((ctx.owner.isType || rest.nonEmpty) && isPureExpr(stat1) && ! ctx.isAfterTyper)
1712+ ctx.warning(em " a pure expression does nothing in statement position " , stat1.pos)
1713+ buf += stat1
17111714 traverse(rest)
17121715 case nil =>
17131716 buf.toList
Original file line number Diff line number Diff line change @@ -163,6 +163,7 @@ class CompilationTests extends ParallelTesting {
163163 compileFile(" ../tests/neg/customArgs/noimports2.scala" , defaultOptions.and(" -Yno-imports" )) +
164164 compileFile(" ../tests/neg/customArgs/overloadsOnAbstractTypes.scala" , allowDoubleBindings) +
165165 compileFile(" ../tests/neg/customArgs/xfatalWarnings.scala" , defaultOptions.and(" -Xfatal-warnings" )) +
166+ compileFile(" ../tests/neg/customArgs/pureStatement.scala" , defaultOptions.and(" -Xfatal-warnings" )) +
166167 compileFile(" ../tests/neg/customArgs/phantom-overload.scala" , allowDoubleBindings) +
167168 compileFile(" ../tests/neg/tailcall/t1672b.scala" , defaultOptions) +
168169 compileFile(" ../tests/neg/tailcall/t3275.scala" , defaultOptions) +
Original file line number Diff line number Diff line change 1+ class IOCapability
2+
3+ object Test {
4+ // Forgot to mark `ev` implicit!
5+ def doSideEffects (x : Int )(ev : IOCapability ) = {
6+ println(" x: " + x)
7+ }
8+
9+ implicit val cap : IOCapability = new IOCapability
10+
11+ 2 // error: pure expression does nothing in statement position
12+
13+ doSideEffects(1 ) // error: pure expression does nothing in statement position
14+ }
You can’t perform that action at this time.
0 commit comments