Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make #[derive(A, B, ...)] cfg-eval its input only for A, B, ... and stabilize feature(macro_attributes_in_derive_output) #87220

Merged
merged 3 commits into from
Sep 25, 2021
Merged
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
29 changes: 14 additions & 15 deletions compiler/rustc_builtin_macros/src/cfg_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ use crate::util::check_builtin_macro_attribute;

use rustc_ast as ast;
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast::ptr::P;
use rustc_ast::tokenstream::CanSynthesizeMissingTokens;
use rustc_ast::visit::Visitor;
use rustc_ast::{mut_visit, visit};
use rustc_ast::{AstLike, Attribute};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_expand::config::StripUnconfigured;
use rustc_expand::configure;
use rustc_feature::Features;
use rustc_parse::parser::ForceCollect;
use rustc_session::utils::FlattenNonterminals;

use rustc_ast::ptr::P;
use rustc_session::Session;
use rustc_span::symbol::sym;
use rustc_span::Span;
use smallvec::SmallVec;
Expand All @@ -24,21 +25,19 @@ crate fn expand(
annotatable: Annotatable,
) -> Vec<Annotatable> {
check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
vec![cfg_eval(ecx, annotatable)]
vec![cfg_eval(ecx.sess, ecx.ecfg.features, annotatable)]
}

crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Annotatable {
CfgEval {
cfg: &mut StripUnconfigured {
sess: ecx.sess,
features: ecx.ecfg.features,
config_tokens: true,
},
}
.configure_annotatable(annotatable)
// Since the item itself has already been configured by the `InvocationCollector`,
// we know that fold result vector will contain exactly one element.
.unwrap()
crate fn cfg_eval(
sess: &Session,
features: Option<&Features>,
annotatable: Annotatable,
) -> Annotatable {
CfgEval { cfg: &mut StripUnconfigured { sess, features, config_tokens: true } }
.configure_annotatable(annotatable)
// Since the item itself has already been configured by the `InvocationCollector`,
// we know that fold result vector will contain exactly one element.
.unwrap()
}

struct CfgEval<'a, 'b> {
Expand Down
40 changes: 33 additions & 7 deletions compiler/rustc_builtin_macros/src/derive.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::cfg_eval::cfg_eval;

use rustc_ast::{self as ast, attr, token, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
use rustc_ast as ast;
use rustc_ast::{attr, token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
use rustc_errors::{struct_span_err, Applicability};
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
use rustc_feature::AttributeTemplate;
use rustc_parse::validate_attr;
use rustc_session::Session;
use rustc_span::symbol::sym;
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;

crate struct Expander;
Expand All @@ -26,8 +27,7 @@ impl MultiItemModifier for Expander {
return ExpandResult::Ready(vec![item]);
}

let item = cfg_eval(ecx, item);

let (sess, features) = (ecx.sess, ecx.ecfg.features);
let result =
ecx.resolver.resolve_derives(ecx.current_expansion.id, ecx.force_mode, &|| {
let template =
Expand All @@ -40,7 +40,8 @@ impl MultiItemModifier for Expander {
template,
);

attr.meta_item_list()
let mut resolutions: Vec<_> = attr
.meta_item_list()
.unwrap_or_default()
.into_iter()
.filter_map(|nested_meta| match nested_meta {
Expand All @@ -56,8 +57,21 @@ impl MultiItemModifier for Expander {
report_path_args(sess, &meta);
meta.path
})
.map(|path| (path, item.clone(), None))
.collect()
.map(|path| (path, dummy_annotatable(), None))
.collect();

// Do not configure or clone items unless necessary.
match &mut resolutions[..] {
[] => {}
[(_, first_item, _), others @ ..] => {
*first_item = cfg_eval(sess, features, item.clone());
for (_, item, _) in others {
*item = first_item.clone();
}
}
}

resolutions
});

match result {
Expand All @@ -67,6 +81,18 @@ impl MultiItemModifier for Expander {
}
}

// The cheapest `Annotatable` to construct.
fn dummy_annotatable() -> Annotatable {
Annotatable::GenericParam(ast::GenericParam {
id: ast::DUMMY_NODE_ID,
ident: Ident::invalid(),
attrs: Default::default(),
bounds: Default::default(),
is_placeholder: false,
kind: GenericParamKind::Lifetime,
})
}

fn report_bad_target(sess: &Session, item: &Annotatable, span: Span) -> bool {
let item_kind = match item {
Annotatable::Item(item) => Some(&item.kind),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ declare_features! (
(accepted, const_fn_union, "1.56.0", Some(51909), None),
/// Allows explicit discriminants on non-unit enum variants.
(accepted, arbitrary_enum_discriminant, "1.56.0", Some(60553), None),
/// Allows macro attributes to observe output of `#[derive]`.
(accepted, macro_attributes_in_derive_output, "1.57.0", Some(81119), None),

// -------------------------------------------------------------------------
// feature-group-end: accepted features
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,6 @@ declare_features! (
/// Lessens the requirements for structs to implement `Unsize`.
(active, relaxed_struct_unsize, "1.51.0", Some(81793), None),

/// Allows macro attributes to observe output of `#[derive]`.
(active, macro_attributes_in_derive_output, "1.51.0", Some(81119), None),

/// Allows associated types in inherent impls.
(incomplete, inherent_associated_types, "1.52.0", Some(8995), None),

Expand Down
32 changes: 0 additions & 32 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,38 +311,6 @@ impl<'a> ResolverExpand for Resolver<'a> {
self.create_stable_hashing_context(),
);

if let Res::Def(_, _) = res {
// Gate macro attributes in `#[derive]` output.
if !self.session.features_untracked().macro_attributes_in_derive_output
&& kind == MacroKind::Attr
&& ext.builtin_name != Some(sym::derive)
{
let mut expn_id = parent_scope.expansion;
loop {
// Helper attr table is a quick way to determine whether the attr is `derive`.
if self.helper_attrs.contains_key(&expn_id) {
feature_err(
&self.session.parse_sess,
sym::macro_attributes_in_derive_output,
path.span,
"macro attributes in `#[derive]` output are unstable",
)
.emit();
break;
} else {
let expn_data = expn_id.expn_data();
match expn_data.kind {
ExpnKind::Root
| ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
break;
}
_ => expn_id = expn_data.parent.expect_local(),
}
}
}
}
}

Ok(ext)
}

Expand Down
37 changes: 0 additions & 37 deletions src/test/ui/proc-macro/attribute-after-derive-feature-gate.rs

This file was deleted.

30 changes: 0 additions & 30 deletions src/test/ui/proc-macro/attribute-after-derive-feature-gate.stderr

This file was deleted.

2 changes: 0 additions & 2 deletions src/test/ui/proc-macro/attribute-after-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// compile-flags: -Z span-debug
// aux-build: test-macros.rs

#![feature(macro_attributes_in_derive_output)]

#![no_std] // Don't load unnecessary hygiene information from std
extern crate std;

Expand Down
Loading