Skip to content

Add -quickfix compiler option to apply quickfixes to source files#10482

Merged
SethTisue merged 1 commit intoscala:2.13.xfrom
lrytz:quickfix
Aug 15, 2023
Merged

Add -quickfix compiler option to apply quickfixes to source files#10482
SethTisue merged 1 commit intoscala:2.13.xfrom
lrytz:quickfix

Conversation

@lrytz
Copy link
Member

@lrytz lrytz commented Aug 3, 2023

With 2.13.12 the compiler starts providing quick fixes with certain warnings and errors. Typically these are presented in IDEs, however it can also be practical to have the compiler directly patch the source files.

From -quickfix:help:

Apply quick fixes provided by the compiler for warnings and errors to source files.
Syntax: -quickfix:<filter>,...,<filter>

<filter> syntax is the same as for configurable warnings, see `-Wconf:help`. Examples:
  -quickfix:any                    apply all available quick fixes
  -quickfix:msg=Auto-application   apply quick fixes where the message contains "Auto-application"

Use `-Wconf:any:warning-verbose` to display applicable message filters with each warning.

@scala-jenkins scala-jenkins added this to the 2.13.13 milestone Aug 3, 2023
@lrytz lrytz modified the milestones: 2.13.13, 2.13.12 Aug 3, 2023
@lrytz lrytz force-pushed the quickfix branch 3 times, most recently from a7bd1db to 3505209 Compare August 3, 2023 19:33
With 2.13.12 the compiler starts providing quick fixes with certain
warnings and errors. Typically these are presented in IDEs, however
it can also be practical to have the compiler directly patch the source
files.

From `-quickfix:help`:

```
Apply quick fixes provided by the compiler for warnings and errors to source files.
Syntax: -quickfix:<filter>,...,<filter>

<filter> syntax is the same as for configurable warnings, see `-Wconf:help`. Examples:
  -quickfix:any                    apply all available quick fixes
  -quickfix:msg=Auto-application   apply quick fixes where the message contains "Auto-application"

Use `-Wconf:any:warning-verbose` to display applicable message filters with each warning.
```
@SethTisue SethTisue added prio:hi high priority (used only by core team, only near release time) release-notes worth highlighting in next release notes labels Aug 8, 2023
@SethTisue SethTisue changed the title Add -quickfix compiler option to apply quick fixes to source files Add -quickfix compiler option to apply quick fixes to source files Aug 13, 2023
@SethTisue SethTisue merged commit 86f40c2 into scala:2.13.x Aug 15, 2023
@SethTisue SethTisue removed the prio:hi high priority (used only by core team, only near release time) label Aug 23, 2023
@SethTisue SethTisue changed the title Add -quickfix compiler option to apply quick fixes to source files Add -quickfix compiler option to apply quickfixes to source files Aug 23, 2023
dongjoon-hyun pushed a commit to apache/spark that referenced this pull request Sep 30, 2023
### What changes were proposed in this pull request?
This pr aims to upgrade Scala from 2.13.11 to 2.13.12.

Additionally, this pr adds ``-Wconf:msg=legacy-binding:s`` to suppress similar compiler warnings as below:

```
[ERROR] /Users/yangjie01/SourceCode/git/spark-mine-13/core/src/main/scala/org/apache/spark/deploy/client/StandaloneAppClient.scala:171: reference to stop is ambiguous;
it is both defined in the enclosing class StandaloneAppClient and inherited in the enclosing class ClientEndpoint as method stop (defined in trait RpcEndpoint, inherited through parent trait ThreadSafeRpcEndpoint)
In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope.
Such references are ambiguous in Scala 3. To continue using the inherited symbol, write `this.stop`.
Or use `-Wconf:msg=legacy-binding:s` to silence this warning. [quickfixable]
Applicable -Wconf / nowarn filters for this fatal warning: msg=<part of the message>, cat=other, site=org.apache.spark.deploy.client.StandaloneAppClient.ClientEndpoint.receive
```

### Why are the changes needed?
The new version bring some regression fixes:
- scala/scala#10422
- scala/scala#10424

