port #[type_length_limit] to the new attribute parsing infrastructure

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

View file

@ -120,3 +120,30 @@ impl<S: Stage> SingleAttributeParser<S> for MoveSizeLimitParser {
})
}
}
pub(crate) struct TypeLengthLimitParser;
impl<S: Stage> SingleAttributeParser<S> for TypeLengthLimitParser {
const PATH: &[Symbol] = &[sym::type_length_limit];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
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::AllowListWarnRest(&[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::TypeLengthLimit {
limit: cx.parse_limit_int(nv)?,
attr_span: cx.attr_span,
limit_span: nv.value_span,
})
}
}

View file

@ -24,7 +24,9 @@ use crate::attributes::codegen_attrs::{
UsedParser,
};
use crate::attributes::confusables::ConfusablesParser;
use crate::attributes::crate_level::{CrateNameParser, MoveSizeLimitParser, RecursionLimitParser};
use crate::attributes::crate_level::{
CrateNameParser, MoveSizeLimitParser, RecursionLimitParser, TypeLengthLimitParser,
};
use crate::attributes::deprecation::DeprecationParser;
use crate::attributes::dummy::DummyParser;
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@ -196,6 +198,7 @@ attribute_parsers!(
Single<ShouldPanicParser>,
Single<SkipDuringMethodDispatchParser>,
Single<TransparencyParser>,
Single<TypeLengthLimitParser>,
Single<WithoutArgs<AllowIncoherentImplParser>>,
Single<WithoutArgs<AllowInternalUnsafeParser>>,
Single<WithoutArgs<AsPtrParser>>,

View file

@ -667,6 +667,9 @@ pub enum AttributeKind {
/// Represents `#[type_const]`.
TypeConst(Span),
/// Represents `#[type_length_limit]`
TypeLengthLimit { attr_span: Span, limit_span: Span, limit: usize },
/// Represents `#[rustc_unsafe_specialization_marker]`.
UnsafeSpecializationMarker(Span),

View file

@ -91,6 +91,7 @@ impl AttributeKind {
TargetFeature { .. } => No,
TrackCaller(..) => Yes,
TypeConst(..) => Yes,
TypeLengthLimit { .. } => Yes,
UnsafeSpecializationMarker(..) => No,
UnstableFeatureBound(..) => No,
Used { .. } => No,

View file

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