Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 2 additions & 38 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
27 changes: 0 additions & 27 deletions tests/ui/proc-macro/attributes-on-modules-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
44 changes: 2 additions & 42 deletions tests/ui/proc-macro/attributes-on-modules-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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
|
Expand All @@ -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`.
41 changes: 41 additions & 0 deletions tests/ui/proc-macro/auxiliary/expect-modules.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
3 changes: 1 addition & 2 deletions tests/ui/proc-macro/inner-attr-file-mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down
12 changes: 1 addition & 11 deletions tests/ui/proc-macro/inner-attr-file-mod.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/rust-lang/rust/issues/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
|
Expand All @@ -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`.
5 changes: 4 additions & 1 deletion tests/ui/proc-macro/module.rs
Original file line number Diff line number Diff line change
@@ -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() {}
47 changes: 47 additions & 0 deletions tests/ui/proc-macro/modules-in-input.rs
Original file line number Diff line number Diff line change
@@ -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() {}
2 changes: 0 additions & 2 deletions tests/ui/proc-macro/unsafe-mod.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Loading