scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.79k stars 1.04k forks source link

Eta expansion undesirable when result will be immediately discarded #11761

Closed japgolly closed 3 years ago

japgolly commented 3 years ago

Compiler version

3.0.0-RC1

Minimized code

def go(x: Int): Unit =
  go

Output

[warn] -- [E129] Potential Issue Warning: /home/golly/projects/public/univeq/univeq/shared/src/main/scala-3/japgolly/univeq/internal/bug.scala:2:2 
[warn] 2 |  go
[warn]   |  ^^
[warn]   |A pure expression does nothing in statement position; you may be omitting necessary parentheses

Expectation

The code shouldn't compile. The go function requires an explicit x argument. It's probably converting go to a function literal which is normally fine, but doesn't make sense if its the entirely of the statement and not a return value.

(Also keep in mind that this is the minimised case, the real case I encountered was much more confusing and dangerous.)

abgruszecki commented 3 years ago

@japgolly was a warning emitted in the real case you encountered? If not, then that's a clear bug.

Reg. this particular issue - pure expressions in statement positions are, in my experience, almost always completely unintentional, so maybe we could make the warning an error. It might make sense to emit a special error on eta-expansion, but that feels a bit too specific for me.

som-snytt commented 3 years ago

I commented on the PR that the optimal behavior might be different for a REPL, where you do typically ask, What does this eta-expand to? Or does it even? Thread.sleep.

odersky commented 3 years ago

But I think repls embed in a val definition anyway, so that should be ok