And a new feature: Quickfixes
```
For some errors and warnings, the compiler now suggests an edit that could fix the issue. Tooling such as IDEs can then offer the edits, or the compiler itself will make the change if run again with -quickfix.
```
- scala/scala#10406
- scala/scala#10482
- scala/scala#10484

The release notes as follows:
- https://github.com/scala/scala/releases/tag/v2.13.12

### Does this PR introduce _any_ user-facing change?
Yes, Scala version changed.

### How was this patch tested?
Pass Github

### Was this patch authored or co-authored using generative AI tooling?
NO

Closes #43185 from LuciferYang/SPARK-45331.

Authored-by: yangjie01 <yangjie01@baidu.com>
Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
rtyley added a commit to guardian/tagmanager that referenced this pull request Oct 22, 2024
…`()` is deprecated

Scala 2.13 deprecates (with PR scala/scala#8833) the old behaviour of Scala that zero-parameter methods could be called with either one or zero pairs of parenthesis - ie if you have a method `def foo()` you could call it as `foo()` or just `foo`. With Scala 3, you have to match the number of brackets *used in the method declaration* when you call it - so you'd _have_ to use `foo()` or you'd get an error like this:

```
[error] ~/code/presence-indicator/app/actor/OpenSocketActor.scala:79:7: Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method sender,
[error] or remove the empty argument list from its definition (Java-defined methods are exempt).
[error] In Scala 3, an unapplied method like this will be eta-expanded into a function. [quickfixable]
[error]       sender ! Map(serverId -> (LiveActors(connectionPing, subscription)))
[error]       ^
```

Java methods are exempt from this restriction - you can call either `hashCode()` (which, in Java, is how the method _has_ to be defined, with empty brackets) or just `hashCode` (which is how that method would have been declared if it was declared in Scala, in Scala methods with no side-effects should be declared without brackets: https://docs.scala-lang.org/style/method-invocation.html#arity-0).

## Automatically fixing this code issue

There are two possible ways of automating this code fix - in this small project, they both produce the same code changes:

### Fixing if the project is already on Scala 2.13 - use `-quickfix` in `scalac`

You can use the `-quickfix` support added to Scala 2.13.12 with scala/scala#10482:

Add either of these to the `scalacOptions` in `build.sbt`:

* `"-quickfix:any"`  ...to apply *all* available quick fixes
* `"-quickfix:msg=Auto-application"`  ...to apply quick fixes where the message contains "Auto-application"

Then run `compile` on the sbt console - the first compile will still fail, but it will subtly change the error message to say `[rewritten by -quickfix]` - your files have been edited to receive the fix:

```
[error] /Users/Roberto_Tyley/code/presence-indicator/app/actor/OpenSocketActor.scala:79:7: [rewritten by -quickfix] Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method sender,
[error] or remove the empty argument list from its definition (Java-defined methods are exempt).
[error] In Scala 3, an unapplied method like this will be eta-expanded into a function.
[error]       sender ! Map(serverId -> (LiveActors(connectionPing, subscription)))
[error]       ^
```

...run `compile` a second time, and compiler will be much happier.

Examples of other PRs using `-quickfix` to fix this code issue:

* guardian/ophan#5719

### Fixing while still on Scala 2.12 - use Scalafix

Fixing this everywhere in a project can be tedious, but thankfully there is a `ExplicitNonNullaryApply` Scalafix rule to fix this in the https://github.com/lightbend-labs/scala-rewrites project.

The Scalafix rule needs to be run while the project is still on Scala 2.12, not Scala 2.13 (otherwise sbt will say: "Error downloading ch.epfl.scala:sbt-scalafix;sbtVersion=1.0;scalaVersion=2.13:0.13.0").

Once the Scalafix plugin is made available to sbt (by adding `addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.13.0")`
to either `project/plugins.sbt` or `~/.sbt/1.0/plugins.sbt`), you can run these commands on the sbt prompt to automatically generate the changes in this PR:

```
scalafixEnable
scalafixAll dependency:fix.scala213.ExplicitNonNullaryApply@org.scala-lang:scala-rewrites:0.1.5
```

Examples of other PRs using Scalafix to fix this code issue:

* guardian/mobile-apps-api#2728
* guardian/presence-indicator#196

See also:

* scalacenter/scalafix#204
* lightbend-labs/scala-rewrites#14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes worth highlighting in next release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants