-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
03e4ab3
Add error message about double definition (#1589)
06c7a51
remove unused methods
d4455fd
fix error counts for tests/neg/singletonOrs
aa9ce63
WIP try to improve error messages
aa803ed
fix off by one error when printing line of previously declared symbol
68dcdec
don't show error about signatures when a var/val and a def are clashing
4df133e
call it declaration consistently
b6e1090
remove Signature.NoMatch from cases because it should not happen
5672fcf
remove redundant asTerm
c569c8a
fix grammar and wording
e4c64b7
remove double definition in singletonOr test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
trait A | ||
trait B | ||
|
||
// test with classes | ||
|
||
class Test1 { | ||
def foo(x: List[A]): Function1[A, A] = ??? | ||
def foo(x: List[B]): Function2[B, B, B] = ??? | ||
// ok, different jvm signature | ||
} | ||
|
||
class Test2 { | ||
def foo(x: List[A]): Function1[A, A] = ??? | ||
def foo(x: List[B]): Function1[B, B] = ??? // error: same jvm signature | ||
// scalac calls this "have same type after erasure" | ||
} | ||
|
||
class Test3 { | ||
// overload with same argument type, but different return types | ||
def foo(x: List[A]): Function1[A, A] = ??? | ||
def foo(x: List[A]): Function2[B, B, B] = ??? // error | ||
} | ||
|
||
class Test4 { | ||
val foo = 1 | ||
def foo = 2 // error | ||
} | ||
|
||
class Test4b { | ||
def foo = 2 | ||
val foo = 1 // error | ||
} | ||
|
||
class Test4c { | ||
def foo = 2 | ||
var foo = 1 // error | ||
} | ||
|
||
class Test4d { | ||
var foo = 1 | ||
def foo = 2 // error | ||
} | ||
|
||
|
||
// test with traits | ||
|
||
trait Test5 { | ||
def foo(x: List[A]): Function1[A, A] = ??? | ||
def foo(x: List[B]): Function2[B, B, B] = ??? | ||
// ok, different jvm signature | ||
} | ||
|
||
trait Test6 { | ||
def foo(x: List[A]): Function1[A, A] = ??? | ||
def foo(x: List[B]): Function1[B, B] = ??? // error: same jvm signature | ||
// scalac calls this "have same type after erasure" | ||
} | ||
|
||
trait Test7 { | ||
// overload with same argument type, but different return types | ||
def foo(x: List[A]): Function1[A, A] = ??? | ||
def foo(x: List[A]): Function2[B, B, B] = ??? // error | ||
} | ||
|
||
class Test8 { | ||
val foo = 1 | ||
def foo = 2 // error | ||
} | ||
|
||
class Test8b { | ||
def foo = 2 | ||
val foo = 1 // error | ||
} | ||
|
||
class Test8c { | ||
def foo = 2 | ||
var foo = 1 // error | ||
} | ||
|
||
class Test8d { | ||
var foo = 1 | ||
def foo = 2 // error | ||
} | ||
|
||
// test method and contructor argument clashing | ||
|
||
class Test9(val foo: Int) { | ||
def foo: String // error | ||
} | ||
|
||
class Test10(val foo: Int) { | ||
def foo: Int // error | ||
} | ||
|
||
abstract class Test11(val foo: Int) { | ||
def foo: String // error | ||
} | ||
|
||
abstract class Test12(val foo: Int) { | ||
def foo: Int // error | ||
} | ||
|
||
class Test13(var foo: Int) { | ||
def foo: String // error | ||
} | ||
|
||
class Test14(var foo: Int) { | ||
def foo: Int // error | ||
} | ||
|
||
abstract class Test15(var foo: Int) { | ||
def foo: String // error | ||
} | ||
|
||
abstract class Test16(var foo: Int) { | ||
def foo: Int // error | ||
} | ||
|
||
// don't error when shadowing | ||
|
||
class Test17 { | ||
val foo = 1 | ||
def bar() = { | ||
val foo = "" | ||
} | ||
} | ||
|
||
// no error when overloading | ||
|
||
class Test18 { | ||
def foo(a: A) = 1 | ||
def foo(b: B) = 1 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
object Test { | ||
def foo: 1 | 2 = 1 // error // error | ||
def bar: 3 | 4 = foo // error // error | ||
def foo: 1 | 2 = 1 // error // error | ||
def bar: 1 = foo | ||
def a: 1 | 2 = 1 // error // error | ||
def b: 3 | 4 = a // error // error | ||
def c: 1 | 2 = 1 // error // error | ||
def d: 1 = a | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You must not drop the info on matching type signatures. Either you preserve it or you figure out why it's there by walking
git blame
(which leads for instance to #597) and then realize you should preserve it.Methods can be overloaded, but ths code reject attempts at overloading that define methods with matching signatures.
For instance, for this code from #597 we need to explain users that this sort of overload is not allowed:
I think this code would make a good testcase.
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.
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 comment
The 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
decl.matches(other)
probably changed since #597 was filed. I did more experiments and asked around to figure this out.So, here is the current behavior:
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
isRealMethod
is tested, please still keep it).Here or in further PRs, we might want some clearer message there. Scalac gives:
Instead,
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:
This should arguably give a different error: