From 3c91bdca1d552055e2b92ecac5275c1ebe9a4ee8 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 29 Jan 2020 20:34:28 +0000 Subject: [PATCH 01/11] Suggest path separator for single-colon typos This commit adds guidance for when a user means to type a path, but ends up typing a single colon, such as `<:Ty>`. This change seemed pertinent as the current error message is particularly misleading, emitting `error: unmatched angle bracket`, despite the angle bracket being matched later on, leaving the user to track down the typo'd colon. --- src/librustc_parse/parser/path.rs | 18 +++++++++++++++++- .../parser/qualified-path-in-turbofish.fixed | 19 +++++++++++++++++++ .../ui/parser/qualified-path-in-turbofish.rs | 19 +++++++++++++++++++ .../parser/qualified-path-in-turbofish.stderr | 8 ++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/parser/qualified-path-in-turbofish.fixed create mode 100644 src/test/ui/parser/qualified-path-in-turbofish.rs create mode 100644 src/test/ui/parser/qualified-path-in-turbofish.stderr diff --git a/src/librustc_parse/parser/path.rs b/src/librustc_parse/parser/path.rs index 0358458c0991d..25ba571a6a494 100644 --- a/src/librustc_parse/parser/path.rs +++ b/src/librustc_parse/parser/path.rs @@ -71,7 +71,23 @@ impl<'a> Parser<'a> { debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count); } - self.expect(&token::ModSep)?; + let lo_colon = self.token.span; + if self.eat(&token::Colon) { + // >:Qux + // ^ + let span = lo_colon.to(self.prev_span); + self.diagnostic() + .struct_span_err(span, "found single colon where type path was expected") + .span_suggestion( + span, + "use double colon", + "::".to_string(), + Applicability::MachineApplicable, + ) + .emit(); + } else { + self.expect(&token::ModSep)?; + } let qself = QSelf { ty, path_span, position: path.segments.len() }; self.parse_path_segments(&mut path.segments, style)?; diff --git a/src/test/ui/parser/qualified-path-in-turbofish.fixed b/src/test/ui/parser/qualified-path-in-turbofish.fixed new file mode 100644 index 0000000000000..a4213bdd3fb34 --- /dev/null +++ b/src/test/ui/parser/qualified-path-in-turbofish.fixed @@ -0,0 +1,19 @@ +// run-rustfix +trait T { + type Ty; +} + +struct Impl; + +impl T for Impl { + type Ty = u32; +} + +fn template() -> i64 { + 3 +} + +fn main() { + template::<::Ty>(); + //~^ ERROR found single colon where type path was expected +} diff --git a/src/test/ui/parser/qualified-path-in-turbofish.rs b/src/test/ui/parser/qualified-path-in-turbofish.rs new file mode 100644 index 0000000000000..75b2af2aa2e08 --- /dev/null +++ b/src/test/ui/parser/qualified-path-in-turbofish.rs @@ -0,0 +1,19 @@ +// run-rustfix +trait T { + type Ty; +} + +struct Impl; + +impl T for Impl { + type Ty = u32; +} + +fn template() -> i64 { + 3 +} + +fn main() { + template::<:Ty>(); + //~^ ERROR found single colon where type path was expected +} diff --git a/src/test/ui/parser/qualified-path-in-turbofish.stderr b/src/test/ui/parser/qualified-path-in-turbofish.stderr new file mode 100644 index 0000000000000..1fe6353b7a013 --- /dev/null +++ b/src/test/ui/parser/qualified-path-in-turbofish.stderr @@ -0,0 +1,8 @@ +error: found single colon where type path was expected + --> $DIR/qualified-path-in-turbofish.rs:17:27 + | +LL | template::<:Ty>(); + | ^ help: use double colon: `::` + +error: aborting due to previous error + From 2a79ed0b4999bf9e805e0e38cd1faf5f85068368 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Thu, 30 Jan 2020 17:25:58 -0800 Subject: [PATCH 02/11] [docs] remind bug reporters to update nightly --- CONTRIBUTING.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fc8ca5d07b212..d6840b20c89ac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -50,6 +50,9 @@ is a bug or not, feel free to file a bug anyway. **If you believe reporting your bug publicly represents a security risk to Rust users, please follow our [instructions for reporting security vulnerabilities](https://www.rust-lang.org/policies/security)**. +If you're using the nightly channel, please check if the bug exists in the +latest toolchain before filing your bug. It might be fixed already. + If you have the chance, before reporting a bug, please [search existing issues](https://github.com/rust-lang/rust/search?q=&type=Issues&utf8=%E2%9C%93), as it's possible that someone else has already reported your error. This doesn't From 08e85aa5865dcb8198942a082d892d863367d4d2 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 31 Jan 2020 17:09:34 +0100 Subject: [PATCH 03/11] Ignore `build` dir formatting --- rustfmt.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rustfmt.toml b/rustfmt.toml index 73f8cc1ff68c6..2a034845c6e0b 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -6,6 +6,8 @@ merge_derives = false # by default we ignore everything in the repository # tidy only checks files which are not ignored, each entry follows gitignore style ignore = [ + "build", + # tests for now are not formatted, as they are sometimes pretty-printing constrained # (and generally rustfmt can move around comments in UI-testing incompatible ways) "src/test", From 482c761704b9f7d08d00b6cf4c567db427da7ec7 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Thu, 23 Jan 2020 00:22:46 +0900 Subject: [PATCH 04/11] Use BufWriter --- .../obligation_forest/graphviz.rs | 3 +- src/librustc_incremental/assert_dep_graph.rs | 4 +-- src/librustc_interface/passes.rs | 4 +-- src/librustc_mir/borrow_check/facts.rs | 34 +++++++++++++++---- src/librustc_mir/transform/dump_mir.rs | 2 +- src/librustc_mir/util/liveness.rs | 5 +-- 6 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/librustc_data_structures/obligation_forest/graphviz.rs b/src/librustc_data_structures/obligation_forest/graphviz.rs index ddf89d99621ca..0fd83dad56b1a 100644 --- a/src/librustc_data_structures/obligation_forest/graphviz.rs +++ b/src/librustc_data_structures/obligation_forest/graphviz.rs @@ -2,6 +2,7 @@ use crate::obligation_forest::{ForestObligation, ObligationForest}; use graphviz as dot; use std::env::var_os; use std::fs::File; +use std::io::BufWriter; use std::path::Path; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; @@ -31,7 +32,7 @@ impl ObligationForest { let file_path = dir.as_ref().join(format!("{:010}_{}.gv", counter, description)); - let mut gv_file = File::create(file_path).unwrap(); + let mut gv_file = BufWriter::new(File::create(file_path).unwrap()); dot::render(&self, &mut gv_file).unwrap(); } diff --git a/src/librustc_incremental/assert_dep_graph.rs b/src/librustc_incremental/assert_dep_graph.rs index 9490128e32d6a..51cc091b6c0af 100644 --- a/src/librustc_incremental/assert_dep_graph.rs +++ b/src/librustc_incremental/assert_dep_graph.rs @@ -49,7 +49,7 @@ use syntax::ast; use std::env; use std::fs::{self, File}; -use std::io::Write; +use std::io::{BufWriter, Write}; pub fn assert_dep_graph(tcx: TyCtxt<'_>) { tcx.dep_graph.with_ignore(|| { @@ -235,7 +235,7 @@ fn dump_graph(tcx: TyCtxt<'_>) { { // dump a .txt file with just the edges: let txt_path = format!("{}.txt", path); - let mut file = File::create(&txt_path).unwrap(); + let mut file = BufWriter::new(File::create(&txt_path).unwrap()); for &(ref source, ref target) in &edges { write!(file, "{:?} -> {:?}\n", source, target).unwrap(); } diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index d62c7539d5f21..6af83a4c5abc1 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -49,7 +49,7 @@ use tempfile::Builder as TempFileBuilder; use std::any::Any; use std::cell::RefCell; use std::ffi::OsString; -use std::io::{self, Write}; +use std::io::{self, BufWriter, Write}; use std::path::PathBuf; use std::rc::Rc; use std::{env, fs, iter, mem}; @@ -575,7 +575,7 @@ fn write_out_deps( }); } - let mut file = fs::File::create(&deps_filename)?; + let mut file = BufWriter::new(fs::File::create(&deps_filename)?); for path in out_filenames { writeln!(file, "{}: {}\n", path.display(), files.join(" "))?; } diff --git a/src/librustc_mir/borrow_check/facts.rs b/src/librustc_mir/borrow_check/facts.rs index a16c36d749f0d..827ccb1c85733 100644 --- a/src/librustc_mir/borrow_check/facts.rs +++ b/src/librustc_mir/borrow_check/facts.rs @@ -8,7 +8,7 @@ use rustc_index::vec::Idx; use std::error::Error; use std::fmt::Debug; use std::fs::{self, File}; -use std::io::Write; +use std::io::{BufWriter, Write}; use std::path::Path; #[derive(Copy, Clone, Debug)] @@ -117,7 +117,7 @@ impl<'w> FactWriter<'w> { T: FactRow, { let file = &self.dir.join(file_name); - let mut file = File::create(file)?; + let mut file = BufWriter::new(File::create(file)?); for row in rows { row.write(&mut file, self.location_table)?; } @@ -126,11 +126,19 @@ impl<'w> FactWriter<'w> { } trait FactRow { - fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box>; + fn write( + &self, + out: &mut dyn Write, + location_table: &LocationTable, + ) -> Result<(), Box>; } impl FactRow for RegionVid { - fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box> { + fn write( + &self, + out: &mut dyn Write, + location_table: &LocationTable, + ) -> Result<(), Box> { write_row(out, location_table, &[self]) } } @@ -140,7 +148,11 @@ where A: FactCell, B: FactCell, { - fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box> { + fn write( + &self, + out: &mut dyn Write, + location_table: &LocationTable, + ) -> Result<(), Box> { write_row(out, location_table, &[&self.0, &self.1]) } } @@ -151,7 +163,11 @@ where B: FactCell, C: FactCell, { - fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box> { + fn write( + &self, + out: &mut dyn Write, + location_table: &LocationTable, + ) -> Result<(), Box> { write_row(out, location_table, &[&self.0, &self.1, &self.2]) } } @@ -163,7 +179,11 @@ where C: FactCell, D: FactCell, { - fn write(&self, out: &mut File, location_table: &LocationTable) -> Result<(), Box> { + fn write( + &self, + out: &mut dyn Write, + location_table: &LocationTable, + ) -> Result<(), Box> { write_row(out, location_table, &[&self.0, &self.1, &self.2, &self.3]) } } diff --git a/src/librustc_mir/transform/dump_mir.rs b/src/librustc_mir/transform/dump_mir.rs index 2cbda33ad2db1..5dec2c6df99dc 100644 --- a/src/librustc_mir/transform/dump_mir.rs +++ b/src/librustc_mir/transform/dump_mir.rs @@ -61,7 +61,7 @@ pub fn on_mir_pass<'tcx>( pub fn emit_mir(tcx: TyCtxt<'_>, outputs: &OutputFilenames) -> io::Result<()> { let path = outputs.path(OutputType::Mir); - let mut f = File::create(&path)?; + let mut f = io::BufWriter::new(File::create(&path)?); mir_util::write_mir_pretty(tcx, None, &mut f)?; Ok(()) } diff --git a/src/librustc_mir/util/liveness.rs b/src/librustc_mir/util/liveness.rs index 1488bfe4d627a..b12ad1e4c15cc 100644 --- a/src/librustc_mir/util/liveness.rs +++ b/src/librustc_mir/util/liveness.rs @@ -36,7 +36,7 @@ use rustc_data_structures::work_queue::WorkQueue; use rustc_index::bit_set::BitSet; use rustc_index::vec::{Idx, IndexVec}; use std::fs; -use std::io::{self, Write}; +use std::io::{self, BufWriter, Write}; use std::path::{Path, PathBuf}; pub type LiveVarSet = BitSet; @@ -288,7 +288,8 @@ fn dump_matched_mir_node<'tcx>( let item_id = tcx.hir().as_local_hir_id(source.def_id()).unwrap(); let file_name = format!("rustc.node{}{}-liveness.mir", item_id, pass_name); file_path.push(&file_name); - let _ = fs::File::create(&file_path).and_then(|mut file| { + let _ = fs::File::create(&file_path).and_then(|file| { + let mut file = BufWriter::new(file); writeln!(file, "// MIR local liveness analysis for `{}`", node_path)?; writeln!(file, "// source = {:?}", source)?; writeln!(file, "// pass_name = {}", pass_name)?; From cad0cfd9fdbc64dece3f38c03b15ed9aad032d5c Mon Sep 17 00:00:00 2001 From: Konrad Borowski Date: Sat, 1 Feb 2020 13:29:00 +0100 Subject: [PATCH 05/11] Remove a comment about pretty printer in formatting tests rustc is now using rustfmt, not the old formatter. --- src/test/ui/ifmt.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/test/ui/ifmt.rs b/src/test/ui/ifmt.rs index 1a070843cc446..27ab3d6b7abff 100644 --- a/src/test/ui/ifmt.rs +++ b/src/test/ui/ifmt.rs @@ -99,7 +99,6 @@ pub fn main() { let a: &dyn fmt::Debug = &1; t!(format!("{:?}", a), "1"); - // Formatting strings and their arguments t!(format!("{}", "a"), "a"); t!(format!("{:4}", "a"), "a "); @@ -187,10 +186,6 @@ pub fn main() { // Ergonomic format_args! t!(format!("{0:x} {0:X}", 15), "f F"); t!(format!("{0:x} {0:X} {}", 15), "f F 15"); - // NOTE: For now the longer test cases must not be followed immediately by - // >1 empty lines, or the pretty printer will break. Since no one wants to - // touch the current pretty printer (#751), we have no choice but to work - // around it. Some of the following test cases are also affected. t!(format!("{:x}{0:X}{a:x}{:X}{1:x}{a:X}", 13, 14, a=15), "dDfEeF"); t!(format!("{a:x} {a:X}", a=15), "f F"); @@ -201,7 +196,6 @@ pub fn main() { t!(format!("{a:.*} {0} {:.*}", 4, 3, "efgh", a="abcdef"), "abcd 4 efg"); t!(format!("{:.a$} {a} {a:#x}", "aaaaaa", a=2), "aa 2 0x2"); - // Test that pointers don't get truncated. { let val = usize::MAX; From 8bbaeb7ff9a830e85106a00c84a1aa262f77a7f3 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Sat, 1 Feb 2020 18:40:12 +0100 Subject: [PATCH 06/11] Remove `Alloc` in favor of `AllocRef` `AllocRef` was reexported as `Alloc` in order to not break toolstate in the week before the next stable release. --- src/libcore/alloc.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 1b7dfafbd704c..38df843d258d3 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -1227,10 +1227,3 @@ pub unsafe trait AllocRef { } } } - -// In order to rename `Alloc` to `AllocRef`, some submoduleshas to be updated as well. The CI fails -// if either of the submodules fails to compile. The submodules have their own CI depending on a -// specific Rust version, which don't have `AllocRef` yet. This alias is used to make the submodules -// compile and pass the CI. -#[unstable(feature = "allocator_api", issue = "32838")] -pub use self::AllocRef as Alloc; From 88d64a09314d7f19201eebc1b92377afed31b5c2 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 1 Feb 2020 19:06:15 +0000 Subject: [PATCH 07/11] Simplify span usage and avoid .eat() --- src/librustc_parse/parser/path.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/librustc_parse/parser/path.rs b/src/librustc_parse/parser/path.rs index 25ba571a6a494..a4541046c6c14 100644 --- a/src/librustc_parse/parser/path.rs +++ b/src/librustc_parse/parser/path.rs @@ -71,15 +71,15 @@ impl<'a> Parser<'a> { debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count); } - let lo_colon = self.token.span; - if self.eat(&token::Colon) { + if self.token.kind == token::Colon { // >:Qux // ^ - let span = lo_colon.to(self.prev_span); + self.bump(); + self.diagnostic() - .struct_span_err(span, "found single colon where type path was expected") + .struct_span_err(self.prev_span, "found single colon where type path was expected") .span_suggestion( - span, + self.prev_span, "use double colon", "::".to_string(), Applicability::MachineApplicable, From 45fb7232abc893a06272550ebcad7b39a3cb26c1 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 1 Feb 2020 19:10:42 +0000 Subject: [PATCH 08/11] Move colon-check to recover_colon_before_qpath_proj() --- src/librustc_parse/parser/path.rs | 38 +++++++++++++++++++------------ 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/librustc_parse/parser/path.rs b/src/librustc_parse/parser/path.rs index a4541046c6c14..027380eaa2a69 100644 --- a/src/librustc_parse/parser/path.rs +++ b/src/librustc_parse/parser/path.rs @@ -71,21 +71,7 @@ impl<'a> Parser<'a> { debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count); } - if self.token.kind == token::Colon { - // >:Qux - // ^ - self.bump(); - - self.diagnostic() - .struct_span_err(self.prev_span, "found single colon where type path was expected") - .span_suggestion( - self.prev_span, - "use double colon", - "::".to_string(), - Applicability::MachineApplicable, - ) - .emit(); - } else { + if !self.recover_colon_before_qpath_proj() { self.expect(&token::ModSep)?; } @@ -95,6 +81,28 @@ impl<'a> Parser<'a> { Ok((qself, Path { segments: path.segments, span: lo.to(self.prev_span) })) } + fn recover_colon_before_qpath_proj(&mut self) -> bool { + if self.token.kind != token::Colon { + return false; + } + + // >:Qux + // ^ + self.bump(); + + self.diagnostic() + .struct_span_err(self.prev_span, "found single colon where type path was expected") + .span_suggestion( + self.prev_span, + "use double colon", + "::".to_string(), + Applicability::MachineApplicable, + ) + .emit(); + + true + } + /// Parses simple paths. /// /// `path = [::] segment+` From 991d2ee282837a0ca3ec5a730e081274d37fa8b0 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 1 Feb 2020 19:21:54 +0000 Subject: [PATCH 09/11] Improve wording and docs for qualified path recovery --- src/librustc_parse/parser/path.rs | 15 +++++++++++---- .../ui/parser/qualified-path-in-turbofish.fixed | 2 +- src/test/ui/parser/qualified-path-in-turbofish.rs | 2 +- .../ui/parser/qualified-path-in-turbofish.stderr | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/librustc_parse/parser/path.rs b/src/librustc_parse/parser/path.rs index 027380eaa2a69..5aa14c1739ff6 100644 --- a/src/librustc_parse/parser/path.rs +++ b/src/librustc_parse/parser/path.rs @@ -81,17 +81,24 @@ impl<'a> Parser<'a> { Ok((qself, Path { segments: path.segments, span: lo.to(self.prev_span) })) } + /// Recover from an invalid single colon, when the user likely meant a qualified path. + /// + /// ```ignore (diagnostics) + /// >:Qux + /// ^ help: use double colon + /// ``` fn recover_colon_before_qpath_proj(&mut self) -> bool { if self.token.kind != token::Colon { return false; } - // >:Qux - // ^ - self.bump(); + self.bump(); // colon self.diagnostic() - .struct_span_err(self.prev_span, "found single colon where type path was expected") + .struct_span_err( + self.prev_span, + "found single colon before projection in qualified path", + ) .span_suggestion( self.prev_span, "use double colon", diff --git a/src/test/ui/parser/qualified-path-in-turbofish.fixed b/src/test/ui/parser/qualified-path-in-turbofish.fixed index a4213bdd3fb34..404d2f7762df4 100644 --- a/src/test/ui/parser/qualified-path-in-turbofish.fixed +++ b/src/test/ui/parser/qualified-path-in-turbofish.fixed @@ -15,5 +15,5 @@ fn template() -> i64 { fn main() { template::<::Ty>(); - //~^ ERROR found single colon where type path was expected + //~^ ERROR found single colon before projection in qualified path } diff --git a/src/test/ui/parser/qualified-path-in-turbofish.rs b/src/test/ui/parser/qualified-path-in-turbofish.rs index 75b2af2aa2e08..2f4b2ed348b9c 100644 --- a/src/test/ui/parser/qualified-path-in-turbofish.rs +++ b/src/test/ui/parser/qualified-path-in-turbofish.rs @@ -15,5 +15,5 @@ fn template() -> i64 { fn main() { template::<:Ty>(); - //~^ ERROR found single colon where type path was expected + //~^ ERROR found single colon before projection in qualified path } diff --git a/src/test/ui/parser/qualified-path-in-turbofish.stderr b/src/test/ui/parser/qualified-path-in-turbofish.stderr index 1fe6353b7a013..8857d2ef30cfc 100644 --- a/src/test/ui/parser/qualified-path-in-turbofish.stderr +++ b/src/test/ui/parser/qualified-path-in-turbofish.stderr @@ -1,4 +1,4 @@ -error: found single colon where type path was expected +error: found single colon before projection in qualified path --> $DIR/qualified-path-in-turbofish.rs:17:27 | LL | template::<:Ty>(); From 07ee472cd18925be45d424d9cfd59c441ea9c9a7 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 1 Feb 2020 19:24:51 +0000 Subject: [PATCH 10/11] Avoid qualified path recovery when not followed by identifier --- src/librustc_parse/parser/path.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/librustc_parse/parser/path.rs b/src/librustc_parse/parser/path.rs index 5aa14c1739ff6..a09eb42dcfe6a 100644 --- a/src/librustc_parse/parser/path.rs +++ b/src/librustc_parse/parser/path.rs @@ -82,13 +82,17 @@ impl<'a> Parser<'a> { } /// Recover from an invalid single colon, when the user likely meant a qualified path. + /// We avoid emitting this if not followed by an identifier, as our assumption that the user + /// intended this to be a qualified path may not be correct. /// /// ```ignore (diagnostics) /// >:Qux /// ^ help: use double colon /// ``` fn recover_colon_before_qpath_proj(&mut self) -> bool { - if self.token.kind != token::Colon { + if self.token.kind != token::Colon + || self.look_ahead(1, |t| !t.is_ident() || t.is_reserved_ident()) + { return false; } From 726568bd1b4ac9af4dc84816eae1957c3d2bfc32 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sun, 2 Feb 2020 04:40:55 +0900 Subject: [PATCH 11/11] Do not suggest things named underscore --- src/librustc_resolve/diagnostics.rs | 5 +++++ .../resolve/typo-suggestion-named-underscore.rs | 14 ++++++++++++++ .../typo-suggestion-named-underscore.stderr | 16 ++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 src/test/ui/resolve/typo-suggestion-named-underscore.rs create mode 100644 src/test/ui/resolve/typo-suggestion-named-underscore.stderr diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index b762e0b08ac04..1705736a67c43 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -769,6 +769,11 @@ impl<'a> Resolver<'a> { span: Span, ) -> bool { if let Some(suggestion) = suggestion { + // We shouldn't suggest underscore. + if suggestion.candidate == kw::Underscore { + return false; + } + let msg = format!( "{} {} with a similar name exists", suggestion.res.article(), diff --git a/src/test/ui/resolve/typo-suggestion-named-underscore.rs b/src/test/ui/resolve/typo-suggestion-named-underscore.rs new file mode 100644 index 0000000000000..a2b05db035150 --- /dev/null +++ b/src/test/ui/resolve/typo-suggestion-named-underscore.rs @@ -0,0 +1,14 @@ +const _: () = (); + +fn main() { + a // Shouldn't suggest underscore + //~^ ERROR: cannot find value `a` in this scope +} + +trait Unknown {} + +#[allow(unused_imports)] +use Unknown as _; + +fn foo(x: T) {} // Shouldn't suggest underscore +//~^ ERROR: cannot find trait `A` in this scope diff --git a/src/test/ui/resolve/typo-suggestion-named-underscore.stderr b/src/test/ui/resolve/typo-suggestion-named-underscore.stderr new file mode 100644 index 0000000000000..65d1b084a3a7b --- /dev/null +++ b/src/test/ui/resolve/typo-suggestion-named-underscore.stderr @@ -0,0 +1,16 @@ +error[E0425]: cannot find value `a` in this scope + --> $DIR/typo-suggestion-named-underscore.rs:4:5 + | +LL | a // Shouldn't suggest underscore + | ^ not found in this scope + +error[E0405]: cannot find trait `A` in this scope + --> $DIR/typo-suggestion-named-underscore.rs:13:11 + | +LL | fn foo(x: T) {} // Shouldn't suggest underscore + | ^ not found in this scope + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0405, E0425. +For more information about an error, try `rustc --explain E0405`.