Skip to content

Rewrite with of "onAction = _ => println("x")" with "-source:3.7-migration" leads to invalid code #23023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jpsacha opened this issue Apr 22, 2025 · 6 comments

Comments

@jpsacha
Copy link

jpsacha commented Apr 22, 2025

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 := true

Output

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

@jpsacha jpsacha added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 22, 2025
@jpsacha
Copy link
Author

jpsacha commented Apr 22, 2025

myscalafxproj.zip

@som-snytt
Copy link
Contributor

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 () => ())

@som-snytt
Copy link
Contributor

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 () => ())

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2025
@som-snytt
Copy link
Contributor

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 som-snytt added stat:duplicate and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 22, 2025
@jpsacha
Copy link
Author

jpsacha commented Apr 25, 2025

@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

@som-snytt
Copy link
Contributor

som-snytt commented Apr 25, 2025

@jpsacha I think 3.7.0 branch requires backporting from main (which is future 3.7.1). During RC release process, probably they balance risk and urgency to decide whether to do that.

on github, clicking on the commit shows what branch(es) it is on.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants