Skip to content

Clearer Error Message for Duplicate Definition #42076

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 1 commit into from
Jun 21, 2017

Conversation

alex-ozdemir
Copy link
Contributor

Clearer use of the error message and span labels to communicate duplication definitions/imports.

fixes #42061

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @arielb1 (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@Mark-Simulacrum
Copy link
Member

Could you give us a before/after comparison?

@@ -11,7 +11,7 @@
#![feature(collections)]

extern crate collections;
//~^ NOTE previous import of `collections` here
//~^ NOTE previous import of the extern crate `collections` here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should just be the crate, there's no such thing as an "extern crate."

Copy link
Contributor Author

@alex-ozdemir alex-ozdemir May 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind either way.

The original behavior was to regard this as an extern crate, although it was identified as such in the error header rather than the span label.

@alex-ozdemir
Copy link
Contributor Author

Of course!

Source (example.rs)

trait Foo { }
struct Foo { }

fn main() {}

Original message:

error[E0428]: a trait named `Foo` has already been defined in this module
 --> example.rs:2:1
  |
1 | trait Foo { }
  | ------------- previous definition of `Foo` here
2 | struct Foo { }
  | ^^^^^^^^^^^^^^ `Foo` already defined

error: aborting due to previous error

New message:

error[E0428]: the name `Foo` is defined twice in this module
 --> example.rs:2:1
  |
1 | trait Foo { }
  | ------------- previous definition of the trait `Foo` here
2 | struct Foo { }
  | ^^^^^^^^^^^^^^ `Foo` redefined here

error: aborting due to previous error

@alexcrichton alexcrichton added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 18, 2017
@alexcrichton
Copy link
Member

Thanks for the PR @alex-ozdemir! We'll make sure @arielb1 or another team member reviews this soon

@arielb1
Copy link
Contributor

arielb1 commented May 19, 2017

r? @nrc

@rust-highfive rust-highfive assigned nrc and unassigned arielb1 May 19, 2017
Copy link
Member

@nrc nrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, I especially like the changes to the notes on the second use. Two issues inline.

};

let msg = format!("the name `{}` is defined twice in this {}", name, container);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message would be inaccurate if the name were defined more than twice, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll change this.

false => "definition",
};

let new_participle = match binding.is_import() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming nits: why old_ and new_ here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should use this more consistently. The original (old) definition/import and the new one may have different participles/nouns.

@petrochenkov
Copy link
Contributor

petrochenkov commented May 24, 2017

The old message was suboptimal, but it tried to convey something about namespaces

a **type** named `X` has already been defined ...

The new message just says "name"

the **name** `X` is defined twice in this module

which is not strictly correct because several items in a module can have the same name if they are in different namespaces

type X = u8;
fn X() {}
macro X {}

Maybe extend the message to

the name `X` is defined twice in this module in type namespace

?

