From 276734d6a4997088b6d2e7416f5d4c07b4c8acf5 Mon Sep 17 00:00:00 2001 From: David Ross Date: Sat, 1 Feb 2020 18:04:07 -0800 Subject: [PATCH 1/3] Fix 59191 This adds an explicit error for when macros replace the crate root with a non-module item. --- src/librustc_expand/expand.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/librustc_expand/expand.rs b/src/librustc_expand/expand.rs index ea459064b0957..1aa4f11a13026 100644 --- a/src/librustc_expand/expand.rs +++ b/src/librustc_expand/expand.rs @@ -363,7 +363,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> { krate.attrs = vec![]; krate.module = ast::Mod { inner: orig_mod_span, items: vec![], inline: true }; } - _ => unreachable!(), + Some(ast::Item { span, kind, .. }) => { + self.cx.span_fatal( + span, + &format!( + "expected crate top-level item to be a module after macro expansion, found a {}", + kind.descriptive_variant() + ), + ); + } }; self.cx.trace_macros_diag(); krate From 410114b9d243020482689a94f7b254600f4d819e Mon Sep 17 00:00:00 2001 From: David Ross Date: Sat, 1 Feb 2020 18:07:26 -0800 Subject: [PATCH 2/3] Add tests for issue 59191 --- src/test/ui/proc-macro/auxiliary/issue-59191.rs | 16 ++++++++++++++++ .../issue-59191-replace-root-with-fn.rs | 7 +++++++ .../issue-59191-replace-root-with-fn.stderr | 8 ++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/test/ui/proc-macro/auxiliary/issue-59191.rs create mode 100644 src/test/ui/proc-macro/issue-59191-replace-root-with-fn.rs create mode 100644 src/test/ui/proc-macro/issue-59191-replace-root-with-fn.stderr diff --git a/src/test/ui/proc-macro/auxiliary/issue-59191.rs b/src/test/ui/proc-macro/auxiliary/issue-59191.rs new file mode 100644 index 0000000000000..d9ee77067ecb7 --- /dev/null +++ b/src/test/ui/proc-macro/auxiliary/issue-59191.rs @@ -0,0 +1,16 @@ +// edition:2018 +// force-host +// no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; +use proc_macro::TokenStream; + +#[proc_macro_attribute] +pub fn no_main(_attrs: TokenStream, _input: TokenStream) -> TokenStream { + let new_krate = r#" + fn main() {} + "#; + new_krate.parse().unwrap() +} diff --git a/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.rs b/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.rs new file mode 100644 index 0000000000000..039878af56eb6 --- /dev/null +++ b/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.rs @@ -0,0 +1,7 @@ +// edition:2018 +// aux-crate:issue_59191=issue-59191.rs +// Test that using a macro to replace the entire crate tree with a non-'mod' item errors out nicely. +// `issue_59191::no_main` replaces whatever's passed in with `fn main() {}`. +#![feature(custom_inner_attributes)] +#![issue_59191::no_main] +//~^ ERROR expected crate top-level item to be a module after macro expansion, found a function diff --git a/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.stderr b/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.stderr new file mode 100644 index 0000000000000..1bda86551d418 --- /dev/null +++ b/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.stderr @@ -0,0 +1,8 @@ +error: expected crate top-level item to be a module after macro expansion, found a function + --> $DIR/issue-59191-replace-root-with-fn.rs:6:1 + | +LL | #![issue_59191::no_main] + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + From 152811d8bf389fce7328ba7bc50c26c34afb0d81 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 2 Feb 2020 11:28:32 -0800 Subject: [PATCH 3/3] Change expansion error to be non-fatal Changes the error handler for inner attributes that replace the root with a non-module. Previously it would emit a fatal error. It now emits an empty expasion and a non-fatal error like the existing handler for a failed expansion. --- src/librustc_expand/expand.rs | 4 +++- .../proc-macro/issue-59191-replace-root-with-fn.rs | 1 + .../issue-59191-replace-root-with-fn.stderr | 13 +++++++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/librustc_expand/expand.rs b/src/librustc_expand/expand.rs index 1aa4f11a13026..4e0fec9ce1d1c 100644 --- a/src/librustc_expand/expand.rs +++ b/src/librustc_expand/expand.rs @@ -364,7 +364,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> { krate.module = ast::Mod { inner: orig_mod_span, items: vec![], inline: true }; } Some(ast::Item { span, kind, .. }) => { - self.cx.span_fatal( + krate.attrs = vec![]; + krate.module = ast::Mod { inner: orig_mod_span, items: vec![], inline: true }; + self.cx.span_err( span, &format!( "expected crate top-level item to be a module after macro expansion, found a {}", diff --git a/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.rs b/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.rs index 039878af56eb6..a59cacb8bde1f 100644 --- a/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.rs +++ b/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.rs @@ -3,5 +3,6 @@ // Test that using a macro to replace the entire crate tree with a non-'mod' item errors out nicely. // `issue_59191::no_main` replaces whatever's passed in with `fn main() {}`. #![feature(custom_inner_attributes)] +//~^ ERROR `main` function not found in crate `issue_59191_replace_root_with_fn` [E0601] #![issue_59191::no_main] //~^ ERROR expected crate top-level item to be a module after macro expansion, found a function diff --git a/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.stderr b/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.stderr index 1bda86551d418..e0a3caef9db88 100644 --- a/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.stderr +++ b/src/test/ui/proc-macro/issue-59191-replace-root-with-fn.stderr @@ -1,8 +1,17 @@ error: expected crate top-level item to be a module after macro expansion, found a function - --> $DIR/issue-59191-replace-root-with-fn.rs:6:1 + --> $DIR/issue-59191-replace-root-with-fn.rs:7:1 | LL | #![issue_59191::no_main] | ^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to previous error +error[E0601]: `main` function not found in crate `issue_59191_replace_root_with_fn` + --> $DIR/issue-59191-replace-root-with-fn.rs:5:1 + | +LL | / #![feature(custom_inner_attributes)] +LL | | +LL | | #![issue_59191::no_main] + | |________________________^ consider adding a `main` function to `$DIR/issue-59191-replace-root-with-fn.rs` + +error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0601`.