Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 329c052

Browse files
committed
resolve: Visit all scopes to collect suggestion candidates for unresolved macros
1 parent 79f0d88 commit 329c052

18 files changed

+258
-144
lines changed

src/librustc/hir/def.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,12 @@ impl<Id> Res<Id> {
398398
Res::Err => Res::Err,
399399
}
400400
}
401+
402+
pub fn macro_kind(self) -> Option<MacroKind> {
403+
match self {
404+
Res::Def(DefKind::Macro(kind), _) => Some(kind),
405+
Res::NonMacroAttr(..) => Some(MacroKind::Attr),
406+
_ => None,
407+
}
408+
}
401409
}

src/librustc_resolve/diagnostics.rs

Lines changed: 222 additions & 108 deletions
Large diffs are not rendered by default.

src/librustc_resolve/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ mod check_unused;
8585
mod build_reduced_graph;
8686
mod resolve_imports;
8787

88-
fn is_known_tool(name: Name) -> bool {
89-
["clippy", "rustfmt"].contains(&&*name.as_str())
90-
}
88+
const KNOWN_TOOLS: &[Name] = &[sym::clippy, sym::rustfmt];
9189

9290
enum Weak {
9391
Yes,
@@ -1498,11 +1496,7 @@ impl<'a> NameBinding<'a> {
14981496
}
14991497

15001498
fn macro_kind(&self) -> Option<MacroKind> {
1501-
match self.res() {
1502-
Res::Def(DefKind::Macro(kind), _) => Some(kind),
1503-
Res::NonMacroAttr(..) => Some(MacroKind::Attr),
1504-
_ => None,
1505-
}
1499+
self.res().macro_kind()
15061500
}
15071501

15081502
fn descr(&self) -> &'static str {
@@ -2390,7 +2384,7 @@ impl<'a> Resolver<'a> {
23902384
return Some(LexicalScopeBinding::Item(binding));
23912385
}
23922386
}
2393-
if ns == TypeNS && is_known_tool(ident.name) {
2387+
if ns == TypeNS && KNOWN_TOOLS.contains(&ident.name) {
23942388
let binding = (Res::ToolMod, ty::Visibility::Public,
23952389
DUMMY_SP, Mark::root()).to_name_binding(self.arenas);
23962390
return Some(LexicalScopeBinding::Item(binding));

src/librustc_resolve/macros.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{AmbiguityError, AmbiguityKind, AmbiguityErrorMisc, Determinacy};
22
use crate::{CrateLint, Resolver, ResolutionError, Scope, ScopeSet, ParentScope, Weak};
33
use crate::{Module, ModuleKind, NameBinding, NameBindingKind, PathResult, Segment, ToNameBinding};
4-
use crate::{is_known_tool, resolve_error};
4+
use crate::{resolve_error, KNOWN_TOOLS};
55
use crate::ModuleOrUniformRoot;
66
use crate::Namespace::*;
77
use crate::build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
@@ -57,10 +57,10 @@ impl<'a> InvocationData<'a> {
5757
/// Not modularized, can shadow previous legacy bindings, etc.
5858
#[derive(Debug)]
5959
pub struct LegacyBinding<'a> {
60-
binding: &'a NameBinding<'a>,
60+
crate binding: &'a NameBinding<'a>,
6161
/// Legacy scope into which the `macro_rules` item was planted.
6262
crate parent_legacy_scope: LegacyScope<'a>,
63-
ident: Ident,
63+
crate ident: Ident,
6464
}
6565

6666
/// The scope introduced by a `macro_rules!` macro.
@@ -582,7 +582,7 @@ impl<'a> Resolver<'a> {
582582
}
583583
}
584584
Scope::ToolPrelude => {
585-
if use_prelude && is_known_tool(ident.name) {
585+
if use_prelude && KNOWN_TOOLS.contains(&ident.name) {
586586
let binding = (Res::ToolMod, ty::Visibility::Public,
587587
DUMMY_SP, Mark::root()).to_name_binding(this.arenas);
588588
Ok((binding, Flags::PRELUDE))
@@ -805,7 +805,7 @@ impl<'a> Resolver<'a> {
805805
let msg =
806806
format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
807807
let mut err = self.session.struct_span_err(ident.span, &msg);
808-
self.suggest_macro_name(ident.name, kind, &mut err, ident.span);
808+
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident);
809809
err.emit();
810810
}
811811
}

src/libsyntax_pos/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ symbols! {
603603
rustc_then_this_would_need,
604604
rustc_variance,
605605
rustdoc,
606+
rustfmt,
606607
rust_eh_personality,
607608
rust_eh_unwind_resume,
608609
rust_oom,

src/test/ui/derives/deriving-meta-unknown-trait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot find derive macro `Eqr` in this scope
22
--> $DIR/deriving-meta-unknown-trait.rs:1:10
33
|
44
LL | #[derive(Eqr)]
5-
| ^^^ help: try: `Eq`
5+
| ^^^ help: a derive macro with a similar name exists: `Eq`
66

77
error: aborting due to previous error
88

src/test/ui/hygiene/no_implicit_prelude-2018.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error: cannot find macro `print!` in this scope
33
|
44
LL | print!();
55
| ^^^^^
6-
|
7-
= help: have you added the `#[macro_use]` on the module/import?
86

97
error: aborting due to previous error
108

src/test/ui/hygiene/no_implicit_prelude.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ error: cannot find macro `panic!` in this scope
1313
LL | assert_eq!(0, 0);
1414
| ^^^^^^^^^^^^^^^^^
1515
|
16-
= help: have you added the `#[macro_use]` on the module/import?
1716
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
1817

1918
error[E0599]: no method named `clone` found for type `()` in the current scope

src/test/ui/issues/issue-49074.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot find attribute macro `marco_use` in this scope
22
--> $DIR/issue-49074.rs:3:3
33
|
44
LL | #[marco_use] // typo
5-
| ^^^^^^^^^
5+
| ^^^^^^^^^ help: a built-in attribute with a similar name exists: `macro_use`
66

77
error: cannot find macro `bar!` in this scope
88
--> $DIR/issue-49074.rs:12:4

src/test/ui/macros/macro-name-typo.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot find macro `printlx!` in this scope
22
--> $DIR/macro-name-typo.rs:2:5
33
|
44
LL | printlx!("oh noes!");
5-
| ^^^^^^^ help: you could try the macro: `println`
5+
| ^^^^^^^ help: a macro with a similar name exists: `println`
66

77
error: aborting due to previous error
88

src/test/ui/macros/macro-path-prelude-fail-3.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot find macro `inline!` in this scope
22
--> $DIR/macro-path-prelude-fail-3.rs:2:5
33
|
44
LL | inline!();
5-
| ^^^^^^ help: you could try the macro: `line`
5+
| ^^^^^^ help: a macro with a similar name exists: `line`
66

77
error: aborting due to previous error
88

src/test/ui/macros/macro-reexport-removed.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error: cannot find attribute macro `macro_reexport` in this scope
1414
--> $DIR/macro-reexport-removed.rs:5:3
1515
|
1616
LL | #[macro_reexport(macro_one)]
17-
| ^^^^^^^^^^^^^^
17+
| ^^^^^^^^^^^^^^ help: a built-in attribute with a similar name exists: `macro_export`
1818

1919
error: aborting due to 2 previous errors
2020

src/test/ui/macros/macro-use-wrong-name.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot find macro `macro_two!` in this scope
22
--> $DIR/macro-use-wrong-name.rs:7:5
33
|
44
LL | macro_two!();
5-
| ^^^^^^^^^ help: you could try the macro: `macro_one`
5+
| ^^^^^^^^^ help: a macro with a similar name exists: `macro_one`
66

77
error: aborting due to previous error
88

src/test/ui/macros/macro_undefined.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot find macro `k!` in this scope
22
--> $DIR/macro_undefined.rs:11:5
33
|
44
LL | k!();
5-
| ^ help: you could try the macro: `kl`
5+
| ^ help: a macro with a similar name exists: `kl`
66

77
error: aborting due to previous error
88

src/test/ui/proc-macro/proc-macro-attributes.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot find attribute macro `C` in this scope
22
--> $DIR/proc-macro-attributes.rs:7:3
33
|
44
LL | #[C]
5-
| ^
5+
| ^ help: a derive helper attribute with a similar name exists: `B`
66

77
error[E0659]: `B` is ambiguous (derive helper attribute vs any other name)
88
--> $DIR/proc-macro-attributes.rs:6:3

src/test/ui/proc-macro/resolve-error.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error: cannot find derive macro `FooWithLongNan` in this scope
22
--> $DIR/resolve-error.rs:22:10
33
|
44
LL | #[derive(FooWithLongNan)]
5-
| ^^^^^^^^^^^^^^ help: try: `FooWithLongName`
5+
| ^^^^^^^^^^^^^^ help: a derive macro with a similar name exists: `FooWithLongName`
66

77
error: cannot find attribute macro `attr_proc_macra` in this scope
88
--> $DIR/resolve-error.rs:27:3
99
|
1010
LL | #[attr_proc_macra]
11-
| ^^^^^^^^^^^^^^^ help: try: `attr_proc_macro`
11+
| ^^^^^^^^^^^^^^^ help: an attribute macro with a similar name exists: `attr_proc_macro`
1212

1313
error: cannot find attribute macro `FooWithLongNan` in this scope
1414
--> $DIR/resolve-error.rs:31:3
@@ -20,13 +20,13 @@ error: cannot find derive macro `Dlone` in this scope
2020
--> $DIR/resolve-error.rs:34:10
2121
|
2222
LL | #[derive(Dlone)]
23-
| ^^^^^ help: try: `Clone`
23+
| ^^^^^ help: a derive macro with a similar name exists: `Clone`
2424

2525
error: cannot find derive macro `Dlona` in this scope
2626
--> $DIR/resolve-error.rs:38:10
2727
|
2828
LL | #[derive(Dlona)]
29-
| ^^^^^ help: try: `Clona`
29+
| ^^^^^ help: a derive macro with a similar name exists: `Clona`
3030

3131
error: cannot find derive macro `attr_proc_macra` in this scope
3232
--> $DIR/resolve-error.rs:42:10
@@ -38,13 +38,13 @@ error: cannot find macro `FooWithLongNama!` in this scope
3838
--> $DIR/resolve-error.rs:47:5
3939
|
4040
LL | FooWithLongNama!();
41-
| ^^^^^^^^^^^^^^^ help: you could try the macro: `FooWithLongNam`
41+
| ^^^^^^^^^^^^^^^ help: a macro with a similar name exists: `FooWithLongNam`
4242

4343
error: cannot find macro `attr_proc_macra!` in this scope
4444
--> $DIR/resolve-error.rs:50:5
4545
|
4646
LL | attr_proc_macra!();
47-
| ^^^^^^^^^^^^^^^ help: you could try the macro: `attr_proc_mac`
47+
| ^^^^^^^^^^^^^^^ help: a macro with a similar name exists: `attr_proc_mac`
4848

4949
error: cannot find macro `Dlona!` in this scope
5050
--> $DIR/resolve-error.rs:53:5
@@ -56,7 +56,7 @@ error: cannot find macro `bang_proc_macrp!` in this scope
5656
--> $DIR/resolve-error.rs:56:5
5757
|
5858
LL | bang_proc_macrp!();
59-
| ^^^^^^^^^^^^^^^ help: you could try the macro: `bang_proc_macro`
59+
| ^^^^^^^^^^^^^^^ help: a macro with a similar name exists: `bang_proc_macro`
6060

6161
error: aborting due to 10 previous errors
6262

src/test/ui/resolve/levenshtein.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0412]: cannot find type `esize` in this scope
22
--> $DIR/levenshtein.rs:5:11
33
|
44
LL | fn foo(c: esize) {} // Misspelled primitive type name.
5-
| ^^^^^ help: a primitive type with a similar name exists: `isize`
5+
| ^^^^^ help: a builtin type with a similar name exists: `isize`
66

77
error[E0412]: cannot find type `Baz` in this scope
88
--> $DIR/levenshtein.rs:10:10

src/test/ui/suggestions/attribute-typos.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ error: cannot find attribute macro `rustc_err` in this scope
1111
--> $DIR/attribute-typos.rs:7:3
1212
|
1313
LL | #[rustc_err]
14-
| ^^^^^^^^^
14+
| ^^^^^^^^^ help: a built-in attribute with a similar name exists: `rustc_error`
1515

1616
error: cannot find attribute macro `tests` in this scope
1717
--> $DIR/attribute-typos.rs:4:3
1818
|
1919
LL | #[tests]
20-
| ^^^^^ help: try: `test`
20+
| ^^^^^ help: an attribute macro with a similar name exists: `test`
2121

2222
error: cannot find attribute macro `deprcated` in this scope
2323
--> $DIR/attribute-typos.rs:1:3
2424
|
2525
LL | #[deprcated]
26-
| ^^^^^^^^^
26+
| ^^^^^^^^^ help: a built-in attribute with a similar name exists: `deprecated`
2727

2828
error: aborting due to 4 previous errors
2929

0 commit comments

Comments
 (0)