@@ -13,10 +13,10 @@
extern {
fn foo();

pub //~ ERROR a value named `foo` has already been defined
pub //~ ERROR E0428
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you keep using messages for testing and avoid error codes outside of E0XXX.rs tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing.

@alex-ozdemir
Copy link
Contributor Author

alex-ozdemir commented May 25, 2017

@petrochenkov: Good idea, will do.

Question: which is better?

the name `X` is bound multiple times in the {type,...} namespace of this {module,...}
the name `X` is defined multiple times in the {type,...} namespace of this {module,...}

The former is more correct (it could be any duplication of imports & definitions), but the word bound is less common.

@petrochenkov
Copy link
Contributor

@alex-ozdemir
I think "defined" should be kept.

@alex-ozdemir
Copy link
Contributor Author

alex-ozdemir commented May 26, 2017

Thanks for the feedback!

New error format:

error[E0428]: the name `Foo` is defined multiple times
 --> example.rs:2:1
  |
1 | trait Foo { }
  | ------------- previous definition of the trait `Foo` here
2 | struct Foo { }
  | ^^^^^^^^^^^^^^ `Foo` redefined here
  = note: `Foo` must be defined only once in the type namespace of this module

error: aborting due to previous error

@kennytm
Copy link
Member

kennytm commented May 26, 2017

The test compile-fail/proc-macro/shadow.rs failed, you need to update the test's error messages.

Error details
[00:39:59] ---- [compile-fail] compile-fail/proc-macro/shadow.rs stdout ----
[00:39:59] 	
[00:39:59] error: /checkout/src/test/compile-fail-fulldeps/proc-macro/shadow.rs:16: unexpected "error": '16:1: 16:23: the name `derive_a` is defined multiple times [E0259]'
[00:39:59] 
[00:39:59] error: /checkout/src/test/compile-fail-fulldeps/proc-macro/shadow.rs:16: expected error not found: the name `derive_a` is defined twice
[00:39:59] 
[00:39:59] error: 1 unexpected errors found, 1 expected errors not found
[00:39:59] status: exit code: 101
[00:39:59] command: /checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc /checkout/src/test/compile-fail-fulldeps/proc-macro/shadow.rs -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps --target=x86_64-unknown-linux-gnu --error-format json -L /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps/proc-macro/shadow.stage2-x86_64-unknown-linux-gnu.compile-fail.libaux -C prefer-dynamic -o /checkout/obj/build/x86_64-unknown-linux-gnu/test/compile-fail-fulldeps/proc-macro/shadow.stage2-x86_64-unknown-linux-gnu -Crpath -O -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers
[00:39:59] unexpected errors (from JSON output): [
[00:39:59]     Error {
[00:39:59]         line_num: 16,
[00:39:59]         kind: Some(
[00:39:59]             Error
[00:39:59]         ),
[00:39:59]         msg: "16:1: 16:23: the name `derive_a` is defined multiple times [E0259]"
[00:39:59]     }
[00:39:59] ]
[00:39:59] 
[00:39:59] not found errors (from test file): [
[00:39:59]     Error {
[00:39:59]         line_num: 16,
[00:39:59]         kind: Some(
[00:39:59]             Error
[00:39:59]         ),
[00:39:59]         msg: "the name `derive_a` is defined twice"
[00:39:59]     }
[00:39:59] ]
[00:39:59] 
[00:39:59] thread '[compile-fail] compile-fail/proc-macro/shadow.rs' panicked at 'explicit panic', /checkout/src/tools/compiletest/src/runtest.rs:1129
[00:39:59] note: Run with `RUST_BACKTRACE=1` for a backtrace.

@alex-ozdemir
Copy link
Contributor Author

Good spot!

@aidanhs
Copy link
Member

aidanhs commented Jun 1, 2017

I've pinged @nrc on IRC to get a second review pass for this PR.

@nrc
Copy link
Member

nrc commented Jun 1, 2017

r = me with commits squashed. Thanks!

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 4, 2017
@arielb1
Copy link
Contributor

arielb1 commented Jun 6, 2017

@alex-ozdemir

We're just waiting for you to squash the commits. Can you get that done so we can finish working on this PR?

@alex-ozdemir alex-ozdemir force-pushed the master branch 4 times, most recently from c7e4cdb to 87c8bf7 Compare June 8, 2017 11:45
@alex-ozdemir
Copy link
Contributor Author

Okay, sorry for the delay. I had forgotten that Rust doesn't use the standard PR workflow that allows y'all to squash & merge on your own.

Assuming I squashed correctly the integration tests should pass again, and we'll be good to go!

@Mark-Simulacrum
Copy link
Member

Looks like the squash possibly introduced some problems, could you take a look and fix those please? https://travis-ci.org/rust-lang/rust/jobs/240754613#L786

@alex-ozdemir
Copy link
Contributor Author

Whoops!

All good now.

@Mark-Simulacrum
Copy link
Member

@bors r=nrc

@bors
Copy link
Collaborator

bors commented Jun 15, 2017

📌 Commit 89bd87e has been approved by nrc

@bors
Copy link
Collaborator

bors commented Jun 15, 2017

🔒 Merge conflict

@bors
Copy link
Collaborator

bors commented Jun 15, 2017

☔ The latest upstream changes (presumably #42648) made this pull request unmergeable. Please resolve the merge conflicts.

Clearer use of the error message and span labels to communicate
duplicaiton defitions/imports.

New error format:

```
error[E0428]: the name `Foo` is defined twice
 --> example.rs:2:1
  |
1 | trait Foo { }
  | ------------- previous definition of the trait `Foo` here
2 | struct Foo { }
  | ^^^^^^^^^^^^^^ `Foo` redefined here
  = note: `Foo` must be defined only once in the type namespace of this module

error: aborting due to previous error
```
@alex-ozdemir
Copy link
Contributor Author

I believe the merge conflicts are resolved.

@arielb1 arielb1 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 20, 2017
@arielb1
Copy link
Contributor

arielb1 commented Jun 20, 2017

Looks like this PR fell between the chairs a bit. Currently waiting for review by @nrc.

@nrc
Copy link
Member

nrc commented Jun 20, 2017

@bors: r+

@bors
Copy link
Collaborator

bors commented Jun 20, 2017

📌 Commit a82890e has been approved by nrc

@nrc nrc added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 20, 2017
@bors
Copy link
Collaborator

bors commented Jun 21, 2017

⌛ Testing commit a82890e with merge bb14389...

bors added a commit that referenced this pull request Jun 21, 2017
Clearer Error Message for Duplicate Definition

Clearer use of the error message and span labels to communicate duplication definitions/imports.

fixes #42061
@bors
Copy link
Collaborator

bors commented Jun 21, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: nrc
Pushing bb14389 to master...

@bors bors merged commit a82890e into rust-lang:master Jun 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

trait/struct name conflict error confusingly worded
10 participants