Skip to content

Commit b82171d

Browse files
committed
port #[pattern_complexity_limit] to the new attribute parsing infrastructure
1 parent 5dbe099 commit b82171d

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,30 @@ impl<S: Stage> SingleAttributeParser<S> for TypeLengthLimitParser {
147147
})
148148
}
149149
}
150+
151+
pub(crate) struct PatternComplexityLimitParser;
152+
153+
impl<S: Stage> SingleAttributeParser<S> for PatternComplexityLimitParser {
154+
const PATH: &[Symbol] = &[sym::pattern_complexity_limit];
155+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
156+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
157+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
158+
const TYPE: AttributeType = AttributeType::CrateLevel;
159+
160+
// FIXME: recursion limit is allowed on all targets and ignored,
161+
// even though it should only be valid on crates of course
162+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
163+
164+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
165+
let ArgParser::NameValue(nv) = args else {
166+
cx.expected_name_value(cx.attr_span, None);
167+
return None;
168+
};
169+
170+
Some(AttributeKind::PatternComplexityLimit {
171+
limit: cx.parse_limit_int(nv)?,
172+
attr_span: cx.attr_span,
173+
limit_span: nv.value_span,
174+
})
175+
}
176+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use crate::attributes::codegen_attrs::{
2525
};
2626
use crate::attributes::confusables::ConfusablesParser;
2727
use crate::attributes::crate_level::{
28-
CrateNameParser, MoveSizeLimitParser, RecursionLimitParser, TypeLengthLimitParser,
28+
CrateNameParser, MoveSizeLimitParser, PatternComplexityLimitParser, RecursionLimitParser,
29+
TypeLengthLimitParser,
2930
};
3031
use crate::attributes::deprecation::DeprecationParser;
3132
use crate::attributes::dummy::DummyParser;
@@ -187,6 +188,7 @@ attribute_parsers!(
187188
Single<MustUseParser>,
188189
Single<OptimizeParser>,
189190
Single<PathAttributeParser>,
191+
Single<PatternComplexityLimitParser>,
190192
Single<ProcMacroDeriveParser>,
191193
Single<RecursionLimitParser>,
192194
Single<RustcBuiltinMacroParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,9 @@ pub enum AttributeKind {
599599
/// Represents `#[path]`
600600
Path(Symbol, Span),
601601

602+
/// Represents `#[pattern_complexity_limit]`
603+
PatternComplexityLimit { attr_span: Span, limit_span: Span, limit: usize },
604+
602605
/// Represents `#[pointee]`
603606
Pointee(Span),
604607

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl AttributeKind {
7171
ParenSugar(..) => No,
7272
PassByValue(..) => Yes,
7373
Path(..) => No,
74+
PatternComplexityLimit { .. } => No,
7475
Pointee(..) => No,
7576
ProcMacro(..) => No,
7677
ProcMacroAttribute(..) => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
273273
| AttributeKind::RecursionLimit { .. }
274274
| AttributeKind::MoveSizeLimit { .. }
275275
| AttributeKind::TypeLengthLimit { .. }
276+
| AttributeKind::PatternComplexityLimit { .. }
276277
) => { /* do nothing */ }
277278
Attribute::Unparsed(attr_item) => {
278279
style = Some(attr_item.style);

0 commit comments

Comments
 (0)