Skip to content

Commit

Permalink
remove attr filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Dec 5, 2024
1 parent 322b8d2 commit 87952f0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 109 deletions.
17 changes: 7 additions & 10 deletions compiler/rustc_attr/src/attributes/confusables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use rustc_hir::AttributeKind;
use rustc_span::{Span, Symbol, sym};
use thin_vec::ThinVec;

use super::{AttributeFilter, AttributeGroup, AttributeMapping};
use super::{AttributeGroup, AttributeMapping};
use crate::context::AttributeGroupContext;
use crate::parser::ArgParser;
use crate::{attribute_filter, session_diagnostics};
use crate::session_diagnostics;

// TODO: turn into CombineGroup?
#[derive(Default)]
Expand Down Expand Up @@ -47,17 +47,14 @@ impl AttributeGroup for ConfusablesGroup {
this.first_span.get_or_insert(cx.attr_span);
})];

fn finalize(self, _cx: &AttributeGroupContext<'_>) -> Option<(AttributeKind, AttributeFilter)> {
fn finalize(self, _cx: &AttributeGroupContext<'_>) -> Option<AttributeKind> {
if self.confusables.is_empty() {
return None;
}

Some((
AttributeKind::Confusables {
symbols: self.confusables,
first_span: self.first_span.unwrap(),
},
attribute_filter!(allow all),
))
Some(AttributeKind::Confusables {
symbols: self.confusables,
first_span: self.first_span.unwrap(),
})
}
}
17 changes: 7 additions & 10 deletions compiler/rustc_attr/src/attributes/deprecation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use rustc_hir::{AttributeKind, DeprecatedSince, Deprecation};
use rustc_span::symbol::Ident;
use rustc_span::{Span, Symbol, sym};

use super::SingleAttributeGroup;
use super::util::parse_version;
use super::{AttributeFilter, SingleAttributeGroup};
use crate::context::AttributeAcceptContext;
use crate::parser::{ArgParser, GenericArgParser, MetaItemParser, NameValueParser};
use crate::session_diagnostics;
use crate::session_diagnostics::UnsupportedLiteralReason;
use crate::{attribute_filter, session_diagnostics};

pub(crate) struct DeprecationGroup;

Expand Down Expand Up @@ -64,7 +64,7 @@ impl SingleAttributeGroup for DeprecationGroup {
fn convert(
cx: &AttributeAcceptContext<'_>,
args: &GenericArgParser<'_, rustc_ast::Expr>,
) -> Option<(AttributeKind, AttributeFilter)> {
) -> Option<AttributeKind> {
let features = cx.features();

let mut since = None;
Expand Down Expand Up @@ -155,12 +155,9 @@ impl SingleAttributeGroup for DeprecationGroup {
return None;
}

Some((
AttributeKind::Deprecation {
deprecation: Deprecation { since, note, suggestion },
span: cx.attr_span,
},
attribute_filter!(allow all),
))
Some(AttributeKind::Deprecation {
deprecation: Deprecation { since, note, suggestion },
span: cx.attr_span,
})
}
}
72 changes: 11 additions & 61 deletions compiler/rustc_attr/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::marker::PhantomData;

use rustc_ast::Expr;
use rustc_hir::AttributeKind;
use rustc_span::{ErrorGuaranteed, Span};
use rustc_span::Span;
use thin_vec::ThinVec;

use crate::context::{AttributeAcceptContext, AttributeGroupContext};
Expand Down Expand Up @@ -56,14 +56,9 @@ pub(crate) trait AttributeGroup: Default + 'static {

/// The extractor has gotten a chance to accept the attributes on an item,
/// now produce an attribute.
fn finalize(self, cx: &AttributeGroupContext<'_>) -> Option<(AttributeKind, AttributeFilter)>;
fn finalize(self, cx: &AttributeGroupContext<'_>) -> Option<AttributeKind>;
}

/// Create an AttributeFilter using [`attribute_filter`].
///
/// Tells the parsing system what other attributes this attribute can be used with together.
pub(crate) struct AttributeFilter(Box<dyn Fn(&AttributeKind) -> Result<(), ErrorGuaranteed>>);

/// A slightly simpler and more restricted way to convert attributes which you can implement for
/// unit types. Assumes that a single attribute can only appear a single time on an item
/// [`SingleGroup<T> where T: SingleAttributeGroup`](Single) creates an [`AttributeGroup`] from any [`SingleAttributeGroup`].
Expand All @@ -83,13 +78,10 @@ pub(crate) trait SingleAttributeGroup: 'static {
fn convert(
cx: &AttributeAcceptContext<'_>,
args: &GenericArgParser<'_, Expr>,
) -> Option<(AttributeKind, AttributeFilter)>;
) -> Option<AttributeKind>;
}

pub(crate) struct Single<T: SingleAttributeGroup>(
PhantomData<T>,
Option<(AttributeKind, AttributeFilter, Span)>,
);
pub(crate) struct Single<T: SingleAttributeGroup>(PhantomData<T>, Option<(AttributeKind, Span)>);

