From c39ad4b145f9eee71a5e6b52f9d10acc7e43ce0d Mon Sep 17 00:00:00 2001 From: Cldfire Date: Mon, 6 Nov 2017 21:40:21 -0500 Subject: [PATCH 1/2] Correctly format `extern crate` conflict resolution help --- src/librustc_resolve/lib.rs | 4 ++-- src/libsyntax/parse/parser.rs | 4 +++- ...ad-extern-crate-rename-suggestion-formatting.rs | 12 ++++++++++++ ...xtern-crate-rename-suggestion-formatting.stderr | 14 ++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs create mode 100644 src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 55c7e5f392416..6fcdb1d651f31 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -3980,14 +3980,14 @@ impl<'a> Resolver<'a> { container)); err.span_label(span, format!("`{}` re{} here", name, new_participle)); - if old_binding.span != syntax_pos::DUMMY_SP { + if old_binding.span != DUMMY_SP { err.span_label(self.session.codemap().def_span(old_binding.span), format!("previous {} of the {} `{}` here", old_noun, old_kind, name)); } // See https://github.com/rust-lang/rust/issues/32354 if old_binding.is_import() || new_binding.is_import() { - let binding = if new_binding.is_import() { + let binding = if new_binding.is_import() && new_binding.span != DUMMY_SP { new_binding } else { old_binding diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 8213d604b91b2..a7977d5235d2e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6128,9 +6128,11 @@ impl<'a> Parser<'a> { } else { (None, crate_name) }; - self.expect(&token::Semi)?; + // We grab this before expecting the `;` so it's not a part of the span let prev_span = self.prev_span; + self.expect(&token::Semi)?; + Ok(self.mk_item(lo.to(prev_span), ident, ItemKind::ExternCrate(maybe_path), diff --git a/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs b/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs new file mode 100644 index 0000000000000..7c55f9c4eb9f0 --- /dev/null +++ b/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs @@ -0,0 +1,12 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +extern crate std; +fn main() {} diff --git a/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr b/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr new file mode 100644 index 0000000000000..a4eadcc8b4675 --- /dev/null +++ b/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr @@ -0,0 +1,14 @@ +error[E0259]: the name `std` is defined multiple times + --> $DIR/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs:11:1 + | +11 | extern crate std; + | ^^^^^^^^^^^^^^^^ `std` reimported here + | + = note: `std` must be defined only once in the type namespace of this module +help: You can use `as` to change the binding name of the import + | +11 | extern crate std as Otherstd; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + From 445e404ba4c9782e4f5028eccb7c9473ae33c70a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 25 Jan 2018 22:40:38 -0800 Subject: [PATCH 2/2] Instead of modifying the item's span synthesize it --- src/librustc_resolve/lib.rs | 8 +++++++- src/libsyntax/parse/parser.rs | 3 +-- ...45799-bad-extern-crate-rename-suggestion-formatting.rs | 1 + ...9-bad-extern-crate-rename-suggestion-formatting.stderr | 4 ++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 6fcdb1d651f31..15ff0bff849e7 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -4000,7 +4000,13 @@ impl<'a> Resolver<'a> { binding.is_renamed_extern_crate()) { err.span_suggestion(binding.span, rename_msg, - format!("{} as Other{}", snippet, name)); + if snippet.ends_with(';') { + format!("{} as Other{};", + &snippet[..snippet.len()-1], + name) + } else { + format!("{} as Other{}", snippet, name) + }); } else { err.span_label(binding.span, rename_msg); } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a7977d5235d2e..35e89652c998f 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6128,10 +6128,9 @@ impl<'a> Parser<'a> { } else { (None, crate_name) }; + self.expect(&token::Semi)?; - // We grab this before expecting the `;` so it's not a part of the span let prev_span = self.prev_span; - self.expect(&token::Semi)?; Ok(self.mk_item(lo.to(prev_span), ident, diff --git a/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs b/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs index 7c55f9c4eb9f0..4d75127b645bd 100644 --- a/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs +++ b/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs @@ -10,3 +10,4 @@ extern crate std; fn main() {} +//~^^ ERROR the name `std` is defined multiple times [E0259] diff --git a/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr b/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr index a4eadcc8b4675..d2ac15f7ffcb3 100644 --- a/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr +++ b/src/test/ui/suggestions/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr @@ -2,13 +2,13 @@ error[E0259]: the name `std` is defined multiple times --> $DIR/issue-45799-bad-extern-crate-rename-suggestion-formatting.rs:11:1 | 11 | extern crate std; - | ^^^^^^^^^^^^^^^^ `std` reimported here + | ^^^^^^^^^^^^^^^^^ `std` reimported here | = note: `std` must be defined only once in the type namespace of this module help: You can use `as` to change the binding name of the import | 11 | extern crate std as Otherstd; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | error: aborting due to previous error