Skip to content

Commit ad4cea4

Browse files
committed
apply review
1 parent cac95ee commit ad4cea4

9 files changed

+75
-53
lines changed

src/librustc_resolve/lib.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -1248,15 +1248,6 @@ impl<'a> NameBinding<'a> {
12481248
}
12491249
}
12501250

1251-
fn is_renamed_extern_crate(&self) -> bool {
1252-
if let NameBindingKind::Import { directive, ..} = self.kind {
1253-
if let ImportDirectiveSubclass::ExternCrate(Some(_)) = directive.subclass {
1254-
return true;
1255-
}
1256-
}
1257-
false
1258-
}
1259-
12601251
fn is_glob_import(&self) -> bool {
12611252
match self.kind {
12621253
NameBindingKind::Import { directive, .. } => directive.is_glob(),
@@ -4783,10 +4774,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
47834774
};
47844775

47854776
let cm = self.session.source_map();
4786-
let rename_msg = "You can use `as` to change the binding name of the import";
4777+
let rename_msg = "you can use `as` to change the binding name of the import";
47874778

4788-
if let (Ok(snippet), false) = (cm.span_to_snippet(binding.span),
4789-
binding.is_renamed_extern_crate()) {
4779+
if let Ok(snippet) = cm.span_to_snippet(binding.span) {
47904780
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
47914781
format!("Other{}", name)
47924782
} else {
@@ -4796,20 +4786,22 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
47964786
err.span_suggestion_with_applicability(
47974787
binding.span,
47984788
rename_msg,
4799-
if snippet.contains(" as ") {
4800-
format!(
4801-
"{} as {}",
4789+
match (snippet.split_whitespace().find(|w| *w == "as"), snippet.ends_with(";")) {
4790+
(Some(_), false) => format!("{} as {}",
48024791
&snippet[..snippet.find(" as ").unwrap()],
48034792
suggested_name,
4804-
)
4805-
} else {
4806-
if snippet.ends_with(';') {
4807-
format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name)
4808-
} else {
4809-
format!("{} as {}", snippet, suggested_name)
4810-
}
4793+
),
4794+
(Some(_), true) => format!("{} as {};",
4795+
&snippet[..snippet.find(" as ").unwrap()],
4796+
suggested_name,
4797+
),
4798+
(None, false) => format!("{} as {}", snippet, suggested_name),
4799+
(None, true) => format!("{} as {};",
4800+
&snippet[..snippet.len() - 1],
4801+
suggested_name
4802+
),
48114803
},
4812-
Applicability::MachineApplicable,
4804+
Applicability::MaybeIncorrect,
48134805
);
48144806
} else {
48154807
err.span_label(binding.span, rename_msg);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod foo {
12+
pub struct A;
13+
pub struct B;
14+
}
15+
16+
use foo::{A, B as A};
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0252]: the name `A` is defined multiple times
2+
--> $DIR/issue-45829.rs:16:14
3+
|
4+
LL | use foo::{A, B as A};
5+
| - ^^^^^^ `A` reimported here
6+
| |
7+
| previous import of the type `A` here
8+
|
9+
= note: `A` must be defined only once in the type namespace of this module
10+
help: you can use `as` to change the binding name of the import
11+
|
12+
LL | use foo::{A, B as OtherA};
13+
| ^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0252`.

src/test/ui/issues/issue-45829/rename-extern-vs-use.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
// aux-build:issue_45829_b.rs
1212

13-
use std;
14-
extern crate issue_45829_b as std;
13+
mod foo {
14+
pub mod bar {}
15+
}
16+
17+
use foo::bar;
18+
extern crate issue_45829_b as bar;
1519

1620
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
error[E0259]: the name `std` is defined multiple times
2-
--> $DIR/rename-extern-vs-use.rs:14:1
1+
error[E0254]: the name `bar` is defined multiple times
2+
--> $DIR/rename-extern-vs-use.rs:18:1
33
|
4-
LL | extern crate issue_45829_b as std;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| `std` reimported here
8-
| You can use `as` to change the binding name of the import
4+
LL | use foo::bar;
5+
| -------- previous import of the module `bar` here
6+
LL | extern crate issue_45829_b as bar;
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `bar` reimported here
98
|
10-
= note: `std` must be defined only once in the type namespace of this module
11-
12-
error[E0254]: the name `std` is defined multiple times
13-
--> $DIR/rename-extern-vs-use.rs:13:5
14-
|
15-
LL | use std;
16-
| ^^^ `std` reimported here
9+
= note: `bar` must be defined only once in the type namespace of this module
10+
help: you can use `as` to change the binding name of the import
1711
|
18-
= note: `std` must be defined only once in the type namespace of this module
19-
help: You can use `as` to change the binding name of the import
12+
LL | extern crate issue_45829_b as other_bar;
2013
|
21-
LL | use std as other_std;
22-
| ^^^^^^^^^^^^^^^^
2314

24-
error: aborting due to 2 previous errors
15+
error: aborting due to previous error
2516

26-
Some errors occurred: E0254, E0259.
27-
For more information about an error, try `rustc --explain E0254`.
17+
For more information about this error, try `rustc --explain E0254`.

src/test/ui/issues/issue-45829/rename-extern.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ error[E0259]: the name `issue_45829_a` is defined multiple times
44
LL | extern crate issue_45829_a;
55
| --------------------------- previous import of the extern crate `issue_45829_a` here
66
LL | extern crate issue_45829_b as issue_45829_a;
7-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8-
| |
9-
| `issue_45829_a` reimported here
10-
| You can use `as` to change the binding name of the import
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `issue_45829_a` reimported here
118
|
129
= note: `issue_45829_a` must be defined only once in the type namespace of this module
10+
help: you can use `as` to change the binding name of the import
11+
|
12+
LL | extern crate issue_45829_b as other_issue_45829_a;
13+
|
1314

1415
error: aborting due to previous error
1516

src/test/ui/issues/issue-45829/rename-use-vs-extern.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use std as issue_45829_b;
77
| ^^^^^^^^^^^^^^^^^^^^ `issue_45829_b` reimported here
88
|
99
= note: `issue_45829_b` must be defined only once in the type namespace of this module
10-
help: You can use `as` to change the binding name of the import
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use std as other_issue_45829_b;
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/issues/issue-45829/rename-with-path.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use std::{collections::HashMap as A, sync::Arc as A};
77
| previous import of the type `A` here
88
|
99
= note: `A` must be defined only once in the type namespace of this module
10-
help: You can use `as` to change the binding name of the import
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use std::{collections::HashMap as A, sync::Arc as OtherA};
1313
| ^^^^^^^^^^^^^^^^^^^

src/test/ui/issues/issue-45829/rename.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use std as core;
77
| ^^^^^^^^^^^ `core` reimported here
88
|
99
= note: `core` must be defined only once in the type namespace of this module
10-
help: You can use `as` to change the binding name of the import
10+
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | use std as other_core;
1313
| ^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)