Skip to content

Overloaded methods with default arguments yield pointless error message #12245

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
ansvonwa opened this issue Apr 28, 2021 · 0 comments · Fixed by #12276
Closed

Overloaded methods with default arguments yield pointless error message #12245

ansvonwa opened this issue Apr 28, 2021 · 0 comments · Fixed by #12276
Milestone

Comments

@ansvonwa
Copy link
Member

Compiler version

3.0.0-RC3

Minimized code

def round(f: Float, digits: Int = 0): Float = ???
//@scala.annotation.targetName("roundDouble") // does not change anything
def round(d: Double, digits: Int = 0): Double = ???

Output

$ scalac Main.scala -Xprint:typer
-- Error: Main.scala:4:6 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4 |  def round(d: Double, digits: Int = 0): Double = ???
  |      ^
  |      two or more overloaded variants of method round have default arguments
-- [E120] Naming Error: Main.scala:4:37 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4 |  def round(d: Double, digits: Int = 0): Double = ???
  |                                     ^
  |                                     Double definition:
  |                                     def round$default$2: => Int in package object Main$package at line 2 and
  |                                     def round$default$2: => Int in package object Main$package at line 4
  |                                     have the same type after erasure.
  |
  |                                     Consider adding a @targetName annotation to one of the conflicting definitions
  |                                     for disambiguation.
2 errors found

Expectation

The code should compile.
If that is not possible because the names clash (as the first error suggests), it should compile with the @targetName given.

In no case should the compiler propose to annotate methods it generated itself. (def round$default$2)

Comments

If I add @targetName to one of the round methods, the output stays exactly the same.

result of Main.scala after typer:
package <empty> {
  final lazy module val Main$package: Main$package$ = new Main$package$()
  final module class Main$package$() extends Object() { this: Main$package.type =>
    def round(f: Float, digits: Int): Float = ???
    def round$default$2: Int = 0
    @scala.annotation.targetName("roundDouble") def round(d: Double, digits: Int): Double = ???
    def round$default$2: Int = 0
  }
}

Maybe the problem could be solved if the generated method names would use the given target name?

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

Successfully merging a pull request may close this issue.

2 participants