Skip to content
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

Java additions can silently break overloading extension methods #16743

Closed
soronpo opened this issue Jan 22, 2023 · 5 comments · Fixed by #17543
Closed

Java additions can silently break overloading extension methods #16743

soronpo opened this issue Jan 22, 2023 · 5 comments · Fixed by #17543
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement
Milestone

Comments

@soronpo
Copy link
Contributor

soronpo commented Jan 22, 2023

I used Java 11 for a long time and I had an extension method indent on String that indents a string.
Java 12 and above introduced its own indent method. As a result, my existing code broke, like demonstrated in the following code.

Compiler version

v3.2.2-RC2

Minimized code

extension (text: String)
  def indent: String = text

println("hello".indent)

Output

Under Java 12 and above:

Playground$$$Lambda$7955/0x00000008010fc800@4e7d4166

Expectation

Print hello or at least give a warning/error that overloading existing class methods via extension methods is not possible.

@soronpo soronpo added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 22, 2023
@sjrd
Copy link
Member

sjrd commented Jan 22, 2023

That's the way it is, I'm afraid. Backward source compatibility can never be promised in the presence of extension methods.

There could potentially be a warning at the definition of indent, but it would only happen when that extension method gets compiled with Java 12. The code that uses indent would never see the difference.

@odersky odersky added itype:enhancement area:reporting Error reporting including formatting, implicit suggestions, etc Spree Suitable for a future Spree and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 23, 2023
@odersky
Copy link
Contributor

odersky commented Jan 23, 2023

I also think we should warn when compiling an extension method for a value that already has a member with the same name; that's the only thing that can reasonably be done.

@nicolasstucki
Copy link
Contributor

A case that needs to be taken into account is the following

trait Foo:
  type X
  extension (text: X) def indent: String
trait Bar extends Foo:
  type X = String
  extension (text: X) def indent: String = text

In this case, we should not warn. This kind of cases can be found in scala.quoted.Quotes and QuotesImpl.

@dwijnand
Copy link
Member

indent isn't a "member" there anyways.

@som-snytt
Copy link
Contributor

There was one case exposed by the PR where extension (s: String) def isAbsolute was inadvertently refactored to Path.

Probably this happens more often than one might assume.

The converse use case is that the spurious extension is a backup for cross-compiling against old API that lacks the method.

@mbovel mbovel removed the Spree Suitable for a future Spree label Jul 4, 2023
smarter added a commit that referenced this issue Apr 9, 2024
Best effort to warn if defining extension method for a type which has a
non-private member of the same name and a subsuming signature.

Excludes opaque types (for defining forwarders to a public API) and
other alias types (where the type member may override an abstract type
member).

Fixes #16743
@Kordyjan Kordyjan added this to the 3.5.0 milestone May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants