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

compiler should generate error "method foo1 in class C must be called with () argument" for inherited nullary method without arguments that has extra overloaded function in child class #21207

Open
unkarjedy opened this issue Jul 17, 2024 · 6 comments

Comments

@unkarjedy
Copy link
Contributor

unkarjedy commented Jul 17, 2024

Scala 3.3.3 (same with 3.5.1-RC1)

I am using the language specification at 1 & 2 & 3.
Use the code below:
(It might be convenient to use worksheet to see the evaluation result)

trait T { // NOTE: it can abstract class or class, it doesn't matter
  def foo1(): Int = 1
  def foo2(x: Int): Int = 33
}

class C extends T {
  def foo1(x: Int): Int = 11
  def foo2(): Int = 33

  def foo3(): Int = 2
  def foo3(x: Int): Int = 22
}

val c = new C
val x1 = c.foo1
val x2 = c.foo2
val x3 = c.foo3

Notice that c.foo1 compiles fine and evaluates to 1

However for c.foo2 and c.foo3 compiler generates a syntax error: method foo3 in class C must be called with () argument

image

According to spec the compiler should also generate an error for c.foo1:
method foo1 in class C must be called with () argument
The exclusion can be Java methods, but foo1 is not defined in Java and not overridden.

Excluded from this rule are methods that are defined in Java or that override methods defined in Java.

Note that if you remove def foo1(x: Int): Int = 11 then the error IS generated.
It seems like the check if foo1 has some parameters somehow interleaves with overloading resolution and is done before def foo1(x: Int): Int is discarded

@unkarjedy unkarjedy added the stat:needs triage Every issue needs to have an "area" and "itype" label label Jul 17, 2024
@unkarjedy
Copy link
Contributor Author

BTW, just in case, is https://www.scala-lang.org/files/archive/spec/3.4/06-expressions.html considered to be up-to date?

@Gedochao Gedochao added itype:bug area:overloading area:spec and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 17, 2024
@Gedochao
Copy link
Contributor

cc @sjrd

@mbovel mbovel added the Spree Suitable for a future Spree label Jul 21, 2024
@mbovel
Copy link
Member

mbovel commented Jul 21, 2024

This issue was picked for the Scala Issue Spree of Tuesday, July 23rd. @aherlihy, @EugeneFlesselle and @hamzaremmal will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

@EugeneFlesselle
Copy link
Contributor

EugeneFlesselle commented Jul 23, 2024

Stepping through adaptOverloded in both cases:

def adaptOverloaded(ref: TermRef) =


In the case of foo2:

  • We find the candidates: [(c.foo2 : (): A), (c.foo2 : (x: A): A)]
  • resolveOverloaded picks (c.foo2 : (): A), which we recursively try adapting
  • We reach adaptNoArgs
    • Which finds needsEta := true since pt.revealIgnored := <?>
    • Then adaptNoArgsUnappliedMethod(arity = -1)
      • Does not insert () since isAutoApplied returns false
      • Goes into missingArgs which reports the error

In the case of foo1:

  • We find the candidates: [(c.foo1 : (x: A): A), (c.foo1 : (): A)]
  • resolveOverloaded does not disambiguate
  • And therefore the following logic is applied, which inserts the (), the absence of which in source is therefore not reported
    case ambiAlts =>
    // If there are ambiguous alternatives, and:
    // 1. the types aren't erroneous
    // 2. the expected type is not a function type
    // 3. there exist a parameterless alternative
    //
    // Then, pick the parameterless alternative.
    // See tests/pos/i10715-scala and tests/pos/i10715-java.

We could try adding a 5th condition: that isAutoApplied is true.

@mbovel mbovel removed the Spree Suitable for a future Spree label Sep 23, 2024
@mbovel
Copy link
Member

mbovel commented Sep 23, 2024

What are the next steps to solve this issue? @EugeneFlesselle, should we do what you suggest above, or does that need to be further discussed?

@EugeneFlesselle
Copy link
Contributor

What are the next steps to solve this issue?

IIRC it would quite tricky to find a combination of conditions s.t.

  • we report warnings as expected in the submitted issue description,
  • without generating too many additional warnings in other existing code.

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

4 participants