From 2c960870234a3ebf66569ff4b2ba2802d85be6f2 Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 2 Dec 2023 23:15:36 +0800 Subject: [PATCH] add test case for #89566 --- compiler/rustc_resolve/src/macros.rs | 4 ++-- ...-suggest-fix-invalid-top-level-macro-attr.fixed | 6 ++++++ ...566-suggest-fix-invalid-top-level-macro-attr.rs | 6 ++++++ ...suggest-fix-invalid-top-level-macro-attr.stderr | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.fixed create mode 100644 tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.rs create mode 100644 tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.stderr diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index ada0a082517ec..f65f55e185c63 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -702,7 +702,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { for attr in attrs.iter().filter(|attr| attr.style == AttrStyle::Inner) { if attr.has_name(name) { let tcx = self.tcx; - tcx.sess.emit_err(errors::InvalidAttrAtCrateLevel { + tcx.dcx().emit_err(errors::InvalidAttrAtCrateLevel { name, span: attr.span, sugg: errors::InvalidAttrSugg { @@ -761,7 +761,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { fallback = !this.report_invalid_crate_level_attr(&krate.attrs, path[0].ident.name); } - if fallback && this.tcx.sess.has_errors().is_none() { + if fallback && this.tcx.dcx().has_errors().is_none() { this.dcx().emit_err(CannotDetermineMacroResolution { span, kind: kind.descr(), diff --git a/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.fixed b/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.fixed new file mode 100644 index 0000000000000..ae5f9f86726f3 --- /dev/null +++ b/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.fixed @@ -0,0 +1,6 @@ +// run-rustfix + +#[derive(Debug)] //~ ERROR `derive` attribute cannot be used at crate level +struct Test {} + +fn main() {} diff --git a/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.rs b/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.rs new file mode 100644 index 0000000000000..639c64f8827c5 --- /dev/null +++ b/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.rs @@ -0,0 +1,6 @@ +// run-rustfix + +#![derive(Debug)] //~ ERROR `derive` attribute cannot be used at crate level +struct Test {} + +fn main() {} diff --git a/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.stderr b/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.stderr new file mode 100644 index 0000000000000..6198b78523abe --- /dev/null +++ b/tests/ui/proc-macro/issue-89566-suggest-fix-invalid-top-level-macro-attr.stderr @@ -0,0 +1,14 @@ +error: `derive` attribute cannot be used at crate level + --> $DIR/issue-89566-suggest-fix-invalid-top-level-macro-attr.rs:3:1 + | +LL | #![derive(Debug)] + | ^^^^^^^^^^^^^^^^^ + | +help: perhaps you meant to use an outer attribute + | +LL - #![derive(Debug)] +LL + #[derive(Debug)] + | + +error: aborting due to 1 previous error +