1
Fork 0

Feature gate macro attributes in #[derive] output

This commit is contained in:
Vadim Petrochenkov 2021-01-17 16:05:02 +03:00
parent dbdbd30bf2
commit f6caae52c1
7 changed files with 124 additions and 29 deletions

View file

@ -634,6 +634,10 @@ declare_features! (
/// Lessens the requirements for structs to implement `Unsize`.
(active, relaxed_struct_unsize, "1.51.0", Some(1), None),
/// Allows macro attributes to observe output of `#[derive]`.
(active, macro_attributes_in_derive_output, "1.51.0", Some(81119), None),
// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------

View file

@ -280,6 +280,36 @@ impl<'a> ResolverExpand for Resolver<'a> {
if let Res::Def(_, _) = res {
let normal_module_def_id = self.macro_def_scope(invoc_id).nearest_parent_mod;
self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
// 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,
}
}
}
}
}
Ok(ext)

View file

@ -679,6 +679,7 @@ symbols! {
loop_break_value,
lt,
macro_at_most_once_rep,
macro_attributes_in_derive_output,
macro_escape,
macro_export,
macro_lifetime_matcher,