impl<T: SingleAttributeGroup> Default for Single<T> {
fn default() -> Self {
Expand All @@ -99,19 +91,18 @@ impl<T: SingleAttributeGroup> Default for Single<T> {

impl<T: SingleAttributeGroup> AttributeGroup for Single<T> {
const ATTRIBUTES: AttributeMapping<Self> = &[(T::PATH, |group: &mut Single<T>, cx, args| {
if let Some((_, _, s)) = group.1 {
if let Some((_, s)) = group.1 {
T::on_duplicate(cx, s);
return;
}

if let Some((pa, f)) = T::convert(cx, args) {
group.1 = Some((pa, f, cx.attr_span));
if let Some(pa) = T::convert(cx, args) {
group.1 = Some((pa, cx.attr_span));
}
})];

fn finalize(self, _cx: &AttributeGroupContext<'_>) -> Option<(AttributeKind, AttributeFilter)> {
let (pa, f, _) = self.1?;
Some((pa, f))
fn finalize(self, _cx: &AttributeGroupContext<'_>) -> Option<AttributeKind> {
Some(self.1?.0)
}
}

Expand Down Expand Up @@ -153,53 +144,12 @@ impl<T: CombineAttributeGroup> AttributeGroup for Combine<T> {
const ATTRIBUTES: AttributeMapping<Self> =
&[(T::PATH, |group: &mut Combine<T>, cx, args| group.1.extend(T::extend(cx, args)))];

fn finalize(self, _cx: &AttributeGroupContext<'_>) -> Option<(AttributeKind, AttributeFilter)> {
fn finalize(self, _cx: &AttributeGroupContext<'_>) -> Option<AttributeKind> {
if self.1.is_empty() {
None
} else {
// TODO: what filter here?
Some((T::CONVERT(self.1), crate::attribute_filter!(allow all)))
Some(T::CONVERT(self.1))
}
}
}

#[macro_export]
macro_rules! attribute_filter {
(deny all: $b: block) => {
$crate::attribute_filter!(allow: else $b)
};

(allow all) => {
$crate::attribute_filter!(deny: )
};

(
allow:
$(
[$pat: pat $(if $expr: expr)?]
),*
else $b: block
) => {
$crate::attributes::AttributeFilter(Box::new(|attr: &rustc_hir::AttributeKind| -> Result<(), rustc_span::ErrorGuaranteed> {
match attr {
$(
$pat $(if $expr)? => Ok(()),
)*
_ => Err($b: block),
}
}))
};

(deny: $(
[$pat: pat $(if $expr: expr)? $(=> $b: block)?]
),*) => {
$crate::attributes::AttributeFilter(Box::new(|attr: &rustc_hir::AttributeKind| -> Result<(), rustc_span::ErrorGuaranteed> {
match attr {
$(
$pat $(if $expr)? => Err($b),
)*
_ => Ok(()),
}
}))
};
}
22 changes: 9 additions & 13 deletions compiler/rustc_attr/src/attributes/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use rustc_hir::{
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};

use super::util::parse_version;
use super::{AttributeFilter, AttributeGroup, AttributeMapping, SingleAttributeGroup};
use crate::attribute_filter;
use super::{AttributeGroup, AttributeMapping, SingleAttributeGroup};
use crate::context::{AttributeAcceptContext, AttributeGroupContext};
use crate::parser::{ArgParser, MetaItemParser, NameValueParser};
use crate::session_diagnostics::{self, UnsupportedLiteralReason};
Expand Down Expand Up @@ -52,7 +51,7 @@ impl AttributeGroup for StabilityGroup {
}),
];

fn finalize(self, cx: &AttributeGroupContext<'_>) -> Option<(AttributeKind, AttributeFilter)> {
fn finalize(self, cx: &AttributeGroupContext<'_>) -> Option<AttributeKind> {
let (mut stability, span) = self.stability?;

if self.allowed_through_unstable_modules {
Expand All @@ -72,7 +71,7 @@ impl AttributeGroup for StabilityGroup {
cx.dcx().emit_err(session_diagnostics::StabilityOutsideStd { span });
}

Some((AttributeKind::Stability { stability, span }, attribute_filter!(allow all)))
Some(AttributeKind::Stability { stability, span })
}
}

Expand All @@ -93,15 +92,15 @@ impl AttributeGroup for BodyStabilityGroup {
}
})];

fn finalize(self, cx: &AttributeGroupContext<'_>) -> Option<(AttributeKind, AttributeFilter)> {
fn finalize(self, cx: &AttributeGroupContext<'_>) -> Option<AttributeKind> {
let (stability, span) = self.stability?;

// Emit errors for non-staged-api crates.
if !cx.features().staged_api() {
cx.dcx().emit_err(session_diagnostics::StabilityOutsideStd { span });
}

Some((AttributeKind::BodyStability { stability, span }, attribute_filter!(allow all)))
Some(AttributeKind::BodyStability { stability, span })
}
}

Expand All @@ -116,8 +115,8 @@ impl SingleAttributeGroup for ConstStabilityIndirectGroup {
fn convert(
_cx: &AttributeAcceptContext<'_>,
_args: &super::GenericArgParser<'_, rustc_ast::Expr>,
) -> Option<(AttributeKind, AttributeFilter)> {
Some((AttributeKind::ConstStabilityIndirect, attribute_filter!(allow all)))
) -> Option<AttributeKind> {
Some(AttributeKind::ConstStabilityIndirect)
}
}

Expand Down Expand Up @@ -166,10 +165,7 @@ impl AttributeGroup for ConstStabilityGroup {
}),
];

fn finalize(
mut self,
cx: &AttributeGroupContext<'_>,
) -> Option<(AttributeKind, AttributeFilter)> {
fn finalize(mut self, cx: &AttributeGroupContext<'_>) -> Option<AttributeKind> {
if self.promotable {
if let Some((ref mut stab, _)) = self.stability {
stab.promotable = true;
Expand All @@ -186,7 +182,7 @@ impl AttributeGroup for ConstStabilityGroup {
cx.dcx().emit_err(session_diagnostics::StabilityOutsideStd { span });
}

Some((AttributeKind::ConstStability { stability, span }, attribute_filter!(allow all)))
Some(AttributeKind::ConstStability { stability, span })
}
}

Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_attr/src/attributes/transparency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use rustc_span::hygiene::Transparency;
use rustc_span::sym;

use super::SingleAttributeGroup;
use crate::attribute_filter;
use crate::parser::{ArgParser, NameValueParser};

pub(crate) struct TransparencyGroup;
Expand All @@ -21,7 +20,7 @@ impl SingleAttributeGroup for TransparencyGroup {
fn convert(
cx: &crate::context::AttributeAcceptContext<'_>,
args: &crate::parser::GenericArgParser<'_, rustc_ast::Expr>,
) -> Option<(AttributeKind, super::AttributeFilter)> {
) -> Option<AttributeKind> {
match args.name_value().and_then(|nv| nv.value_as_str()) {
Some(sym::transparent) => Some(Transparency::Transparent),
Some(sym::semitransparent) => Some(Transparency::SemiTransparent),
Expand All @@ -32,6 +31,6 @@ impl SingleAttributeGroup for TransparencyGroup {
}
None => None,
}
.map(|t| (AttributeKind::MacroTransparency(t), attribute_filter!(allow all)))
.map(AttributeKind::MacroTransparency)
}
}
17 changes: 6 additions & 11 deletions compiler/rustc_attr/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::attributes::stability::{
BodyStabilityGroup, ConstStabilityGroup, ConstStabilityIndirectGroup, StabilityGroup,
};
use crate::attributes::transparency::TransparencyGroup;
use crate::attributes::{AttributeFilter, AttributeGroup, Combine, Single};
use crate::attributes::{AttributeGroup, Combine, Single};
use crate::parser::{GenericArgParser, GenericMetaItemParser, MetaItemParser};

macro_rules! attribute_groups {
Expand All @@ -28,10 +28,10 @@ macro_rules! attribute_groups {
) => {
pub(crate) static $name: LazyLock<(
BTreeMap<&'static [Symbol], Vec<Box<dyn Fn(&AttributeAcceptContext<'_>, &GenericArgParser<'_, ast::Expr>) + Send + Sync>>>,
Vec<Box<dyn Send + Sync + Fn(&AttributeGroupContext<'_>) -> Option<(AttributeKind, AttributeFilter)>>>
Vec<Box<dyn Send + Sync + Fn(&AttributeGroupContext<'_>) -> Option<AttributeKind>>>
)> = LazyLock::new(|| {
let mut accepts = BTreeMap::<_, Vec<Box<dyn Fn(&AttributeAcceptContext<'_>, &GenericArgParser<'_, ast::Expr>) + Send + Sync>>>::new();
let mut finalizes = Vec::<Box<dyn Send + Sync + Fn(&AttributeGroupContext<'_>) -> Option<(AttributeKind, AttributeFilter)>>>::new();
let mut finalizes = Vec::<Box<dyn Send + Sync + Fn(&AttributeGroupContext<'_>) -> Option<AttributeKind>>>::new();

$(
{
Expand Down Expand Up @@ -278,14 +278,9 @@ impl<'sess> AttributeParseContext<'sess> {

let mut parsed_attributes = Vec::new();
for f in &ATTRIBUTE_GROUP_MAPPING.1 {
let Some((attr, _filter)) = f(&group_cx) else {
continue;
};

// TODO: ignore filters?
// todo!("evaluate filters");

parsed_attributes.push(Attribute::Parsed(attr));
if let Some(attr) = f(&group_cx) {
parsed_attributes.push(Attribute::Parsed(attr));
}
}

attributes.extend(parsed_attributes);
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir/src/stability.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::num::NonZero;
use std::ops::Deref;

use rustc_macros::{Decodable, Encodable, HashStable_Generic};
use rustc_span::{Symbol, sym};
Expand Down

0 comments on commit 87952f0

Please sign in to comment.