Skip to content

Commit e7af21c

Browse files
committed
port #[type_length_limit] to the new attribute parsing infrastructure
1 parent 3399405 commit e7af21c

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::AllowList(ALL_TARGETS);
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};
@@ -195,6 +197,7 @@ attribute_parsers!(
195197
Single<ShouldPanicParser>,
196198
Single<SkipDuringMethodDispatchParser>,
197199
Single<TransparencyParser>,
200+
Single<TypeLengthLimitParser>,
198201
Single<WithoutArgs<AllowIncoherentImplParser>>,
199202
Single<WithoutArgs<AllowInternalUnsafeParser>>,
200203
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
@@ -550,6 +550,9 @@ pub enum AttributeKind {
550550
/// Represents `#[type_const]`.
551551
TypeConst(Span),
552552

553+
/// Represents `#[type_length_limit]`
554+
TypeLengthLimit { attr_span: Span, limit_span: Span, limit: usize },
555+
553556
/// Represents `#[rustc_unsafe_specialization_marker]`.
554557
UnsafeSpecializationMarker(Span),
555558

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

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

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
254254
| AttributeKind::CrateName { .. }
255255
| AttributeKind::RecursionLimit { .. }
256256
| AttributeKind::MoveSizeLimit { .. }
257+
| AttributeKind::TypeLengthLimit { .. }
257258
) => { /* do nothing */ }
258259
Attribute::Unparsed(attr_item) => {
259260
style = Some(attr_item.style);

0 commit comments

Comments
 (0)