From fed6131c4185aabcd8fa3e28851373944a407d21 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 13:35:51 +0200 Subject: [PATCH 01/11] Add note to 'macro not found' to point to identically-named imports. --- compiler/rustc_resolve/src/macros.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 7f86f891c4450..89b153a6c0e42 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -19,7 +19,7 @@ use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::compile_declarative_macro; use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion}; use rustc_feature::is_builtin_attr_name; -use rustc_hir::def::{self, DefKind, NonMacroAttrKind}; +use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind}; use rustc_hir::def_id::{CrateNum, LocalDefId}; use rustc_hir::PrimTy; use rustc_middle::middle::stability; @@ -1115,6 +1115,24 @@ impl<'a> Resolver<'a> { let msg = format!("cannot find {} `{}` in this scope", expected, ident); let mut err = self.session.struct_span_err(ident.span, &msg); self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident); + if let Ok(binding) = self.early_resolve_ident_in_lexical_scope( + ident, + ScopeSet::All(Namespace::TypeNS, false), + &parent_scope, + false, + false, + ident.span, + ) { + if let crate::NameBindingKind::Import { import, .. } = binding.kind { + err.span_note( + import.span, + &format!( + "`{}` is imported here, but it is not a {}", + ident, expected + ), + ); + } + } err.emit(); } } From 9051c056edd91b5e66ea31cb275bc8877a989da8 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 13:37:27 +0200 Subject: [PATCH 02/11] Add test for macro-not-found-but-name-imported-here note. --- src/test/ui/derives/issue-88206.rs | 18 ++++++++++++++++++ src/test/ui/derives/issue-88206.stderr | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/test/ui/derives/issue-88206.rs create mode 100644 src/test/ui/derives/issue-88206.stderr diff --git a/src/test/ui/derives/issue-88206.rs b/src/test/ui/derives/issue-88206.rs new file mode 100644 index 0000000000000..476c346792853 --- /dev/null +++ b/src/test/ui/derives/issue-88206.rs @@ -0,0 +1,18 @@ +// compile-flags: -Z deduplicate-diagnostics=yes + +#![warn(unused_imports)] +//~^ NOTE lint level + +mod hey { + pub trait Serialize {} +} + +use hey::Serialize; +//~^ WARNING unused import +//~| NOTE `Serialize` is imported here + +#[derive(Serialize)] +//~^ ERROR cannot find derive macro `Serialize` +struct A; + +fn main() {} diff --git a/src/test/ui/derives/issue-88206.stderr b/src/test/ui/derives/issue-88206.stderr new file mode 100644 index 0000000000000..8a0cb7616c25b --- /dev/null +++ b/src/test/ui/derives/issue-88206.stderr @@ -0,0 +1,26 @@ +error: cannot find derive macro `Serialize` in this scope + --> $DIR/issue-88206.rs:14:10 + | +LL | #[derive(Serialize)] + | ^^^^^^^^^ + | +note: `Serialize` is imported here, but it is not a derive macro + --> $DIR/issue-88206.rs:10:5 + | +LL | use hey::Serialize; + | ^^^^^^^^^^^^^^ + +warning: unused import: `hey::Serialize` + --> $DIR/issue-88206.rs:10:5 + | +LL | use hey::Serialize; + | ^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/issue-88206.rs:3:9 + | +LL | #![warn(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to previous error; 1 warning emitted + From 7977cb43b01af009434b54e4136c7e460e1f733f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 15:02:35 +0200 Subject: [PATCH 03/11] Look for macro names in all namespaces for diagnostics. --- compiler/rustc_resolve/src/diagnostics.rs | 33 +++++++++++++++++++++++ compiler/rustc_resolve/src/macros.rs | 20 +------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 3cf042687562b..40a98d74f33c4 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -956,9 +956,42 @@ impl<'a> Resolver<'a> { if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) { let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident); err.span_note(ident.span, &msg); + return; } if self.macro_names.contains(&ident.normalize_to_macros_2_0()) { err.help("have you added the `#[macro_use]` on the module/import?"); + return; + } + for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] { + if let Ok(binding) = self.early_resolve_ident_in_lexical_scope( + ident, + ScopeSet::All(ns, false), + &parent_scope, + false, + false, + ident.span, + ) { + let it_is = match binding.macro_kind() { + Some(MacroKind::Bang) => "it is a function-like macro".to_string(), + Some(kind) => format!("it is {} {}", kind.article(), kind.descr_expected()), + None => format!( + "it is not {} {}", + macro_kind.article(), + macro_kind.descr_expected() + ), + }; + if let crate::NameBindingKind::Import { import, .. } = binding.kind { + if !import.span.is_dummy() { + err.span_note( + import.span, + &format!("`{}` is imported here, but {}", ident, it_is), + ); + return; + } + } + err.note(&format!("`{}` is in scope, but {}", ident, it_is)); + return; + } } } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 89b153a6c0e42..7f86f891c4450 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -19,7 +19,7 @@ use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::compile_declarative_macro; use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion}; use rustc_feature::is_builtin_attr_name; -use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind}; +use rustc_hir::def::{self, DefKind, NonMacroAttrKind}; use rustc_hir::def_id::{CrateNum, LocalDefId}; use rustc_hir::PrimTy; use rustc_middle::middle::stability; @@ -1115,24 +1115,6 @@ impl<'a> Resolver<'a> { let msg = format!("cannot find {} `{}` in this scope", expected, ident); let mut err = self.session.struct_span_err(ident.span, &msg); self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident); - if let Ok(binding) = self.early_resolve_ident_in_lexical_scope( - ident, - ScopeSet::All(Namespace::TypeNS, false), - &parent_scope, - false, - false, - ident.span, - ) { - if let crate::NameBindingKind::Import { import, .. } = binding.kind { - err.span_note( - import.span, - &format!( - "`{}` is imported here, but it is not a {}", - ident, expected - ), - ); - } - } err.emit(); } } From 11c879d209d71700e5fa955fdfbbc0883768a942 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 15:05:13 +0200 Subject: [PATCH 04/11] Add tests for macro-not-found diagnostics. --- src/test/ui/derives/issue-88206.rs | 18 ---- src/test/ui/derives/issue-88206.stderr | 26 ------ src/test/ui/macros/issue-88206.rs | 63 +++++++++++++ src/test/ui/macros/issue-88206.stderr | 120 +++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 44 deletions(-) delete mode 100644 src/test/ui/derives/issue-88206.rs delete mode 100644 src/test/ui/derives/issue-88206.stderr create mode 100644 src/test/ui/macros/issue-88206.rs create mode 100644 src/test/ui/macros/issue-88206.stderr diff --git a/src/test/ui/derives/issue-88206.rs b/src/test/ui/derives/issue-88206.rs deleted file mode 100644 index 476c346792853..0000000000000 --- a/src/test/ui/derives/issue-88206.rs +++ /dev/null @@ -1,18 +0,0 @@ -// compile-flags: -Z deduplicate-diagnostics=yes - -#![warn(unused_imports)] -//~^ NOTE lint level - -mod hey { - pub trait Serialize {} -} - -use hey::Serialize; -//~^ WARNING unused import -//~| NOTE `Serialize` is imported here - -#[derive(Serialize)] -//~^ ERROR cannot find derive macro `Serialize` -struct A; - -fn main() {} diff --git a/src/test/ui/derives/issue-88206.stderr b/src/test/ui/derives/issue-88206.stderr deleted file mode 100644 index 8a0cb7616c25b..0000000000000 --- a/src/test/ui/derives/issue-88206.stderr +++ /dev/null @@ -1,26 +0,0 @@ -error: cannot find derive macro `Serialize` in this scope - --> $DIR/issue-88206.rs:14:10 - | -LL | #[derive(Serialize)] - | ^^^^^^^^^ - | -note: `Serialize` is imported here, but it is not a derive macro - --> $DIR/issue-88206.rs:10:5 - | -LL | use hey::Serialize; - | ^^^^^^^^^^^^^^ - -warning: unused import: `hey::Serialize` - --> $DIR/issue-88206.rs:10:5 - | -LL | use hey::Serialize; - | ^^^^^^^^^^^^^^ - | -note: the lint level is defined here - --> $DIR/issue-88206.rs:3:9 - | -LL | #![warn(unused_imports)] - | ^^^^^^^^^^^^^^ - -error: aborting due to previous error; 1 warning emitted - diff --git a/src/test/ui/macros/issue-88206.rs b/src/test/ui/macros/issue-88206.rs new file mode 100644 index 0000000000000..67b7448a3ed83 --- /dev/null +++ b/src/test/ui/macros/issue-88206.rs @@ -0,0 +1,63 @@ +// compile-flags: -Z deduplicate-diagnostics=yes + +#![warn(unused_imports)] +//~^ NOTE lint level + +use std::str::*; +//~^ WARNING unused import +//~| NOTE `from_utf8` is imported here, but it is not a macro +//~| NOTE `from_utf8_mut` is imported here, but it is not a derive macro +//~| NOTE `from_utf8_unchecked` is imported here, but it is not an attribute + +mod hey { + pub trait Serialize {} + pub trait Deserialize {} +} + +use hey::{Serialize, Deserialize}; +//~^ WARNING unused import +//~| NOTE `Serialize` is imported here, but it is not a derive macro +//~| NOTE `Deserialize` is imported here, but it is not an attribute + +#[derive(Serialize)] +//~^ ERROR cannot find derive macro `Serialize` +struct A; + +#[derive(from_utf8_mut)] +//~^ ERROR cannot find derive macro `from_utf8_mut` +struct B; + +#[derive(println)] +//~^ ERROR cannot find derive macro `println` +//~| NOTE `println` is in scope, but it is a function-like macro +struct C; + +#[Deserialize] +//~^ ERROR cannot find attribute `Deserialize` +struct D; + +#[from_utf8_unchecked] +//~^ ERROR cannot find attribute `from_utf8_unchecked` +struct E; + +#[println] +//~^ ERROR cannot find attribute `println` +//~| NOTE `println` is in scope, but it is a function-like macro +struct F; + +fn main() { + from_utf8!(); + //~^ ERROR cannot find macro `from_utf8` + + Box!(); + //~^ ERROR cannot find macro `Box` + //~| NOTE `Box` is in scope, but it is not a macro + + Copy!(); + //~^ ERROR cannot find macro `Copy` + //~| NOTE `Copy` is in scope, but it is a derive macro + + test!(); + //~^ ERROR cannot find macro `test` + //~| NOTE `test` is in scope, but it is an attribute +} diff --git a/src/test/ui/macros/issue-88206.stderr b/src/test/ui/macros/issue-88206.stderr new file mode 100644 index 0000000000000..4e45de2ee3ed3 --- /dev/null +++ b/src/test/ui/macros/issue-88206.stderr @@ -0,0 +1,120 @@ +error: cannot find macro `test` in this scope + --> $DIR/issue-88206.rs:60:5 + | +LL | test!(); + | ^^^^ + | + = note: `test` is in scope, but it is an attribute + +error: cannot find macro `Copy` in this scope + --> $DIR/issue-88206.rs:56:5 + | +LL | Copy!(); + | ^^^^ + | + = note: `Copy` is in scope, but it is a derive macro + +error: cannot find macro `Box` in this scope + --> $DIR/issue-88206.rs:52:5 + | +LL | Box!(); + | ^^^ + | + = note: `Box` is in scope, but it is not a macro + +error: cannot find macro `from_utf8` in this scope + --> $DIR/issue-88206.rs:49:5 + | +LL | from_utf8!(); + | ^^^^^^^^^ + | +note: `from_utf8` is imported here, but it is not a macro + --> $DIR/issue-88206.rs:6:5 + | +LL | use std::str::*; + | ^^^^^^^^^^^ + +error: cannot find attribute `println` in this scope + --> $DIR/issue-88206.rs:43:3 + | +LL | #[println] + | ^^^^^^^ + | + = note: `println` is in scope, but it is a function-like macro + +error: cannot find attribute `from_utf8_unchecked` in this scope + --> $DIR/issue-88206.rs:39:3 + | +LL | #[from_utf8_unchecked] + | ^^^^^^^^^^^^^^^^^^^ + | +note: `from_utf8_unchecked` is imported here, but it is not an attribute + --> $DIR/issue-88206.rs:6:5 + | +LL | use std::str::*; + | ^^^^^^^^^^^ + +error: cannot find attribute `Deserialize` in this scope + --> $DIR/issue-88206.rs:35:3 + | +LL | #[Deserialize] + | ^^^^^^^^^^^ + | +note: `Deserialize` is imported here, but it is not an attribute + --> $DIR/issue-88206.rs:17:22 + | +LL | use hey::{Serialize, Deserialize}; + | ^^^^^^^^^^^ + +error: cannot find derive macro `println` in this scope + --> $DIR/issue-88206.rs:30:10 + | +LL | #[derive(println)] + | ^^^^^^^ + | + = note: `println` is in scope, but it is a function-like macro + +error: cannot find derive macro `from_utf8_mut` in this scope + --> $DIR/issue-88206.rs:26:10 + | +LL | #[derive(from_utf8_mut)] + | ^^^^^^^^^^^^^ + | +note: `from_utf8_mut` is imported here, but it is not a derive macro + --> $DIR/issue-88206.rs:6:5 + | +LL | use std::str::*; + | ^^^^^^^^^^^ + +error: cannot find derive macro `Serialize` in this scope + --> $DIR/issue-88206.rs:22:10 + | +LL | #[derive(Serialize)] + | ^^^^^^^^^ + | +note: `Serialize` is imported here, but it is not a derive macro + --> $DIR/issue-88206.rs:17:11 + | +LL | use hey::{Serialize, Deserialize}; + | ^^^^^^^^^ + +warning: unused import: `std::str::*` + --> $DIR/issue-88206.rs:6:5 + | +LL | use std::str::*; + | ^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/issue-88206.rs:3:9 + | +LL | #![warn(unused_imports)] + | ^^^^^^^^^^^^^^ + +warning: unused imports: `Deserialize`, `Serialize` + --> $DIR/issue-88206.rs:17:11 + | +LL | use hey::{Serialize, Deserialize}; + | ^^^^^^^^^ ^^^^^^^^^^^ + +error: aborting due to 10 previous errors; 2 warnings emitted + From ce95a57a723e260b9ce6e5c54a3b77c536237b3d Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 15:05:35 +0200 Subject: [PATCH 05/11] Update tests. --- src/test/ui/issues/issue-11692-2.stderr | 2 ++ src/test/ui/macros/issue-88228.rs | 1 + src/test/ui/macros/issue-88228.stderr | 4 +++- src/test/ui/macros/macro-path-prelude-fail-3.stderr | 2 ++ .../ui/proc-macro/macro-namespace-reserved-2.stderr | 10 ++++++++++ .../tool-attributes/tool-attributes-misplaced-1.stderr | 8 ++++++++ 6 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/test/ui/issues/issue-11692-2.stderr b/src/test/ui/issues/issue-11692-2.stderr index f021943da32da..de705e740264e 100644 --- a/src/test/ui/issues/issue-11692-2.stderr +++ b/src/test/ui/issues/issue-11692-2.stderr @@ -3,6 +3,8 @@ error: cannot find macro `test` in this scope | LL | concat!(test!()); | ^^^^ + | + = note: `test` is in scope, but it is an attribute error: aborting due to previous error diff --git a/src/test/ui/macros/issue-88228.rs b/src/test/ui/macros/issue-88228.rs index 615b865e9f1e1..cbdef5f0d40a9 100644 --- a/src/test/ui/macros/issue-88228.rs +++ b/src/test/ui/macros/issue-88228.rs @@ -13,6 +13,7 @@ struct A; #[derive(println)] //~^ ERROR cannot find derive macro `println` +//~|`println` is in scope, but it is a function-like macro struct B; fn main() { diff --git a/src/test/ui/macros/issue-88228.stderr b/src/test/ui/macros/issue-88228.stderr index b164e39064c97..62afa67a783c9 100644 --- a/src/test/ui/macros/issue-88228.stderr +++ b/src/test/ui/macros/issue-88228.stderr @@ -1,5 +1,5 @@ error: cannot find macro `bla` in this scope - --> $DIR/issue-88228.rs:19:5 + --> $DIR/issue-88228.rs:20:5 | LL | bla!(); | ^^^ @@ -12,6 +12,8 @@ error: cannot find derive macro `println` in this scope | LL | #[derive(println)] | ^^^^^^^ + | + = note: `println` is in scope, but it is a function-like macro error: cannot find derive macro `Bla` in this scope --> $DIR/issue-88228.rs:9:10 diff --git a/src/test/ui/macros/macro-path-prelude-fail-3.stderr b/src/test/ui/macros/macro-path-prelude-fail-3.stderr index 1abb501ec8051..0f448f7cab1f6 100644 --- a/src/test/ui/macros/macro-path-prelude-fail-3.stderr +++ b/src/test/ui/macros/macro-path-prelude-fail-3.stderr @@ -8,6 +8,8 @@ LL | inline!(); | LL | macro_rules! line { | ----------------- similarly named macro `line` defined here + | + = note: `inline` is in scope, but it is an attribute error: aborting due to previous error diff --git a/src/test/ui/proc-macro/macro-namespace-reserved-2.stderr b/src/test/ui/proc-macro/macro-namespace-reserved-2.stderr index a617319faea80..3c7ba5ac9bae7 100644 --- a/src/test/ui/proc-macro/macro-namespace-reserved-2.stderr +++ b/src/test/ui/proc-macro/macro-namespace-reserved-2.stderr @@ -93,30 +93,40 @@ error: cannot find macro `my_macro_attr` in this scope | LL | my_macro_attr!(); | ^^^^^^^^^^^^^ + | + = note: `my_macro_attr` is in scope, but it is an attribute error: cannot find macro `MyTrait` in this scope --> $DIR/macro-namespace-reserved-2.rs:33:5 | LL | MyTrait!(); | ^^^^^^^ + | + = note: `MyTrait` is in scope, but it is a derive macro error: cannot find attribute `my_macro` in this scope --> $DIR/macro-namespace-reserved-2.rs:38:3 | LL | #[my_macro] | ^^^^^^^^ + | + = note: `my_macro` is in scope, but it is a function-like macro error: cannot find derive macro `my_macro` in this scope --> $DIR/macro-namespace-reserved-2.rs:48:10 | LL | #[derive(my_macro)] | ^^^^^^^^ + | + = note: `my_macro` is in scope, but it is a function-like macro error: cannot find derive macro `my_macro` in this scope --> $DIR/macro-namespace-reserved-2.rs:48:10 | LL | #[derive(my_macro)] | ^^^^^^^^ + | + = note: `my_macro` is in scope, but it is a function-like macro error: aborting due to 20 previous errors diff --git a/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr b/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr index 71fd5f1d44a8d..2d20d10346529 100644 --- a/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr +++ b/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr @@ -3,24 +3,32 @@ error: cannot find derive macro `rustfmt` in this scope | LL | #[derive(rustfmt)] | ^^^^^^^ + | + = note: `rustfmt` is in scope, but it is not a derive macro error: cannot find derive macro `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:4:10 | LL | #[derive(rustfmt)] | ^^^^^^^ + | + = note: `rustfmt` is in scope, but it is not a derive macro error: cannot find attribute `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:9:3 | LL | #[rustfmt] | ^^^^^^^ + | + = note: `rustfmt` is in scope, but it is not an attribute error: cannot find macro `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:15:5 | LL | rustfmt!(); | ^^^^^^^ + | + = note: `rustfmt` is in scope, but it is not a macro error[E0573]: expected type, found tool module `rustfmt` --> $DIR/tool-attributes-misplaced-1.rs:1:10 From d834d2a7422886890c861e120af5f6a9e9ca9985 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 16:33:21 +0200 Subject: [PATCH 06/11] Silence confusing 'unused import' warnings. --- compiler/rustc_resolve/src/diagnostics.rs | 3 ++ src/test/ui/macros/issue-88206.rs | 7 +--- src/test/ui/macros/issue-88206.stderr | 50 ++++++++--------------- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 40a98d74f33c4..60f0ad9e30d64 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -986,6 +986,9 @@ impl<'a> Resolver<'a> { import.span, &format!("`{}` is imported here, but {}", ident, it_is), ); + // Silence the 'unused import' warning we might get, + // since this diagnostic already covers that import. + self.record_use(ident, binding, false); return; } } diff --git a/src/test/ui/macros/issue-88206.rs b/src/test/ui/macros/issue-88206.rs index 67b7448a3ed83..f0dab54c149a9 100644 --- a/src/test/ui/macros/issue-88206.rs +++ b/src/test/ui/macros/issue-88206.rs @@ -1,11 +1,9 @@ // compile-flags: -Z deduplicate-diagnostics=yes #![warn(unused_imports)] -//~^ NOTE lint level use std::str::*; -//~^ WARNING unused import -//~| NOTE `from_utf8` is imported here, but it is not a macro +//~^ NOTE `from_utf8` is imported here, but it is not a macro //~| NOTE `from_utf8_mut` is imported here, but it is not a derive macro //~| NOTE `from_utf8_unchecked` is imported here, but it is not an attribute @@ -15,8 +13,7 @@ mod hey { } use hey::{Serialize, Deserialize}; -//~^ WARNING unused import -//~| NOTE `Serialize` is imported here, but it is not a derive macro +//~^ NOTE `Serialize` is imported here, but it is not a derive macro //~| NOTE `Deserialize` is imported here, but it is not an attribute #[derive(Serialize)] diff --git a/src/test/ui/macros/issue-88206.stderr b/src/test/ui/macros/issue-88206.stderr index 4e45de2ee3ed3..b6fd606e28cb2 100644 --- a/src/test/ui/macros/issue-88206.stderr +++ b/src/test/ui/macros/issue-88206.stderr @@ -1,5 +1,5 @@ error: cannot find macro `test` in this scope - --> $DIR/issue-88206.rs:60:5 + --> $DIR/issue-88206.rs:57:5 | LL | test!(); | ^^^^ @@ -7,7 +7,7 @@ LL | test!(); = note: `test` is in scope, but it is an attribute error: cannot find macro `Copy` in this scope - --> $DIR/issue-88206.rs:56:5 + --> $DIR/issue-88206.rs:53:5 | LL | Copy!(); | ^^^^ @@ -15,7 +15,7 @@ LL | Copy!(); = note: `Copy` is in scope, but it is a derive macro error: cannot find macro `Box` in this scope - --> $DIR/issue-88206.rs:52:5 + --> $DIR/issue-88206.rs:49:5 | LL | Box!(); | ^^^ @@ -23,19 +23,19 @@ LL | Box!(); = note: `Box` is in scope, but it is not a macro error: cannot find macro `from_utf8` in this scope - --> $DIR/issue-88206.rs:49:5 + --> $DIR/issue-88206.rs:46:5 | LL | from_utf8!(); | ^^^^^^^^^ | note: `from_utf8` is imported here, but it is not a macro - --> $DIR/issue-88206.rs:6:5 + --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; | ^^^^^^^^^^^ error: cannot find attribute `println` in this scope - --> $DIR/issue-88206.rs:43:3 + --> $DIR/issue-88206.rs:40:3 | LL | #[println] | ^^^^^^^ @@ -43,31 +43,31 @@ LL | #[println] = note: `println` is in scope, but it is a function-like macro error: cannot find attribute `from_utf8_unchecked` in this scope - --> $DIR/issue-88206.rs:39:3 + --> $DIR/issue-88206.rs:36:3 | LL | #[from_utf8_unchecked] | ^^^^^^^^^^^^^^^^^^^ | note: `from_utf8_unchecked` is imported here, but it is not an attribute - --> $DIR/issue-88206.rs:6:5 + --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; | ^^^^^^^^^^^ error: cannot find attribute `Deserialize` in this scope - --> $DIR/issue-88206.rs:35:3 + --> $DIR/issue-88206.rs:32:3 | LL | #[Deserialize] | ^^^^^^^^^^^ | note: `Deserialize` is imported here, but it is not an attribute - --> $DIR/issue-88206.rs:17:22 + --> $DIR/issue-88206.rs:15:22 | LL | use hey::{Serialize, Deserialize}; | ^^^^^^^^^^^ error: cannot find derive macro `println` in this scope - --> $DIR/issue-88206.rs:30:10 + --> $DIR/issue-88206.rs:27:10 | LL | #[derive(println)] | ^^^^^^^ @@ -75,46 +75,28 @@ LL | #[derive(println)] = note: `println` is in scope, but it is a function-like macro error: cannot find derive macro `from_utf8_mut` in this scope - --> $DIR/issue-88206.rs:26:10 + --> $DIR/issue-88206.rs:23:10 | LL | #[derive(from_utf8_mut)] | ^^^^^^^^^^^^^ | note: `from_utf8_mut` is imported here, but it is not a derive macro - --> $DIR/issue-88206.rs:6:5 + --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; | ^^^^^^^^^^^ error: cannot find derive macro `Serialize` in this scope - --> $DIR/issue-88206.rs:22:10 + --> $DIR/issue-88206.rs:19:10 | LL | #[derive(Serialize)] | ^^^^^^^^^ | note: `Serialize` is imported here, but it is not a derive macro - --> $DIR/issue-88206.rs:17:11 + --> $DIR/issue-88206.rs:15:11 | LL | use hey::{Serialize, Deserialize}; | ^^^^^^^^^ -warning: unused import: `std::str::*` - --> $DIR/issue-88206.rs:6:5 - | -LL | use std::str::*; - | ^^^^^^^^^^^ - | -note: the lint level is defined here - --> $DIR/issue-88206.rs:3:9 - | -LL | #![warn(unused_imports)] - | ^^^^^^^^^^^^^^ - -warning: unused imports: `Deserialize`, `Serialize` - --> $DIR/issue-88206.rs:17:11 - | -LL | use hey::{Serialize, Deserialize}; - | ^^^^^^^^^ ^^^^^^^^^^^ - -error: aborting due to 10 previous errors; 2 warnings emitted +error: aborting due to 10 previous errors From 5dea5d7549acbd85f7c2a7b63c8d8aeeb5f6de07 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 17:16:24 +0200 Subject: [PATCH 07/11] Say what things are, instead of what they are not. --- compiler/rustc_resolve/src/diagnostics.rs | 19 ++++--- src/test/ui/macros/issue-88206.rs | 20 ++++--- src/test/ui/macros/issue-88206.stderr | 54 +++++++++++-------- .../tool-attributes-misplaced-1.stderr | 8 +-- 4 files changed, 59 insertions(+), 42 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 60f0ad9e30d64..088596b2cbc06 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -971,20 +971,19 @@ impl<'a> Resolver<'a> { false, ident.span, ) { - let it_is = match binding.macro_kind() { - Some(MacroKind::Bang) => "it is a function-like macro".to_string(), - Some(kind) => format!("it is {} {}", kind.article(), kind.descr_expected()), - None => format!( - "it is not {} {}", - macro_kind.article(), - macro_kind.descr_expected() - ), + let desc = match binding.macro_kind() { + Some(MacroKind::Bang) => "a function-like macro".to_string(), + Some(kind) => format!("{} {}", kind.article(), kind.descr_expected()), + None => { + let res = binding.res(); + format!("{} {}", res.article(), res.descr()) + } }; if let crate::NameBindingKind::Import { import, .. } = binding.kind { if !import.span.is_dummy() { err.span_note( import.span, - &format!("`{}` is imported here, but {}", ident, it_is), + &format!("`{}` is imported here, but it is {}", ident, desc), ); // Silence the 'unused import' warning we might get, // since this diagnostic already covers that import. @@ -992,7 +991,7 @@ impl<'a> Resolver<'a> { return; } } - err.note(&format!("`{}` is in scope, but {}", ident, it_is)); + err.note(&format!("`{}` is in scope, but it is {}", ident, desc)); return; } } diff --git a/src/test/ui/macros/issue-88206.rs b/src/test/ui/macros/issue-88206.rs index f0dab54c149a9..9f1306349e901 100644 --- a/src/test/ui/macros/issue-88206.rs +++ b/src/test/ui/macros/issue-88206.rs @@ -3,18 +3,21 @@ #![warn(unused_imports)] use std::str::*; -//~^ NOTE `from_utf8` is imported here, but it is not a macro -//~| NOTE `from_utf8_mut` is imported here, but it is not a derive macro -//~| NOTE `from_utf8_unchecked` is imported here, but it is not an attribute +//~^ NOTE `from_utf8` is imported here, but it is a function +//~| NOTE `from_utf8_mut` is imported here, but it is a function +//~| NOTE `from_utf8_unchecked` is imported here, but it is a function mod hey { pub trait Serialize {} pub trait Deserialize {} + + pub struct X(i32); } -use hey::{Serialize, Deserialize}; -//~^ NOTE `Serialize` is imported here, but it is not a derive macro -//~| NOTE `Deserialize` is imported here, but it is not an attribute +use hey::{Serialize, Deserialize, X}; +//~^ NOTE `Serialize` is imported here, but it is a trait +//~| NOTE `Deserialize` is imported here, but it is a trait +//~| NOTE `X` is imported here, but it is a struct #[derive(Serialize)] //~^ ERROR cannot find derive macro `Serialize` @@ -48,7 +51,7 @@ fn main() { Box!(); //~^ ERROR cannot find macro `Box` - //~| NOTE `Box` is in scope, but it is not a macro + //~| NOTE `Box` is in scope, but it is a struct Copy!(); //~^ ERROR cannot find macro `Copy` @@ -57,4 +60,7 @@ fn main() { test!(); //~^ ERROR cannot find macro `test` //~| NOTE `test` is in scope, but it is an attribute + + X!(); + //~^ ERROR cannot find macro `X` } diff --git a/src/test/ui/macros/issue-88206.stderr b/src/test/ui/macros/issue-88206.stderr index b6fd606e28cb2..9220c16f6ae88 100644 --- a/src/test/ui/macros/issue-88206.stderr +++ b/src/test/ui/macros/issue-88206.stderr @@ -1,5 +1,17 @@ +error: cannot find macro `X` in this scope + --> $DIR/issue-88206.rs:64:5 + | +LL | X!(); + | ^ + | +note: `X` is imported here, but it is a struct + --> $DIR/issue-88206.rs:17:35 + | +LL | use hey::{Serialize, Deserialize, X}; + | ^ + error: cannot find macro `test` in this scope - --> $DIR/issue-88206.rs:57:5 + --> $DIR/issue-88206.rs:60:5 | LL | test!(); | ^^^^ @@ -7,7 +19,7 @@ LL | test!(); = note: `test` is in scope, but it is an attribute error: cannot find macro `Copy` in this scope - --> $DIR/issue-88206.rs:53:5 + --> $DIR/issue-88206.rs:56:5 | LL | Copy!(); | ^^^^ @@ -15,27 +27,27 @@ LL | Copy!(); = note: `Copy` is in scope, but it is a derive macro error: cannot find macro `Box` in this scope - --> $DIR/issue-88206.rs:49:5 + --> $DIR/issue-88206.rs:52:5 | LL | Box!(); | ^^^ | - = note: `Box` is in scope, but it is not a macro + = note: `Box` is in scope, but it is a struct error: cannot find macro `from_utf8` in this scope - --> $DIR/issue-88206.rs:46:5 + --> $DIR/issue-88206.rs:49:5 | LL | from_utf8!(); | ^^^^^^^^^ | -note: `from_utf8` is imported here, but it is not a macro +note: `from_utf8` is imported here, but it is a function --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; | ^^^^^^^^^^^ error: cannot find attribute `println` in this scope - --> $DIR/issue-88206.rs:40:3 + --> $DIR/issue-88206.rs:43:3 | LL | #[println] | ^^^^^^^ @@ -43,31 +55,31 @@ LL | #[println] = note: `println` is in scope, but it is a function-like macro error: cannot find attribute `from_utf8_unchecked` in this scope - --> $DIR/issue-88206.rs:36:3 + --> $DIR/issue-88206.rs:39:3 | LL | #[from_utf8_unchecked] | ^^^^^^^^^^^^^^^^^^^ | -note: `from_utf8_unchecked` is imported here, but it is not an attribute +note: `from_utf8_unchecked` is imported here, but it is a function --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; | ^^^^^^^^^^^ error: cannot find attribute `Deserialize` in this scope - --> $DIR/issue-88206.rs:32:3 + --> $DIR/issue-88206.rs:35:3 | LL | #[Deserialize] | ^^^^^^^^^^^ | -note: `Deserialize` is imported here, but it is not an attribute - --> $DIR/issue-88206.rs:15:22 +note: `Deserialize` is imported here, but it is a trait + --> $DIR/issue-88206.rs:17:22 | -LL | use hey::{Serialize, Deserialize}; +LL | use hey::{Serialize, Deserialize, X}; | ^^^^^^^^^^^ error: cannot find derive macro `println` in this scope - --> $DIR/issue-88206.rs:27:10 + --> $DIR/issue-88206.rs:30:10 | LL | #[derive(println)] | ^^^^^^^ @@ -75,28 +87,28 @@ LL | #[derive(println)] = note: `println` is in scope, but it is a function-like macro error: cannot find derive macro `from_utf8_mut` in this scope - --> $DIR/issue-88206.rs:23:10 + --> $DIR/issue-88206.rs:26:10 | LL | #[derive(from_utf8_mut)] | ^^^^^^^^^^^^^ | -note: `from_utf8_mut` is imported here, but it is not a derive macro +note: `from_utf8_mut` is imported here, but it is a function --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; | ^^^^^^^^^^^ error: cannot find derive macro `Serialize` in this scope - --> $DIR/issue-88206.rs:19:10 + --> $DIR/issue-88206.rs:22:10 | LL | #[derive(Serialize)] | ^^^^^^^^^ | -note: `Serialize` is imported here, but it is not a derive macro - --> $DIR/issue-88206.rs:15:11 +note: `Serialize` is imported here, but it is a trait + --> $DIR/issue-88206.rs:17:11 | -LL | use hey::{Serialize, Deserialize}; +LL | use hey::{Serialize, Deserialize, X}; | ^^^^^^^^^ -error: aborting due to 10 previous errors +error: aborting due to 11 previous errors diff --git a/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr b/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr index 2d20d10346529..6e875eebb4675 100644 --- a/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr +++ b/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr @@ -4,7 +4,7 @@ error: cannot find derive macro `rustfmt` in this scope LL | #[derive(rustfmt)] | ^^^^^^^ | - = note: `rustfmt` is in scope, but it is not a derive macro + = note: `rustfmt` is in scope, but it is a tool module error: cannot find derive macro `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:4:10 @@ -12,7 +12,7 @@ error: cannot find derive macro `rustfmt` in this scope LL | #[derive(rustfmt)] | ^^^^^^^ | - = note: `rustfmt` is in scope, but it is not a derive macro + = note: `rustfmt` is in scope, but it is a tool module error: cannot find attribute `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:9:3 @@ -20,7 +20,7 @@ error: cannot find attribute `rustfmt` in this scope LL | #[rustfmt] | ^^^^^^^ | - = note: `rustfmt` is in scope, but it is not an attribute + = note: `rustfmt` is in scope, but it is a tool module error: cannot find macro `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:15:5 @@ -28,7 +28,7 @@ error: cannot find macro `rustfmt` in this scope LL | rustfmt!(); | ^^^^^^^ | - = note: `rustfmt` is in scope, but it is not a macro + = note: `rustfmt` is in scope, but it is a tool module error[E0573]: expected type, found tool module `rustfmt` --> $DIR/tool-attributes-misplaced-1.rs:1:10 From 4bd415f70a7af9a64a700772ece354829a777a50 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 17:26:19 +0200 Subject: [PATCH 08/11] Clarify what attribute and derive macros look like. --- compiler/rustc_resolve/src/diagnostics.rs | 3 ++- src/test/ui/issues/issue-11692-2.stderr | 2 +- src/test/ui/macros/issue-88206.stderr | 4 ++-- src/test/ui/macros/macro-path-prelude-fail-3.stderr | 2 +- src/test/ui/proc-macro/macro-namespace-reserved-2.stderr | 4 ++-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 088596b2cbc06..a9e499c9a71c1 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -973,7 +973,8 @@ impl<'a> Resolver<'a> { ) { let desc = match binding.macro_kind() { Some(MacroKind::Bang) => "a function-like macro".to_string(), - Some(kind) => format!("{} {}", kind.article(), kind.descr_expected()), + Some(MacroKind::Attr) => format!("an attribute: `#[{}]`", ident), + Some(MacroKind::Derive) => format!("a derive macro: `#[derive({})]`", ident), None => { let res = binding.res(); format!("{} {}", res.article(), res.descr()) diff --git a/src/test/ui/issues/issue-11692-2.stderr b/src/test/ui/issues/issue-11692-2.stderr index de705e740264e..84746ca2c889c 100644 --- a/src/test/ui/issues/issue-11692-2.stderr +++ b/src/test/ui/issues/issue-11692-2.stderr @@ -4,7 +4,7 @@ error: cannot find macro `test` in this scope LL | concat!(test!()); | ^^^^ | - = note: `test` is in scope, but it is an attribute + = note: `test` is in scope, but it is an attribute: `#[test]` error: aborting due to previous error diff --git a/src/test/ui/macros/issue-88206.stderr b/src/test/ui/macros/issue-88206.stderr index 9220c16f6ae88..c189fc4735713 100644 --- a/src/test/ui/macros/issue-88206.stderr +++ b/src/test/ui/macros/issue-88206.stderr @@ -16,7 +16,7 @@ error: cannot find macro `test` in this scope LL | test!(); | ^^^^ | - = note: `test` is in scope, but it is an attribute + = note: `test` is in scope, but it is an attribute: `#[test]` error: cannot find macro `Copy` in this scope --> $DIR/issue-88206.rs:56:5 @@ -24,7 +24,7 @@ error: cannot find macro `Copy` in this scope LL | Copy!(); | ^^^^ | - = note: `Copy` is in scope, but it is a derive macro + = note: `Copy` is in scope, but it is a derive macro: `#[derive(Copy)]` error: cannot find macro `Box` in this scope --> $DIR/issue-88206.rs:52:5 diff --git a/src/test/ui/macros/macro-path-prelude-fail-3.stderr b/src/test/ui/macros/macro-path-prelude-fail-3.stderr index 0f448f7cab1f6..70900a6bc81d3 100644 --- a/src/test/ui/macros/macro-path-prelude-fail-3.stderr +++ b/src/test/ui/macros/macro-path-prelude-fail-3.stderr @@ -9,7 +9,7 @@ LL | inline!(); LL | macro_rules! line { | ----------------- similarly named macro `line` defined here | - = note: `inline` is in scope, but it is an attribute + = note: `inline` is in scope, but it is an attribute: `#[inline]` error: aborting due to previous error diff --git a/src/test/ui/proc-macro/macro-namespace-reserved-2.stderr b/src/test/ui/proc-macro/macro-namespace-reserved-2.stderr index 3c7ba5ac9bae7..633a6c6a0d3cb 100644 --- a/src/test/ui/proc-macro/macro-namespace-reserved-2.stderr +++ b/src/test/ui/proc-macro/macro-namespace-reserved-2.stderr @@ -94,7 +94,7 @@ error: cannot find macro `my_macro_attr` in this scope LL | my_macro_attr!(); | ^^^^^^^^^^^^^ | - = note: `my_macro_attr` is in scope, but it is an attribute + = note: `my_macro_attr` is in scope, but it is an attribute: `#[my_macro_attr]` error: cannot find macro `MyTrait` in this scope --> $DIR/macro-namespace-reserved-2.rs:33:5 @@ -102,7 +102,7 @@ error: cannot find macro `MyTrait` in this scope LL | MyTrait!(); | ^^^^^^^ | - = note: `MyTrait` is in scope, but it is a derive macro + = note: `MyTrait` is in scope, but it is a derive macro: `#[derive(MyTrait)]` error: cannot find attribute `my_macro` in this scope --> $DIR/macro-namespace-reserved-2.rs:38:3 From a13c66e0a522957591b04237ccc72b411671fc9c Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 17:41:22 +0200 Subject: [PATCH 09/11] Don't confuse the user with notes about tool modules. --- compiler/rustc_resolve/src/diagnostics.rs | 10 +++++----- .../tool-attributes/tool-attributes-misplaced-1.stderr | 8 -------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index a9e499c9a71c1..7b3da373fc9d9 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -971,14 +971,14 @@ impl<'a> Resolver<'a> { false, ident.span, ) { - let desc = match binding.macro_kind() { + let res = binding.res(); + let desc = match res.macro_kind() { Some(MacroKind::Bang) => "a function-like macro".to_string(), Some(MacroKind::Attr) => format!("an attribute: `#[{}]`", ident), Some(MacroKind::Derive) => format!("a derive macro: `#[derive({})]`", ident), - None => { - let res = binding.res(); - format!("{} {}", res.article(), res.descr()) - } + // Don't confuse the user with tool modules. + None if res == Res::ToolMod => continue, + None => format!("{} {}", res.article(), res.descr()), }; if let crate::NameBindingKind::Import { import, .. } = binding.kind { if !import.span.is_dummy() { diff --git a/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr b/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr index 6e875eebb4675..71fd5f1d44a8d 100644 --- a/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr +++ b/src/test/ui/tool-attributes/tool-attributes-misplaced-1.stderr @@ -3,32 +3,24 @@ error: cannot find derive macro `rustfmt` in this scope | LL | #[derive(rustfmt)] | ^^^^^^^ - | - = note: `rustfmt` is in scope, but it is a tool module error: cannot find derive macro `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:4:10 | LL | #[derive(rustfmt)] | ^^^^^^^ - | - = note: `rustfmt` is in scope, but it is a tool module error: cannot find attribute `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:9:3 | LL | #[rustfmt] | ^^^^^^^ - | - = note: `rustfmt` is in scope, but it is a tool module error: cannot find macro `rustfmt` in this scope --> $DIR/tool-attributes-misplaced-1.rs:15:5 | LL | rustfmt!(); | ^^^^^^^ - | - = note: `rustfmt` is in scope, but it is a tool module error[E0573]: expected type, found tool module `rustfmt` --> $DIR/tool-attributes-misplaced-1.rs:1:10 From 4e22bf47d0fb496dea79ec436ad8f5f6fef2418c Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 18:15:07 +0200 Subject: [PATCH 10/11] Show what things are, but also what they are not. --- compiler/rustc_resolve/src/diagnostics.rs | 8 +++++++- src/test/ui/macros/issue-88206.stderr | 14 +++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 7b3da373fc9d9..a6e26d06bb12a 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -978,7 +978,13 @@ impl<'a> Resolver<'a> { Some(MacroKind::Derive) => format!("a derive macro: `#[derive({})]`", ident), // Don't confuse the user with tool modules. None if res == Res::ToolMod => continue, - None => format!("{} {}", res.article(), res.descr()), + None => format!( + "{} {}, not {} {}", + res.article(), + res.descr(), + macro_kind.article(), + macro_kind.descr_expected(), + ), }; if let crate::NameBindingKind::Import { import, .. } = binding.kind { if !import.span.is_dummy() { diff --git a/src/test/ui/macros/issue-88206.stderr b/src/test/ui/macros/issue-88206.stderr index c189fc4735713..0413605fdb7f2 100644 --- a/src/test/ui/macros/issue-88206.stderr +++ b/src/test/ui/macros/issue-88206.stderr @@ -4,7 +4,7 @@ error: cannot find macro `X` in this scope LL | X!(); | ^ | -note: `X` is imported here, but it is a struct +note: `X` is imported here, but it is a struct, not a macro --> $DIR/issue-88206.rs:17:35 | LL | use hey::{Serialize, Deserialize, X}; @@ -32,7 +32,7 @@ error: cannot find macro `Box` in this scope LL | Box!(); | ^^^ | - = note: `Box` is in scope, but it is a struct + = note: `Box` is in scope, but it is a struct, not a macro error: cannot find macro `from_utf8` in this scope --> $DIR/issue-88206.rs:49:5 @@ -40,7 +40,7 @@ error: cannot find macro `from_utf8` in this scope LL | from_utf8!(); | ^^^^^^^^^ | -note: `from_utf8` is imported here, but it is a function +note: `from_utf8` is imported here, but it is a function, not a macro --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; @@ -60,7 +60,7 @@ error: cannot find attribute `from_utf8_unchecked` in this scope LL | #[from_utf8_unchecked] | ^^^^^^^^^^^^^^^^^^^ | -note: `from_utf8_unchecked` is imported here, but it is a function +note: `from_utf8_unchecked` is imported here, but it is a function, not an attribute --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; @@ -72,7 +72,7 @@ error: cannot find attribute `Deserialize` in this scope LL | #[Deserialize] | ^^^^^^^^^^^ | -note: `Deserialize` is imported here, but it is a trait +note: `Deserialize` is imported here, but it is a trait, not an attribute --> $DIR/issue-88206.rs:17:22 | LL | use hey::{Serialize, Deserialize, X}; @@ -92,7 +92,7 @@ error: cannot find derive macro `from_utf8_mut` in this scope LL | #[derive(from_utf8_mut)] | ^^^^^^^^^^^^^ | -note: `from_utf8_mut` is imported here, but it is a function +note: `from_utf8_mut` is imported here, but it is a function, not a derive macro --> $DIR/issue-88206.rs:5:5 | LL | use std::str::*; @@ -104,7 +104,7 @@ error: cannot find derive macro `Serialize` in this scope LL | #[derive(Serialize)] | ^^^^^^^^^ | -note: `Serialize` is imported here, but it is a trait +note: `Serialize` is imported here, but it is a trait, not a derive macro --> $DIR/issue-88206.rs:17:11 | LL | use hey::{Serialize, Deserialize, X}; From 908ce2fd1f8f87a18f005b6d86276649786475d8 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 22 Aug 2021 18:22:06 +0200 Subject: [PATCH 11/11] Improve wording of macro-not-found-but-name-exists note. --- compiler/rustc_resolve/src/diagnostics.rs | 26 ++++++++++++++++------- src/test/ui/macros/issue-88206.rs | 2 +- src/test/ui/macros/issue-88206.stderr | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index a6e26d06bb12a..ca2c22854c4f5 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -971,14 +971,24 @@ impl<'a> Resolver<'a> { false, ident.span, ) { - let res = binding.res(); - let desc = match res.macro_kind() { - Some(MacroKind::Bang) => "a function-like macro".to_string(), - Some(MacroKind::Attr) => format!("an attribute: `#[{}]`", ident), - Some(MacroKind::Derive) => format!("a derive macro: `#[derive({})]`", ident), - // Don't confuse the user with tool modules. - None if res == Res::ToolMod => continue, - None => format!( + let desc = match binding.res() { + Res::Def(DefKind::Macro(MacroKind::Bang), _) => { + "a function-like macro".to_string() + } + Res::Def(DefKind::Macro(MacroKind::Attr), _) | Res::NonMacroAttr(..) => { + format!("an attribute: `#[{}]`", ident) + } + Res::Def(DefKind::Macro(MacroKind::Derive), _) => { + format!("a derive macro: `#[derive({})]`", ident) + } + Res::ToolMod => { + // Don't confuse the user with tool modules. + continue; + } + Res::Def(DefKind::Trait, _) if macro_kind == MacroKind::Derive => { + "only a trait, without a derive macro".to_string() + } + res => format!( "{} {}, not {} {}", res.article(), res.descr(), diff --git a/src/test/ui/macros/issue-88206.rs b/src/test/ui/macros/issue-88206.rs index 9f1306349e901..14e2f66068b01 100644 --- a/src/test/ui/macros/issue-88206.rs +++ b/src/test/ui/macros/issue-88206.rs @@ -15,7 +15,7 @@ mod hey { } use hey::{Serialize, Deserialize, X}; -//~^ NOTE `Serialize` is imported here, but it is a trait +//~^ NOTE `Serialize` is imported here, but it is only a trait, without a derive macro //~| NOTE `Deserialize` is imported here, but it is a trait //~| NOTE `X` is imported here, but it is a struct diff --git a/src/test/ui/macros/issue-88206.stderr b/src/test/ui/macros/issue-88206.stderr index 0413605fdb7f2..f7f5b56488007 100644 --- a/src/test/ui/macros/issue-88206.stderr +++ b/src/test/ui/macros/issue-88206.stderr @@ -104,7 +104,7 @@ error: cannot find derive macro `Serialize` in this scope LL | #[derive(Serialize)] | ^^^^^^^^^ | -note: `Serialize` is imported here, but it is a trait, not a derive macro +note: `Serialize` is imported here, but it is only a trait, without a derive macro --> $DIR/issue-88206.rs:17:11 | LL | use hey::{Serialize, Deserialize, X};