diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 0fd21e017ff61..c6bbeda36e930 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -5,7 +5,7 @@ use std::{iter, mem, slice}; use rustc_ast::mut_visit::*; use rustc_ast::tokenstream::TokenStream; -use rustc_ast::visit::{self, AssocCtxt, Visitor, VisitorResult, try_visit, walk_list}; +use rustc_ast::visit::{AssocCtxt, Visitor, VisitorResult, try_visit, walk_list}; use rustc_ast::{ self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrItemKind, AttrStyle, AttrVec, DUMMY_NODE_ID, DelegationSource, DelegationSuffixes, EarlyParsedAttribute, ExprKind, @@ -20,7 +20,7 @@ use rustc_attr_parsing::{ }; use rustc_data_structures::flat_map_in_place::FlatMapInPlace; use rustc_data_structures::stack::ensure_sufficient_stack; -use rustc_errors::{PResult, msg}; +use rustc_errors::PResult; use rustc_feature::Features; use rustc_hir::Target; use rustc_hir::def::MacroKinds; @@ -29,7 +29,6 @@ use rustc_parse::parser::{ AllowConstBlockItems, AttemptLocalParseRecovery, CommaRecoveryMode, ForceCollect, Parser, RecoverColon, RecoverComma, Recovery, token_descr, }; -use rustc_session::Session; use rustc_session::errors::feature_err; use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS}; use rustc_span::hygiene::SyntaxContext; @@ -770,7 +769,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } InvocationKind::Attr { attr, pos, mut item, derives } => { if let Some(expander) = ext.as_attr() { - self.gate_proc_macro_input(&item); self.gate_proc_macro_attr_item(span, &item); let tokens = match &item { // FIXME: Collect tokens and use them instead of generating @@ -904,9 +902,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> { InvocationKind::Derive { path, item, is_const } => match ext { SyntaxExtensionKind::Derive(expander) | SyntaxExtensionKind::LegacyDerive(expander) => { - if let SyntaxExtensionKind::Derive(..) = ext { - self.gate_proc_macro_input(&item); - } // The `MetaItem` representing the trait to derive can't // have an unsafe around it (as of now). let meta = ast::MetaItem { @@ -1043,37 +1038,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> { .emit(); } - fn gate_proc_macro_input(&self, annotatable: &Annotatable) { - struct GateProcMacroInput<'a> { - sess: &'a Session, - } - - impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> { - fn visit_item(&mut self, item: &'ast ast::Item) { - match &item.kind { - ItemKind::Mod(_, _, mod_kind) - if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) => - { - feature_err( - self.sess, - sym::proc_macro_hygiene, - item.span, - msg!("file modules in proc macro input are unstable"), - ) - .emit(); - } - _ => {} - } - - visit::walk_item(self, item); - } - } - - if !self.cx.ecfg.features.proc_macro_hygiene() { - annotatable.visit_with(&mut GateProcMacroInput { sess: self.cx.sess }); - } - } - fn parse_ast_fragment( &mut self, toks: TokenStream, diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index e1f63b7c3dce8..f1df5afd75daa 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -688,7 +688,7 @@ declare_features! ( (unstable, powerpc_target_feature, "1.27.0", Some(150255)), /// The prfchw target feature on x86. (unstable, prfchw_target_feature, "1.78.0", Some(150256)), - /// Allows macro attributes on expressions, statements and non-inline modules. + /// Allows macro attributes on expressions and statements. (unstable, proc_macro_hygiene, "1.30.0", Some(54727)), /// Allows the use of raw-dylibs on ELF platforms (incomplete, raw_dylib_elf, "1.87.0", Some(135694)), diff --git a/tests/ui/proc-macro/attributes-on-modules-fail.rs b/tests/ui/proc-macro/attributes-on-modules-fail.rs index 80701523d49e6..6ad935d494458 100644 --- a/tests/ui/proc-macro/attributes-on-modules-fail.rs +++ b/tests/ui/proc-macro/attributes-on-modules-fail.rs @@ -17,31 +17,4 @@ type A = X; //~ ERROR cannot find type `X` in this scope #[derive(Copy)] //~ ERROR `derive` may only be applied to `struct`s, `enum`s and `union`s mod n {} -#[empty_attr] -mod module; //~ ERROR file modules in proc macro input are unstable - -#[empty_attr] -mod outer { - mod inner; //~ ERROR file modules in proc macro input are unstable - - mod inner_inline {} // OK -} - -#[derive(Empty)] -struct S { - field: [u8; { - #[path = "outer/inner.rs"] - mod inner; //~ ERROR file modules in proc macro input are unstable - mod inner_inline {} // OK - 0 - }], -} - -#[identity_attr] -fn f() { - #[path = "outer/inner.rs"] - mod inner; //~ ERROR file modules in proc macro input are unstable - mod inner_inline {} // OK -} - fn main() {} diff --git a/tests/ui/proc-macro/attributes-on-modules-fail.stderr b/tests/ui/proc-macro/attributes-on-modules-fail.stderr index 1dc87fd0d5513..78593bf8de897 100644 --- a/tests/ui/proc-macro/attributes-on-modules-fail.stderr +++ b/tests/ui/proc-macro/attributes-on-modules-fail.stderr @@ -6,46 +6,6 @@ LL | #[derive(Copy)] LL | mod n {} | -------- not a `struct`, `enum` or `union` -error[E0658]: file modules in proc macro input are unstable - --> $DIR/attributes-on-modules-fail.rs:21:1 - | -LL | mod module; - | ^^^^^^^^^^^ - | - = note: see issue #54727 for more information - = help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: file modules in proc macro input are unstable - --> $DIR/attributes-on-modules-fail.rs:25:5 - | -LL | mod inner; - | ^^^^^^^^^^ - | - = note: see issue #54727 for more information - = help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: file modules in proc macro input are unstable - --> $DIR/attributes-on-modules-fail.rs:34:9 - | -LL | mod inner; - | ^^^^^^^^^^ - | - = note: see issue #54727 for more information - = help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: file modules in proc macro input are unstable - --> $DIR/attributes-on-modules-fail.rs:43:5 - | -LL | mod inner; - | ^^^^^^^^^^ - | - = note: see issue #54727 for more information - = help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - error[E0425]: cannot find type `Y` in this scope --> $DIR/attributes-on-modules-fail.rs:11:14 | @@ -68,7 +28,7 @@ help: consider importing this struct LL + use m::X; | -error: aborting due to 7 previous errors +error: aborting due to 3 previous errors -Some errors have detailed explanations: E0425, E0658, E0774. +Some errors have detailed explanations: E0425, E0774. For more information about an error, try `rustc --explain E0425`. diff --git a/tests/ui/proc-macro/auxiliary/expect-modules.rs b/tests/ui/proc-macro/auxiliary/expect-modules.rs new file mode 100644 index 0000000000000..57bd5f39f71a3 --- /dev/null +++ b/tests/ui/proc-macro/auxiliary/expect-modules.rs @@ -0,0 +1,41 @@ +/// Macros that assert that certain `mod` items are present in their input. + +extern crate proc_macro; + +use proc_macro::TokenStream; + +#[proc_macro_attribute] +pub fn expect_mod_item(_attrs: TokenStream, item: TokenStream) -> TokenStream { + let s = item.to_string(); + + assert_eq!(s, "mod module;"); + + item +} + +#[proc_macro_attribute] +pub fn expect_mods_attr(_attrs: TokenStream, item: TokenStream) -> TokenStream { + let s = item.to_string(); + + assert_contains(&s, "mod attr_outlined;"); + assert_contains(&s, "mod attr_inline {}"); + + item +} + +#[proc_macro_derive(ExpectModsDerive)] +pub fn expect_mods_derive(item: TokenStream) -> TokenStream { + let s = item.to_string(); + + assert_contains(&s, "mod derive_outlined;"); + assert_contains(&s, "mod derive_inline {}"); + + TokenStream::new() +} + +#[track_caller] +fn assert_contains(s: &str, needle: &str) { + if !s.contains(needle) { + panic!("{needle:?} not found in:\n---\n{s}\n---\n"); + } +} diff --git a/tests/ui/proc-macro/inner-attr-file-mod.rs b/tests/ui/proc-macro/inner-attr-file-mod.rs index e592331c2c6db..535341957ade5 100644 --- a/tests/ui/proc-macro/inner-attr-file-mod.rs +++ b/tests/ui/proc-macro/inner-attr-file-mod.rs @@ -9,8 +9,7 @@ extern crate test_macros; #[deny(unused_attributes)] mod module_with_attrs; -//~^ ERROR file modules in proc macro input are unstable -//~| ERROR custom inner attributes are unstable +//~^ ERROR custom inner attributes are unstable fn main() {} diff --git a/tests/ui/proc-macro/inner-attr-file-mod.stderr b/tests/ui/proc-macro/inner-attr-file-mod.stderr index 92262284a00c4..a377313fc519e 100644 --- a/tests/ui/proc-macro/inner-attr-file-mod.stderr +++ b/tests/ui/proc-macro/inner-attr-file-mod.stderr @@ -18,16 +18,6 @@ LL | #![print_attr] = help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error[E0658]: file modules in proc macro input are unstable - --> $DIR/inner-attr-file-mod.rs:11:1 - | -LL | mod module_with_attrs; - | ^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #54727 for more information - = help: add `#![feature(proc_macro_hygiene)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - error[E0658]: custom inner attributes are unstable --> $DIR/inner-attr-file-mod.rs:11:1 | @@ -38,6 +28,6 @@ LL | mod module_with_attrs; = help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/proc-macro/module.rs b/tests/ui/proc-macro/module.rs index 5878f1b7ddd8a..f01bd5e50f0ab 100644 --- a/tests/ui/proc-macro/module.rs +++ b/tests/ui/proc-macro/module.rs @@ -1 +1,4 @@ -//@ ignore-auxiliary (used by `./attributes-on-modules-fail.rs`) +//@ ignore-auxiliary (used by `./attributes-on-modules-fail.rs` and `modules-in-input.rs`) + +// An item that can be referenced to verify the module has been properly loaded. +pub const fn hello() {} diff --git a/tests/ui/proc-macro/modules-in-input.rs b/tests/ui/proc-macro/modules-in-input.rs new file mode 100644 index 0000000000000..50b4dc9e40f44 --- /dev/null +++ b/tests/ui/proc-macro/modules-in-input.rs @@ -0,0 +1,47 @@ +//@ check-pass +//@ proc-macro: expect-modules.rs +//@ reference: macro.invocation.attr.mod + +// Verifies how module items are represented in proc macro input. + +#[macro_use] +extern crate expect_modules; + +#[expect_modules::expect_mod_item] +mod module; + +fn check() { + module::hello(); +} + +#[expect_modules::expect_mods_attr] +mod outer { + #[path = "../module.rs"] + mod attr_outlined; + mod attr_inline {} + + fn check() { + attr_outlined::hello(); + } +} + +#[expect_modules::expect_mods_attr] +fn foo() { + #[path = "module.rs"] + mod attr_outlined; + mod attr_inline {} + attr_outlined::hello(); +} + +#[derive(expect_modules::ExpectModsDerive)] +struct Foo( + [u8; { + #[path = "module.rs"] + mod derive_outlined; + mod derive_inline {} + derive_outlined::hello(); + 0 + }], +); + +fn main() {} diff --git a/tests/ui/proc-macro/unsafe-mod.rs b/tests/ui/proc-macro/unsafe-mod.rs index f8453c2f62cd4..d9fbe7a8c5e1d 100644 --- a/tests/ui/proc-macro/unsafe-mod.rs +++ b/tests/ui/proc-macro/unsafe-mod.rs @@ -1,8 +1,6 @@ //@ run-pass //@ proc-macro: macro-only-syntax.rs -#![feature(proc_macro_hygiene)] - extern crate macro_only_syntax; #[macro_only_syntax::expect_unsafe_mod]