port #[pattern_complexity_limit] to the new attribute parsing infrastructure

This commit is contained in:
Jana Dönszelmann 2025-08-24 13:19:31 +02:00
parent 5dbe099dd3
commit b82171de5f
No known key found for this signature in database
5 changed files with 35 additions and 1 deletions

View file

@ -147,3 +147,30 @@ impl<S: Stage> SingleAttributeParser<S> for TypeLengthLimitParser {
})
}
}
pub(crate) struct PatternComplexityLimitParser;
impl<S: Stage> SingleAttributeParser<S> for PatternComplexityLimitParser {
const PATH: &[Symbol] = &[sym::pattern_complexity_limit];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
const TYPE: AttributeType = AttributeType::CrateLevel;
// FIXME: recursion limit is allowed on all targets and ignored,
// even though it should only be valid on crates of course
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
let ArgParser::NameValue(nv) = args else {
cx.expected_name_value(cx.attr_span, None);
return None;
};
Some(AttributeKind::PatternComplexityLimit {
limit: cx.parse_limit_int(nv)?,
attr_span: cx.attr_span,
limit_span: nv.value_span,
})
}
}

View file

@ -25,7 +25,8 @@ use crate::attributes::codegen_attrs::{
};
use crate::attributes::confusables::ConfusablesParser;
use crate::attributes::crate_level::{
CrateNameParser, MoveSizeLimitParser, RecursionLimitParser, TypeLengthLimitParser,
CrateNameParser, MoveSizeLimitParser, PatternComplexityLimitParser, RecursionLimitParser,
TypeLengthLimitParser,
};
use crate::attributes::deprecation::DeprecationParser;
use crate::attributes::dummy::DummyParser;
@ -187,6 +188,7 @@ attribute_parsers!(
Single<MustUseParser>,
Single<OptimizeParser>,
Single<PathAttributeParser>,
Single<PatternComplexityLimitParser>,
Single<ProcMacroDeriveParser>,
Single<RecursionLimitParser>,
Single<RustcBuiltinMacroParser>,

View file

@ -599,6 +599,9 @@ pub enum AttributeKind {
/// Represents `#[path]`
Path(Symbol, Span),
/// Represents `#[pattern_complexity_limit]`
PatternComplexityLimit { attr_span: Span, limit_span: Span, limit: usize },
/// Represents `#[pointee]`
Pointee(Span),

View file

@ -71,6 +71,7 @@ impl AttributeKind {
ParenSugar(..) => No,
PassByValue(..) => Yes,
Path(..) => No,
PatternComplexityLimit { .. } => No,
Pointee(..) => No,
ProcMacro(..) => No,
ProcMacroAttribute(..) => No,

View file

@ -273,6 +273,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::RecursionLimit { .. }
| AttributeKind::MoveSizeLimit { .. }
| AttributeKind::TypeLengthLimit { .. }
| AttributeKind::PatternComplexityLimit { .. }
) => { /* do nothing */ }
Attribute::Unparsed(attr_item) => {
style = Some(attr_item.style);