Skip to content

Commit 170ef20

Browse files
committed
port #[type_length_limit] to the new attribute parsing infrastructure
1 parent b3c87cc commit 170ef20

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-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
@@ -120,3 +120,30 @@ impl<S: Stage> SingleAttributeParser<S> for MoveSizeLimitParser {
120120
})
121121
}
122122
}
123+
124+
pub(crate) struct TypeLengthLimitParser;
125+
126+
impl<S: Stage> SingleAttributeParser<S> for TypeLengthLimitParser {
127+
const PATH: &[Symbol] = &[sym::type_length_limit];
128+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
129+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
130+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
131+
const TYPE: AttributeType = AttributeType::CrateLevel;
132+
133+
// FIXME: recursion limit is allowed on all targets and ignored,
134+
// even though it should only be valid on crates of course
135+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[Target::Crate]);
136+
137+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
138+
let ArgParser::NameValue(nv) = args else {
139+
cx.expected_name_value(cx.attr_span, None);
140+
return None;
141+
};
142+
143+
Some(AttributeKind::TypeLengthLimit {
144+
limit: cx.parse_limit_int(nv)?,
145+
attr_span: cx.attr_span,
146+
limit_span: nv.value_span,
147+
})
148+
}
149+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use crate::attributes::codegen_attrs::{
2424
UsedParser,
2525
};
2626
use crate::attributes::confusables::ConfusablesParser;
27-
use crate::attributes::crate_level::{CrateNameParser, MoveSizeLimitParser, RecursionLimitParser};
27+
use crate::attributes::crate_level::{
28+
CrateNameParser, MoveSizeLimitParser, RecursionLimitParser, TypeLengthLimitParser,
29+
};
2830
use crate::attributes::deprecation::DeprecationParser;
2931
use crate::attributes::dummy::DummyParser;
3032
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -196,6 +198,7 @@ attribute_parsers!(
196198
Single<ShouldPanicParser>,
197199
Single<SkipDuringMethodDispatchParser>,
198200
Single<TransparencyParser>,
201+
Single<TypeLengthLimitParser>,
199202
Single<WithoutArgs<AllowIncoherentImplParser>>,
200203
Single<WithoutArgs<AllowInternalUnsafeParser>>,
201204
Single<WithoutArgs<AsPtrParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,9 @@ pub enum AttributeKind {
667667
/// Represents `#[type_const]`.
668668
TypeConst(Span),
669669

670+
/// Represents `#[type_length_limit]`
671+
TypeLengthLimit { attr_span: Span, limit_span: Span, limit: usize },
672+
670673
/// Represents `#[rustc_unsafe_specialization_marker]`.
671674
UnsafeSpecializationMarker(Span),
672675

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl AttributeKind {
9191
TargetFeature { .. } => No,
9292
TrackCaller(..) => Yes,
9393
TypeConst(..) => Yes,
94+
TypeLengthLimit { .. } => Yes,
9495
UnsafeSpecializationMarker(..) => No,
9596
UnstableFeatureBound(..) => No,
9697
Used { .. } => No,

compiler/rustc_passes/src/check_attr.rs

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

0 commit comments

Comments
 (0)