-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add error message about double definition (#1589) #4064
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
Changes from 1 commit
03e4ab3
06c7a51
d4455fd
aa9ce63
aa803ed
68dcdec
4df133e
b6e1090
5672fcf
c569c8a
e4c64b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -627,7 +627,7 @@ trait Checking { | |
def explanation = | ||
if (!decl.isRealMethod) "" | ||
else "\n(the definitions have matching type signatures)" | ||
ctx.error(em"$decl is already defined as $other$ofType$explanation", decl.pos) | ||
ctx.error(DoubleDeclaration(decl, other), decl.pos) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You must not drop the info on matching type signatures. Either you preserve it or you figure out why it's there by walking For instance, for this code from #597 we need to explain users that this sort of overload is not allowed: trait A
trait B
class Test {
def foo(x: List[A]): Function1[A, A] = ???
def foo(x: List[B]): Function2[B, B, B] = ??? //error
} I think this code would make a good testcase. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your feedback. If I understand correctly, the code given in #597 should not compile. I added a test case in tests/neg/, but the tests failed because it compiles successfully. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh! I just wanted to say "please don't remove the message", but I should still have tried. The behavior of So, here is the current behavior: scala> class Test {
def foo(x: List[A]): Function1[A, A] = ???
def foo(x: List[B]): Function1[B, B] = ??? // error
}
3 | def foo(x: List[B]): Function1[B, B] = ??? // error
| ^
| method foo is already defined as method foo: (x: List[A]): A => A
| (the definitions have matching type signatures) That code fails because, while the overload is "acceptable" (you can tell at call site which one to call, I forget the right word), the two functions erase to the same "signature" (which here means JVM signature, according to @smarter). The regression I was observing is that here "the definitions have matching type signatures" is important. I'll accept this PR as soon as that message is restored with the same check (I'm not sure why Here or in further PRs, we might want some clearer message there. Scalac gives: <console>:15: error: double definition:
def foo(x: List[A]): A => A at line 14 and
def foo(x: List[B]): B => B at line 15
have same type after erasure: (x: List)Function1
def foo(x: List[B]): Function1[B, B] = ??? // error Instead, class Test {
def foo(x: List[A]): Function1[A, A] = ???
def foo(x: List[B]): Function2[B, B, B] = ??? //error
} is supported because the overload is acceptable and they erase to different JVM signatures (because of the different return types). Finally, this code is rejected because this overload isn't acceptable: scala> class Test {
def foo(x: List[A]): Function1[A, A] = ???
def foo(x: List[A]): Function2[B, B, B] = ??? // error
}
3 | def foo(x: List[A]): Function2[B, B, B] = ??? // error
| ^
| method foo is already defined as method foo: (x: List[A]): A => A
| (the definitions have matching type signatures) This should arguably give a different error: scala> class Test {
| def foo(x: List[A]): Function1[A, A] = ???
| def foo(x: List[A]): Function2[B, B, B] = ??? // error
| }
<console>:15: error: method foo is defined twice;
the conflicting method foo was defined at line 14:16
def foo(x: List[A]): Function2[B, B, B] = ??? // error
^ |
||
} | ||
if (decl is Synthetic) doubleDefError(other, decl) | ||
else doubleDefError(decl, other) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe keep using
explanation
in the DoubleDeclaration class, or movedef explanation
there?