Handle renamed imports.

This commit extends the suggestion to handle imports that are aliased to
another name.
This commit is contained in:
David Wood 2019-04-07 20:34:21 +02:00
parent d84907bbcc
commit d589cf9111
No known key found for this signature in database
GPG Key ID: 01760B4F9F53F154
5 changed files with 64 additions and 21 deletions

View File

@ -11,7 +11,7 @@
use syntax_pos::Span; use syntax_pos::Span;
use crate::macros::ParentScope; 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::{import_candidate_to_enum_paths, is_self_type, is_self_value, path_names_to_string};
use crate::{AssocSuggestion, CrateLint, ImportSuggestion, ModuleOrUniformRoot, PathResult, use crate::{AssocSuggestion, CrateLint, ImportSuggestion, ModuleOrUniformRoot, PathResult,
PathSource, Resolver, Segment, Suggestion}; PathSource, Resolver, Segment, Suggestion};
@ -610,11 +610,16 @@ pub(crate) fn check_for_module_export_macro(
let resolution = resolutions.get(&(ident, MacroNS))?; let resolution = resolutions.get(&(ident, MacroNS))?;
let binding = resolution.borrow().binding()?; let binding = resolution.borrow().binding()?;
if let Def::Macro(_, MacroKind::Bang) = binding.def() { 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(( let suggestion = Some((
directive.span, directive.span,
String::from("a macro with this name exists at the root of the crate"), String::from("a macro with this name exists at the root of the crate"),
format!("{}::{}", name, ident), format!("{}::{}", module_name, import),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
)); ));
let note = vec![ let note = vec![

View File

@ -1111,21 +1111,17 @@ fn finalize_import(
} }
}); });
let (suggestion, note) = if let Some((suggestion, note)) = let lev_suggestion = find_best_match_for_name(names, &ident.as_str(), None)
self.check_for_module_export_macro(directive, module, ident) .map(|suggestion|
{ (ident.span, String::from("a similar name exists in the module"),
suggestion.to_string(), Applicability::MaybeIncorrect)
);
( let (suggestion, note) = match self.check_for_module_export_macro(
suggestion.or_else(|| directive, module, ident,
find_best_match_for_name(names, &ident.as_str(), None) ) {
.map(|suggestion| Some((suggestion, note)) => (suggestion.or(lev_suggestion), note),
(ident.span, String::from("a similar name exists in the module"), _ => (lev_suggestion, Vec::new()),
suggestion.to_string(), Applicability::MaybeIncorrect)
)),
note,
)
} else {
(None, Vec::new())
}; };
let label = match module { let label = match module {

View File

@ -3,6 +3,21 @@
// edition:2018 // edition:2018
// run-rustfix // 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; use issue_59764::makro;
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432] //~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]

View File

@ -3,6 +3,21 @@
// edition:2018 // edition:2018
// run-rustfix // 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; use issue_59764::foo::makro;
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432] //~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]

View File

@ -1,5 +1,17 @@
error[E0432]: unresolved import `issue_59764::foo::makro` 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; LL | use issue_59764::foo::makro;
| ^^^^^^^^^^^^^^^^^^^^^^^ no `makro` in `foo` | ^^^^^^^^^^^^^^^^^^^^^^^ no `makro` in `foo`
@ -11,7 +23,7 @@ LL | use issue_59764::makro;
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
error: cannot determine resolution for the macro `makro` error: cannot determine resolution for the macro `makro`
--> $DIR/issue-59764.rs:9:1 --> $DIR/issue-59764.rs:24:1
| |
LL | makro!(bar); LL | makro!(bar);
| ^^^^^ | ^^^^^
@ -19,12 +31,12 @@ LL | makro!(bar);
= note: import resolution is stuck, try simplifying macro imports = note: import resolution is stuck, try simplifying macro imports
error[E0425]: cannot find function `bar` in this scope error[E0425]: cannot find function `bar` in this scope
--> $DIR/issue-59764.rs:13:5 --> $DIR/issue-59764.rs:28:5
| |
LL | bar(); LL | bar();
| ^^^ not found in this scope | ^^^ not found in this scope
error: aborting due to 3 previous errors error: aborting due to 4 previous errors
Some errors occurred: E0425, E0432. Some errors occurred: E0425, E0432.
For more information about an error, try `rustc --explain E0425`. For more information about an error, try `rustc --explain E0425`.