Rewrite with of "onAction = _ => println("x")" with "-source:3.7-migration" leads to invalid code
Closed this issue · 6 comments
Compiler version
"3.7.0-RC3"
Minimized code
This is the working code that I can minimize to. The code compiles file, but scalac option -source:3.7-migration generates invalid code
Original file file: ScalaFXHelloWorld.scala:
import scalafx.application.JFXApp3
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.control.Button
import scalafx.scene.layout.HBox
object ScalaFXHelloWorld extends JFXApp3:
override def start(): Unit =
stage = new JFXApp3.PrimaryStage:
title = "ScalaFX Hello World"
scene = new Scene:
content = new HBox:
padding = Insets(21)
children +=
new Button:
text = "Print Hello World!"
onAction = _ => println("Hello World!")File build.sbt
scalaVersion := "3.7.0-RC3"
libraryDependencies += "org.scalafx" %% "scalafx" % "23.0.1-R34"
scalacOptions ++= Seq(
"-rewrite",
"-source:3.7-migration",
)
// Fork a new JVM for 'run' and 'test:run' to avoid JavaFX double initialization problems
fork := trueOutput
When the code is build a warning is issued, but code copiles:
[warn] -- Warning: C:\tmp\myscalafxproj\src\main\scala\my\scalafx\ScalaFXHelloWorld.scala:20:27
[warn] 20 | onAction = _ => println("Hello World!")
[warn] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[warn] |Implicit parameters should be provided with a `using` clause.
[warn] |This code can be rewritten automatically under -rewrite -source 3.7-migration.
[warn] |To disable the warning, please use the following option:
[warn] | "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
[info] [patched file C:\tmp\myscalafxproj\src\main\scala\my\scalafx\ScalaFXHelloWorld.scala]
[warn] one warning found
The rewritten code, the last line changed, using was added:
package my.scalafx
import scalafx.application.JFXApp3
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.control.Button
import scalafx.scene.layout.HBox
object ScalaFXHelloWorld extends JFXApp3:
override def start(): Unit =
stage = new JFXApp3.PrimaryStage:
title = "ScalaFX Hello World"
scene = new Scene:
content = new HBox:
padding = Insets(21)
children +=
new Button:
text = "Print Hello World!"
onAction = using _ => println("Hello World!")This code will no longer compile.
Expectation
Re-generated code compiles without errors
See also: scalafx/scalafx#421
A simple SBT project is attaches
This duplicates #22731 but with the twist that the application is an assignment.
That is an unusual signature, so I don't know what the check should do.
class C:
def x: Runnable = ???
def x_=(implicit r: Runnable): Unit = ()
@main def test = println:
val c = C()
c.x = { () => () }
c.x_={ () => () }
c.x_=(using () => ())
Closing as duplicate because I see the fix already does the reasonable thing (nothing) except for the reasonable case of a regular method call with parens.
This is the patched:
c.x = { () => () }
c.x = () => ()
c.x_={() => ()}
c.x_=(using () => ())
I intended to add that it would be nice not to warn about assignment.
8 | c.x = { () => () }
| ^^^^^^^^^^^^
|Implicit parameters should be provided with a `using` clause.
It would be nicer not to have an implicit parameter for an assignment method.
@som-snytt there is still the same problem in 3.7.0-RC4. Code generated by "-rewrite -source 3.7-migration" is invalid and will not compile
