-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Detect duplicate implementations of assoc. types and constants. #25993
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Hmm, one thing that seems to be missing here is that it should be an error for any pair of associated items to have the same name (e.g. a constant and a method). I believe I have a fix for this on a branch of mine, but that branch is temporarily stalled (and a long way from master now), so I will have to see if I can actually offer it as an alternative. |
That gives an error in the trait (trait declarations already check that you don't have multiple trait-items with the same name, and attempts to declare impl-items of the wrong kind are of course rejected). |
I don't have a recent rustc on hand right now, but what about inherent impls? #![feature(associated_consts)]
struct Foo;
impl Foo {
const bar: bool = true;
fn bar() {}
} |
That does pose a problem :-). Seems like a job for resolve through. |
@arielb1 I'm not sure about that. If you refer to (All that said, code that follows the usual naming conventions will have uppercase names for constants and lowercase for methods, so this PR probably does cover the most important cases in practice.) |
Could you make the error for every 2 associated items, and add a test for the inherent impl case? I would change E201 for associated items in general (it was missed when associated types were added). |
@arielb1 I will take a look and see if I can figure out what to do. |
If the problem is just the error code, it's easy enough to factor out the error reporting into a helper method (and indeed, you could just use one big set to detect duplicates between kinds of items). |
35fd79a
to
ecaee08
Compare
I've changed it to correctly handle @quantheory 's example (and also added it as a test). Everything is under E0201 now as well. |
@nham sorry for being slow here -- I'm way being on reviewing but aiming to be keeping up from now. However, I think there is a slight issue here. Normally in Rust there are two namespaces: types and values. If I am reading the code right, this is reporting an error for something like: trait Foo {
type Bar;
fn Bar();
} I don't think this is correct. We should report an error if there are two constants/fns with the same name or two types, but between those two groups aliasing is legal. |
@nikomatsakis Ahh, interesting. Your example actually fails to compile (seems to be erroring out in resolve, see below), but you're right that if it did compile, my approach wouldn't work.
To address this I'm changing it to use separate HashSets for types and values. Do you see any issues with this approach? |
Seems good. Niko -------- Original message -------- From: Nick Hamann notifications@github.com Date:07/08/2015 17:32 (GMT-05:00) To: rust-lang/rust rust@noreply.github.com Cc: Niko Matsakis niko@alum.mit.edu Subject: Re: [rust] Detect duplicate implementations of assoc. types and
constants. (#25993) $ cat 25993.rs — |
@nham sorry, one nit, can you rename the tests to something like |
@nikomatsakis Done! Let me know if you'd like me to squash these commits. |
@nham looks good, r+. Squashing would be good though, yeah. |
Expands E0201 to be used for any duplicate associated items, not just duplicate methods/functions. It also correctly detects when two different kinds of items (like a constant and a method) have the same name. Fixes rust-lang#23969.
Squashed. |
@bors r+ thanks! |
📌 Commit 560bb0a has been approved by |
⌛ Testing commit 560bb0a with merge 8690536... |
⌛ Testing commit 560bb0a with merge 898e18d... |
(closing just because bors is being stubborn) |
@bors r+ |
📌 Commit 560bb0a has been approved by |
@bors r- |
@bors r=nikomatsakis |
📌 Commit 560bb0a has been approved by |
⌛ Testing commit 560bb0a with merge bab630e... |
@bors force |
@bors r=nikomatsakis |
📌 Commit 560bb0a has been approved by |
⌛ Testing commit 560bb0a with merge 5930c94... |
Adds two error codes, one for duplicate associated constants and one for types. I'm not certain these should each have their own code, but E0201 is already solely for duplicate associated functions so at least it kinda matches. This will lead to somewhat redundant error explanations, but that's nothing new! Fixes rust-lang#23969.
⛄ The build was interrupted to prioritize another pull request. |
⌛ Testing commit 560bb0a with merge d977c82... |
⛄ The build was interrupted to prioritize another pull request. |
⌛ Testing commit 560bb0a with merge 57dcda1... |
⛄ The build was interrupted to prioritize another pull request. |
Adds two error codes, one for duplicate associated constants and one for types. I'm not certain these should each have their own code, but E0201 is already solely for duplicate associated functions so at least it kinda matches. This will lead to somewhat redundant error explanations, but that's nothing new!
Fixes #23969.