Skip to content

Commit 7046a40

Browse files
committed
Auto merge of #47767 - estebank:as-suggestion, r=petrochenkov
Correctly format `extern crate` conflict resolution help Closes #45799. Follow up to @Cldfire's #45820. If the `extern` statement that will have a suggestion ends on a `;`, synthesize a new span that doesn't include it.
2 parents 87990a1 + 445e404 commit 7046a40

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/librustc_resolve/lib.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -3980,14 +3980,14 @@ impl<'a> Resolver<'a> {
39803980
container));
39813981

39823982
err.span_label(span, format!("`{}` re{} here", name, new_participle));
3983-
if old_binding.span != syntax_pos::DUMMY_SP {
3983+
if old_binding.span != DUMMY_SP {
39843984
err.span_label(self.session.codemap().def_span(old_binding.span),
39853985
format!("previous {} of the {} `{}` here", old_noun, old_kind, name));
39863986
}
39873987

39883988
// See https://github.com/rust-lang/rust/issues/32354
39893989
if old_binding.is_import() || new_binding.is_import() {
3990-
let binding = if new_binding.is_import() {
3990+
let binding = if new_binding.is_import() && new_binding.span != DUMMY_SP {
39913991
new_binding
39923992
} else {
39933993
old_binding
@@ -4000,7 +4000,13 @@ impl<'a> Resolver<'a> {
40004000
binding.is_renamed_extern_crate()) {
40014001
err.span_suggestion(binding.span,
40024002
rename_msg,
4003-
format!("{} as Other{}", snippet, name));
4003+
if snippet.ends_with(';') {
4004+
format!("{} as Other{};",
4005+
&snippet[..snippet.len()-1],
4006+
name)
4007+
} else {
4008+
format!("{} as Other{}", snippet, name)
4009+
});
40044010
} else {
40054011
err.span_label(binding.span, rename_msg);
40064012
}

src/libsyntax/parse/parser.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6115,6 +6115,7 @@ impl<'a> Parser<'a> {
61156115
self.expect(&token::Semi)?;
61166116

61176117
let prev_span = self.prev_span;
6118+
61186119
Ok(self.mk_item(lo.to(prev_span),
61196120
ident,
61206121
ItemKind::ExternCrate(maybe_path),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 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+
extern crate std;
12+
fn main() {}
13+
//~^^ ERROR the name `std` is defined multiple times [E0259]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0259]: the name `std` is defined multiple times
2+
--> $DIR/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs:11:1
3+
|
4+
11 | extern crate std;
5+
| ^^^^^^^^^^^^^^^^^ `std` reimported here
6+
|
7+
= note: `std` must be defined only once in the type namespace of this module
8+
help: You can use `as` to change the binding name of the import
9+
|
10+
11 | extern crate std as Otherstd;
11+
|
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)