Skip to content

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

0 commit comments

Comments
 (0)