From 5a2c3011067ffa1ff935b7d415da9e9fa74b70ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 29 Dec 2018 16:35:57 -0800 Subject: [PATCH] Do not complain about missing crate named as a keyword --- src/librustc_resolve/lib.rs | 10 ++++++++-- src/librustc_resolve/macros.rs | 6 ++++-- src/librustc_resolve/resolve_imports.rs | 6 +++++- src/test/ui/issues/issue-57198-pass.rs | 9 +++++++++ src/test/ui/issues/issue-57198.rs | 8 ++++++++ src/test/ui/issues/issue-57198.stderr | 8 ++++++++ 6 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/issues/issue-57198-pass.rs create mode 100644 src/test/ui/issues/issue-57198.rs create mode 100644 src/test/ui/issues/issue-57198.stderr diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 6c7d0efbc02..14d3eebe68d 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1034,6 +1034,8 @@ enum PathResult<'a> { NonModule(PathResolution), Indeterminate, Failed(Span, String, bool /* is the error from the last segment? */), + /// Encountered an error that is reported elsewhere + Ignore, } enum ModuleKind { @@ -1766,6 +1768,7 @@ impl<'a> Resolver<'a> { error_callback(self, span, ResolutionError::FailedToResolve(&msg)); Def::Err } + PathResult::Ignore => Def::Err, }; let segments: Vec<_> = segments.iter().map(|seg| { @@ -3693,7 +3696,7 @@ impl<'a> Resolver<'a> { resolve_error(self, span, ResolutionError::FailedToResolve(&msg)); err_path_resolution() } - PathResult::Module(..) | PathResult::Failed(..) => return None, + PathResult::Module(..) | PathResult::Failed(..) | PathResult::Ignore => return None, PathResult::Indeterminate => bug!("indetermined path result in resolve_qpath"), }; @@ -3925,8 +3928,11 @@ impl<'a> Resolver<'a> { }); if let Some(candidate) = candidates.get(0) { format!("did you mean `{}`?", candidate.path) - } else { + } else if !ident.is_used_keyword() { format!("maybe a missing `extern crate {};`?", ident) + } else { + // the parser will already have complained about the keyword being used + return PathResult::Ignore; } } else if i == 0 { format!("use of undeclared type or module `{}`", ident) diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 8454dbc3410..5106db53a39 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -364,7 +364,8 @@ impl<'a> Resolver<'a> { Ok(path_res.base_def()) } PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined), - PathResult::NonModule(..) | PathResult::Indeterminate | PathResult::Failed(..) => { + PathResult::NonModule(..) | PathResult::Indeterminate | + PathResult::Failed(..) | PathResult::Ignore => { Err(Determinacy::Determined) } PathResult::Module(..) => unreachable!(), @@ -929,7 +930,8 @@ impl<'a> Resolver<'a> { let def = path_res.base_def(); check_consistency(self, &path, path_span, kind, initial_def, def); } - path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed(..) => { + path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed(..) | + path_res @ PathResult::Ignore => { let (span, msg) = if let PathResult::Failed(span, msg, ..) = path_res { (span, msg) } else { diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index ba4b18abdfc..35b5b8b55c5 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -767,7 +767,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { match path_res { PathResult::Module(module) => module, PathResult::Indeterminate => return false, - PathResult::NonModule(..) | PathResult::Failed(..) => return true, + PathResult::NonModule(..) | PathResult::Failed(..) | + PathResult::Ignore => return true, } }; @@ -861,6 +862,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { module } + PathResult::Ignore => { + return None; + } PathResult::Failed(span, msg, false) => { if no_ambiguity { assert!(directive.imported_module.get().is_none()); diff --git a/src/test/ui/issues/issue-57198-pass.rs b/src/test/ui/issues/issue-57198-pass.rs new file mode 100644 index 00000000000..3857def9824 --- /dev/null +++ b/src/test/ui/issues/issue-57198-pass.rs @@ -0,0 +1,9 @@ +// run-pass + +mod m { + pub fn r#for() {} +} + +fn main() { + m::r#for(); +} diff --git a/src/test/ui/issues/issue-57198.rs b/src/test/ui/issues/issue-57198.rs new file mode 100644 index 00000000000..714a46cbc9f --- /dev/null +++ b/src/test/ui/issues/issue-57198.rs @@ -0,0 +1,8 @@ +mod m { + pub fn r#for() {} +} + +fn main() { + m::for(); + //~^ ERROR expected identifier, found keyword `for` +} diff --git a/src/test/ui/issues/issue-57198.stderr b/src/test/ui/issues/issue-57198.stderr new file mode 100644 index 00000000000..c4d297b2d95 --- /dev/null +++ b/src/test/ui/issues/issue-57198.stderr @@ -0,0 +1,8 @@ +error: expected identifier, found keyword `for` + --> $DIR/issue-57198.rs:6:8 + | +LL | m::for(); + | ^^^ expected identifier, found keyword + +error: aborting due to previous error +