From d589cf911109149564c8898ad1cb1d906f91caea Mon Sep 17 00:00:00 2001 From: David Wood Date: Sun, 7 Apr 2019 20:34:21 +0200 Subject: [PATCH] Handle renamed imports. This commit extends the suggestion to handle imports that are aliased to another name. --- src/librustc_resolve/error_reporting.rs | 11 ++++++++--- src/librustc_resolve/resolve_imports.rs | 24 ++++++++++-------------- src/test/ui/issue-59764.fixed | 15 +++++++++++++++ src/test/ui/issue-59764.rs | 15 +++++++++++++++ src/test/ui/issue-59764.stderr | 20 ++++++++++++++++---- 5 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/librustc_resolve/error_reporting.rs b/src/librustc_resolve/error_reporting.rs index 99aa6e39f7b..b7ccbbf6079 100644 --- a/src/librustc_resolve/error_reporting.rs +++ b/src/librustc_resolve/error_reporting.rs @@ -11,7 +11,7 @@ use syntax::symbol::keywords; use syntax_pos::Span; use crate::macros::ParentScope; -use crate::resolve_imports::{ImportDirective, ImportResolver}; +use crate::resolve_imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver}; use crate::{import_candidate_to_enum_paths, is_self_type, is_self_value, path_names_to_string}; use crate::{AssocSuggestion, CrateLint, ImportSuggestion, ModuleOrUniformRoot, PathResult, PathSource, Resolver, Segment, Suggestion}; @@ -610,11 +610,16 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { let resolution = resolutions.get(&(ident, MacroNS))?; let binding = resolution.borrow().binding()?; if let Def::Macro(_, MacroKind::Bang) = binding.def() { - let name = crate_module.kind.name().unwrap(); + let module_name = crate_module.kind.name().unwrap(); + let import = match directive.subclass { + ImportDirectiveSubclass::SingleImport { source, target, .. } if source != target => + format!("{} as {}", source, target), + _ => format!("{}", ident), + }; let suggestion = Some(( directive.span, String::from("a macro with this name exists at the root of the crate"), - format!("{}::{}", name, ident), + format!("{}::{}", module_name, import), Applicability::MaybeIncorrect, )); let note = vec![ diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 6a01af5d115..ef5f0f54ee3 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -1111,21 +1111,17 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { } }); - let (suggestion, note) = if let Some((suggestion, note)) = - self.check_for_module_export_macro(directive, module, ident) - { + let lev_suggestion = find_best_match_for_name(names, &ident.as_str(), None) + .map(|suggestion| + (ident.span, String::from("a similar name exists in the module"), + suggestion.to_string(), Applicability::MaybeIncorrect) + ); - ( - suggestion.or_else(|| - find_best_match_for_name(names, &ident.as_str(), None) - .map(|suggestion| - (ident.span, String::from("a similar name exists in the module"), - suggestion.to_string(), Applicability::MaybeIncorrect) - )), - note, - ) - } else { - (None, Vec::new()) + let (suggestion, note) = match self.check_for_module_export_macro( + directive, module, ident, + ) { + Some((suggestion, note)) => (suggestion.or(lev_suggestion), note), + _ => (lev_suggestion, Vec::new()), }; let label = match module { diff --git a/src/test/ui/issue-59764.fixed b/src/test/ui/issue-59764.fixed index d7a01ce5f4d..34b9fc4edd9 100644 --- a/src/test/ui/issue-59764.fixed +++ b/src/test/ui/issue-59764.fixed @@ -3,6 +3,21 @@ // edition:2018 // run-rustfix +#![allow(warnings)] + +// This tests the suggestion to import macros from the root of a crate. This aims to capture +// the case where a user attempts to import a macro from the definition location instead of the +// root of the crate and the macro is annotated with `#![macro_export]`. + +// Edge cases.. + +mod renamed_import { + use issue_59764::makro as baz; + //~^ ERROR unresolved import `issue_59764::foo::makro` [E0432] +} + +// Simple case.. + use issue_59764::makro; //~^ ERROR unresolved import `issue_59764::foo::makro` [E0432] diff --git a/src/test/ui/issue-59764.rs b/src/test/ui/issue-59764.rs index e6f7205bfca..b33b8e0cf5c 100644 --- a/src/test/ui/issue-59764.rs +++ b/src/test/ui/issue-59764.rs @@ -3,6 +3,21 @@ // edition:2018 // run-rustfix +#![allow(warnings)] + +// This tests the suggestion to import macros from the root of a crate. This aims to capture +// the case where a user attempts to import a macro from the definition location instead of the +// root of the crate and the macro is annotated with `#![macro_export]`. + +// Edge cases.. + +mod renamed_import { + use issue_59764::foo::makro as baz; + //~^ ERROR unresolved import `issue_59764::foo::makro` [E0432] +} + +// Simple case.. + use issue_59764::foo::makro; //~^ ERROR unresolved import `issue_59764::foo::makro` [E0432] diff --git a/src/test/ui/issue-59764.stderr b/src/test/ui/issue-59764.stderr index 6b3237e9272..8bb4d03fc5d 100644 --- a/src/test/ui/issue-59764.stderr +++ b/src/test/ui/issue-59764.stderr @@ -1,5 +1,17 @@ error[E0432]: unresolved import `issue_59764::foo::makro` - --> $DIR/issue-59764.rs:6:5 + --> $DIR/issue-59764.rs:15:9 + | +LL | use issue_59764::foo::makro as baz; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `makro` in `foo` + | + = note: this could be because a macro annotated with `#[macro_export]` will be exported at the root of the crate instead of the module where it is defined +help: a macro with this name exists at the root of the crate + | +LL | use issue_59764::makro as baz; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0432]: unresolved import `issue_59764::foo::makro` + --> $DIR/issue-59764.rs:21:5 | LL | use issue_59764::foo::makro; | ^^^^^^^^^^^^^^^^^^^^^^^ no `makro` in `foo` @@ -11,7 +23,7 @@ LL | use issue_59764::makro; | ^^^^^^^^^^^^^^^^^^ error: cannot determine resolution for the macro `makro` - --> $DIR/issue-59764.rs:9:1 + --> $DIR/issue-59764.rs:24:1 | LL | makro!(bar); | ^^^^^ @@ -19,12 +31,12 @@ LL | makro!(bar); = note: import resolution is stuck, try simplifying macro imports error[E0425]: cannot find function `bar` in this scope - --> $DIR/issue-59764.rs:13:5 + --> $DIR/issue-59764.rs:28:5 | LL | bar(); | ^^^ not found in this scope -error: aborting due to 3 previous errors +error: aborting due to 4 previous errors Some errors occurred: E0425, E0432. For more information about an error, try `rustc --explain E0425`.