Skip to content

Commit d589cf9

Browse files
committed
Handle renamed imports.
This commit extends the suggestion to handle imports that are aliased to another name.
1 parent d84907b commit d589cf9

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

src/librustc_resolve/error_reporting.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syntax::symbol::keywords;
1111
use syntax_pos::Span;
1212

1313
use crate::macros::ParentScope;
14-
use crate::resolve_imports::{ImportDirective, ImportResolver};
14+
use crate::resolve_imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver};
1515
use crate::{import_candidate_to_enum_paths, is_self_type, is_self_value, path_names_to_string};
1616
use crate::{AssocSuggestion, CrateLint, ImportSuggestion, ModuleOrUniformRoot, PathResult,
1717
PathSource, Resolver, Segment, Suggestion};
@@ -610,11 +610,16 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
610610
let resolution = resolutions.get(&(ident, MacroNS))?;
611611
let binding = resolution.borrow().binding()?;
612612
if let Def::Macro(_, MacroKind::Bang) = binding.def() {
613-
let name = crate_module.kind.name().unwrap();
613+
let module_name = crate_module.kind.name().unwrap();
614+
let import = match directive.subclass {
615+
ImportDirectiveSubclass::SingleImport { source, target, .. } if source != target =>
616+
format!("{} as {}", source, target),
617+
_ => format!("{}", ident),
618+
};
614619
let suggestion = Some((
615620
directive.span,
616621
String::from("a macro with this name exists at the root of the crate"),
617-
format!("{}::{}", name, ident),
622+
format!("{}::{}", module_name, import),
618623
Applicability::MaybeIncorrect,
619624
));
620625
let note = vec![

src/librustc_resolve/resolve_imports.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,21 +1111,17 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
11111111
}
11121112
});
11131113

1114-
let (suggestion, note) = if let Some((suggestion, note)) =
1115-
self.check_for_module_export_macro(directive, module, ident)
1116-
{
1117-
1118-
(
1119-
suggestion.or_else(||
1120-
find_best_match_for_name(names, &ident.as_str(), None)
1121-
.map(|suggestion|
1122-
(ident.span, String::from("a similar name exists in the module"),
1123-
suggestion.to_string(), Applicability::MaybeIncorrect)
1124-
)),
1125-
note,
1126-
)
1127-
} else {
1128-
(None, Vec::new())
1114+
let lev_suggestion = find_best_match_for_name(names, &ident.as_str(), None)
1115+
.map(|suggestion|
1116+
(ident.span, String::from("a similar name exists in the module"),
1117+
suggestion.to_string(), Applicability::MaybeIncorrect)
1118+
);
1119+
1120+
let (suggestion, note) = match self.check_for_module_export_macro(
1121+
directive, module, ident,
1122+
) {
1123+
Some((suggestion, note)) => (suggestion.or(lev_suggestion), note),
1124+
_ => (lev_suggestion, Vec::new()),
11291125
};
11301126

11311127
let label = match module {

src/test/ui/issue-59764.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
// edition:2018
44
// run-rustfix
55

6+
#![allow(warnings)]
7+
8+
// This tests the suggestion to import macros from the root of a crate. This aims to capture
9+
// the case where a user attempts to import a macro from the definition location instead of the
10+
// root of the crate and the macro is annotated with `#![macro_export]`.
11+
12+
// Edge cases..
13+
14+
mod renamed_import {
15+
use issue_59764::makro as baz;
16+
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]
17+
}
18+
19+
// Simple case..
20+
621
use issue_59764::makro;
722
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]
823

src/test/ui/issue-59764.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
// edition:2018
44
// run-rustfix
55

6+
#![allow(warnings)]
7+
8+
// This tests the suggestion to import macros from the root of a crate. This aims to capture
9+
// the case where a user attempts to import a macro from the definition location instead of the
10+
// root of the crate and the macro is annotated with `#![macro_export]`.
11+
12+
// Edge cases..
13+
14+
mod renamed_import {
15+
use issue_59764::foo::makro as baz;
16+
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]
17+
}
18+
19+
// Simple case..
20+
621
use issue_59764::foo::makro;
722
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]
823

src/test/ui/issue-59764.stderr

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
error[E0432]: unresolved import `issue_59764::foo::makro`
2-
--> $DIR/issue-59764.rs:6:5
2+
--> $DIR/issue-59764.rs:15:9
3+
|
4+
LL | use issue_59764::foo::makro as baz;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `makro` in `foo`
6+
|
7+
= 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
8+
help: a macro with this name exists at the root of the crate
9+
|
10+
LL | use issue_59764::makro as baz;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0432]: unresolved import `issue_59764::foo::makro`
14+
--> $DIR/issue-59764.rs:21:5
315
|
416
LL | use issue_59764::foo::makro;
517
| ^^^^^^^^^^^^^^^^^^^^^^^ no `makro` in `foo`
@@ -11,20 +23,20 @@ LL | use issue_59764::makro;
1123
| ^^^^^^^^^^^^^^^^^^
1224

1325
error: cannot determine resolution for the macro `makro`
14-
--> $DIR/issue-59764.rs:9:1
26+
--> $DIR/issue-59764.rs:24:1
1527
|
1628
LL | makro!(bar);
1729
| ^^^^^
1830
|
1931
= note: import resolution is stuck, try simplifying macro imports
2032

2133
error[E0425]: cannot find function `bar` in this scope
22-
--> $DIR/issue-59764.rs:13:5
34+
--> $DIR/issue-59764.rs:28:5
2335
|
2436
LL | bar();
2537
| ^^^ not found in this scope
2638

27-
error: aborting due to 3 previous errors
39+
error: aborting due to 4 previous errors
2840

2941
Some errors occurred: E0425, E0432.
3042
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)