Skip to content

Commit f98ea3d

Browse files
Port #[rustc_layout_scalar_valid_range_start/end] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
1 parent e61dd43 commit f98ea3d

File tree

14 files changed

+146
-69
lines changed

14 files changed

+146
-69
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ pub enum AttributeKind {
273273
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
274274
Repr(ThinVec<(ReprAttr, Span)>),
275275

276+
/// Represents `#[rustc_layout_scalar_valid_range_end]`.
277+
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),
278+
279+
/// Represents `#[rustc_layout_scalar_valid_range_start]`.
280+
RustcLayoutScalarValidRangeStart(Box<u128>, Span),
281+
276282
/// Represents `#[rustc_skip_during_method_dispatch]`.
277283
SkipDuringMethodDispatch { array: bool, boxed_slice: bool, span: Span },
278284

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ impl AttributeKind {
3636
NoMangle(..) => No,
3737
Optimize(..) => No,
3838
PubTransparent(..) => Yes,
39+
RustcLayoutScalarValidRangeEnd(..) => Yes,
40+
RustcLayoutScalarValidRangeStart(..) => Yes,
3941
SkipDuringMethodDispatch { .. } => No,
4042
TrackCaller(..) => Yes,
4143
}

compiler/rustc_attr_data_structures/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ pub trait PrintAttribute {
4949
fn print_attribute(&self, p: &mut Printer);
5050
}
5151

52+
impl PrintAttribute for u128 {
53+
fn should_render(&self) -> bool {
54+
true
55+
}
56+
57+
fn print_attribute(&self, p: &mut Printer) {
58+
p.word(self.to_string())
59+
}
60+
}
61+
5262
impl<T: PrintAttribute> PrintAttribute for &T {
5363
fn should_render(&self) -> bool {
5464
T::should_render(self)

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) mod lint_helpers;
3535
pub(crate) mod loop_match;
3636
pub(crate) mod must_use;
3737
pub(crate) mod repr;
38+
pub(crate) mod rustc_internal;
3839
pub(crate) mod semantics;
3940
pub(crate) mod stability;
4041
pub(crate) mod traits;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use rustc_ast::LitKind;
2+
use rustc_attr_data_structures::AttributeKind;
3+
use rustc_feature::{AttributeTemplate, template};
4+
use rustc_span::{Symbol, sym};
5+
6+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
7+
use crate::context::{AcceptContext, Stage};
8+
use crate::parser::ArgParser;
9+
10+
pub(crate) struct RustcLayoutScalarValidRangeStart;
11+
12+
impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeStart {
13+
const PATH: &'static [Symbol] = &[sym::rustc_layout_scalar_valid_range_start];
14+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
15+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
16+
const TEMPLATE: AttributeTemplate = template!(List: "start");
17+
18+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
19+
parse_rustc_layout_scalar_valid_range(cx, args)
20+
.map(|n| AttributeKind::RustcLayoutScalarValidRangeStart(n, cx.attr_span))
21+
}
22+
}
23+
24+
pub(crate) struct RustcLayoutScalarValidRangeEnd;
25+
26+
impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeEnd {
27+
const PATH: &'static [Symbol] = &[sym::rustc_layout_scalar_valid_range_end];
28+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
29+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
30+
const TEMPLATE: AttributeTemplate = template!(List: "end");
31+
32+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
33+
parse_rustc_layout_scalar_valid_range(cx, args)
34+
.map(|n| AttributeKind::RustcLayoutScalarValidRangeEnd(n, cx.attr_span))
35+
}
36+
}
37+
38+
fn parse_rustc_layout_scalar_valid_range<S: Stage>(
39+
cx: &mut AcceptContext<'_, '_, S>,
40+
args: &ArgParser<'_>,
41+
) -> Option<Box<u128>> {
42+
let Some(list) = args.list() else {
43+
cx.expected_list(cx.attr_span);
44+
return None;
45+
};
46+
let Some(single) = list.single() else {
47+
cx.expected_single_argument(list.span);
48+
return None;
49+
};
50+
let Some(lit) = single.lit() else {
51+
cx.expected_integer_literal(single.span());
52+
return None;
53+
};
54+
let LitKind::Int(num, _ty) = lit.kind else {
55+
cx.expected_integer_literal(single.span());
56+
return None;
57+
};
58+
Some(Box::new(num.0))
59+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2525
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2626
use crate::attributes::must_use::MustUseParser;
2727
use crate::attributes::repr::{AlignParser, ReprParser};
28+
use crate::attributes::rustc_internal::{
29+
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
30+
};
2831
use crate::attributes::semantics::MayDangleParser;
2932
use crate::attributes::stability::{
3033
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
@@ -126,6 +129,8 @@ attribute_parsers!(
126129
Single<OptimizeParser>,
127130
Single<PubTransparentParser>,
128131
Single<RustcForceInlineParser>,
132+
Single<RustcLayoutScalarValidRangeEnd>,
133+
Single<RustcLayoutScalarValidRangeStart>,
129134
Single<SkipDuringMethodDispatchParser>,
130135
Single<TrackCallerParser>,
131136
Single<TransparencyParser>,
@@ -269,6 +274,16 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
269274
})
270275
}
271276

277+
pub(crate) fn expected_integer_literal(&self, span: Span) -> ErrorGuaranteed {
278+
self.emit_err(AttributeParseError {
279+
span,
280+
attr_span: self.attr_span,
281+
template: self.template.clone(),
282+
attribute: self.attr_path.clone(),
283+
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
284+
})
285+
}
286+
272287
pub(crate) fn expected_list(&self, span: Span) -> ErrorGuaranteed {
273288
self.emit_err(AttributeParseError {
274289
span,

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ pub(crate) struct NakedFunctionIncompatibleAttribute {
503503
pub(crate) enum AttributeParseErrorReason {
504504
ExpectedNoArgs,
505505
ExpectedStringLiteral { byte_string: Option<Span> },
506+
ExpectedIntegerLiteral,
506507
ExpectedAtLeastOneArgument,
507508
ExpectedSingleArgument,
508509
ExpectedList,
@@ -543,6 +544,9 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
543544
diag.span_label(self.span, "expected a string literal here");
544545
}
545546
}
547+
AttributeParseErrorReason::ExpectedIntegerLiteral => {
548+
diag.span_label(self.span, "expected an integer literal here");
549+
}
546550
AttributeParseErrorReason::ExpectedSingleArgument => {
547551
diag.span_label(self.span, "expected a single argument here");
548552
diag.code(E0805);

compiler/rustc_middle/src/ty/context.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::{fmt, iter, mem};
1717

1818
use rustc_abi::{ExternAbi, FieldIdx, Layout, LayoutData, TargetDataLayout, VariantIdx};
1919
use rustc_ast as ast;
20+
use rustc_attr_data_structures::{AttributeKind, find_attr};
2021
use rustc_data_structures::defer;
2122
use rustc_data_structures::fingerprint::Fingerprint;
2223
use rustc_data_structures::fx::FxHashMap;
@@ -1648,32 +1649,9 @@ impl<'tcx> TyCtxt<'tcx> {
16481649
/// `rustc_layout_scalar_valid_range` attribute.
16491650
// FIXME(eddyb) this is an awkward spot for this method, maybe move it?
16501651
pub fn layout_scalar_valid_range(self, def_id: DefId) -> (Bound<u128>, Bound<u128>) {
1651-
let get = |name| {
1652-
let Some(attr) = self.get_attr(def_id, name) else {
1653-
return Bound::Unbounded;
1654-
};
1655-
debug!("layout_scalar_valid_range: attr={:?}", attr);
1656-
if let Some(
1657-
&[
1658-
ast::MetaItemInner::Lit(ast::MetaItemLit {
1659-
kind: ast::LitKind::Int(a, _), ..
1660-
}),
1661-
],
1662-
) = attr.meta_item_list().as_deref()
1663-
{
1664-
Bound::Included(a.get())
1665-
} else {
1666-
self.dcx().span_delayed_bug(
1667-
attr.span(),
1668-
"invalid rustc_layout_scalar_valid_range attribute",
1669-
);
1670-
Bound::Unbounded
1671-
}
1672-
};
1673-
(
1674-
get(sym::rustc_layout_scalar_valid_range_start),
1675-
get(sym::rustc_layout_scalar_valid_range_end),
1676-
)
1652+
let start = find_attr!(self.get_all_attrs(def_id), AttributeKind::RustcLayoutScalarValidRangeStart(n, _) => Bound::Included(**n)).unwrap_or(Bound::Unbounded);
1653+
let end = find_attr!(self.get_all_attrs(def_id), AttributeKind::RustcLayoutScalarValidRangeEnd(n, _) => Bound::Included(**n)).unwrap_or(Bound::Unbounded);
1654+
(start, end)
16771655
}
16781656

16791657
pub fn lift<T: Lift<TyCtxt<'tcx>>>(self, value: T) -> Option<T::Lifted> {

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,6 @@ passes_rustc_force_inline_coro =
618618
attribute cannot be applied to a `async`, `gen` or `async gen` function
619619
.label = `async`, `gen` or `async gen` function
620620
621-
passes_rustc_layout_scalar_valid_range_arg =
622-
expected exactly one integer literal argument
623-
624621
passes_rustc_layout_scalar_valid_range_not_struct =
625622
attribute should be applied to a struct
626623
.label = not a struct

compiler/rustc_passes/src/check_attr.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::cell::Cell;
99
use std::collections::hash_map::Entry;
1010

1111
use rustc_abi::{Align, ExternAbi, Size};
12-
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, MetaItemLit, ast};
12+
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, ast};
1313
use rustc_attr_data_structures::{AttributeKind, InlineAttr, ReprAttr, find_attr};
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
@@ -183,6 +183,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
183183
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
184184
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
185185
}
186+
Attribute::Parsed(
187+
AttributeKind::RustcLayoutScalarValidRangeStart(_num, attr_span)
188+
| AttributeKind::RustcLayoutScalarValidRangeEnd(_num, attr_span),
189+
) => self.check_rustc_layout_scalar_valid_range(*attr_span, span, target),
186190
Attribute::Parsed(
187191
AttributeKind::BodyStability { .. }
188192
| AttributeKind::ConstStabilityIndirect
@@ -228,10 +232,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
228232
&mut doc_aliases,
229233
),
230234
[sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
231-
[sym::rustc_layout_scalar_valid_range_start, ..]
232-
| [sym::rustc_layout_scalar_valid_range_end, ..] => {
233-
self.check_rustc_layout_scalar_valid_range(attr, span, target)
234-
}
235235
[sym::debugger_visualizer, ..] => self.check_debugger_visualizer(attr, target),
236236
[sym::rustc_std_internal_symbol, ..] => {
237237
self.check_rustc_std_internal_symbol(attr, span, target)
@@ -1675,24 +1675,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16751675
}
16761676
}
16771677

1678-
fn check_rustc_layout_scalar_valid_range(&self, attr: &Attribute, span: Span, target: Target) {
1678+
fn check_rustc_layout_scalar_valid_range(&self, attr_span: Span, span: Span, target: Target) {
16791679
if target != Target::Struct {
1680-
self.dcx().emit_err(errors::RustcLayoutScalarValidRangeNotStruct {
1681-
attr_span: attr.span(),
1682-
span,
1683-
});
1680+
self.dcx().emit_err(errors::RustcLayoutScalarValidRangeNotStruct { attr_span, span });
16841681
return;
16851682
}
1686-
1687-
let Some(list) = attr.meta_item_list() else {
1688-
return;
1689-
};
1690-
1691-
if !matches!(&list[..], &[MetaItemInner::Lit(MetaItemLit { kind: LitKind::Int(..), .. })]) {
1692-
self.tcx
1693-
.dcx()
1694-
.emit_err(errors::RustcLayoutScalarValidRangeArg { attr_span: attr.span() });
1695-
}
16961683
}
16971684

16981685
/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,6 @@ pub(crate) struct RustcLayoutScalarValidRangeNotStruct {
535535
pub span: Span,
536536
}
537537

538-
#[derive(Diagnostic)]
539-
#[diag(passes_rustc_layout_scalar_valid_range_arg)]
540-
pub(crate) struct RustcLayoutScalarValidRangeArg {
541-
#[primary_span]
542-
pub attr_span: Span,
543-
}
544-
545538
#[derive(Diagnostic)]
546539
#[diag(passes_rustc_legacy_const_generics_only)]
547540
pub(crate) struct RustcLegacyConstGenericsOnly {
Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
1-
error: expected exactly one integer literal argument
1+
error[E0539]: malformed `rustc_layout_scalar_valid_range_start` attribute input
22
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:3:1
33
|
44
LL | #[rustc_layout_scalar_valid_range_start(u32::MAX)]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------^^
6+
| | |
7+
| | expected an integer literal here
8+
| help: must be of the form: `#[rustc_layout_scalar_valid_range_start(start)]`
69

7-
error: expected exactly one integer literal argument
10+
error[E0805]: malformed `rustc_layout_scalar_valid_range_end` attribute input
811
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:6:1
912
|
1013
LL | #[rustc_layout_scalar_valid_range_end(1, 2)]
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------^
15+
| | |
16+
| | expected a single argument here
17+
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
1218

13-
error: expected exactly one integer literal argument
19+
error[E0539]: malformed `rustc_layout_scalar_valid_range_end` attribute input
1420
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:9:1
1521
|
1622
LL | #[rustc_layout_scalar_valid_range_end(a = "a")]
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------^^
24+
| | |
25+
| | expected an integer literal here
26+
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
27+
28+
error[E0539]: malformed `rustc_layout_scalar_valid_range_start` attribute input
29+
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:18:1
30+
|
31+
LL | #[rustc_layout_scalar_valid_range_start(rustc_layout_scalar_valid_range_start)]
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^
33+
| | |
34+
| | expected an integer literal here
35+
| help: must be of the form: `#[rustc_layout_scalar_valid_range_start(start)]`
1836

1937
error: attribute should be applied to a struct
2038
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:12:1
@@ -27,11 +45,7 @@ LL | | Y = 14,
2745
LL | | }
2846
| |_- not a struct
2947

30-
error: expected exactly one integer literal argument
31-
--> $DIR/invalid_rustc_layout_scalar_valid_range.rs:18:1
32-
|
33-
LL | #[rustc_layout_scalar_valid_range_start(rustc_layout_scalar_valid_range_start)]
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35-
3648
error: aborting due to 5 previous errors
3749

50+
Some errors have detailed explanations: E0539, E0805.
51+
For more information about an error, try `rustc --explain E0539`.

tests/ui/parser/bad-lit-suffixes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ extern "C" {}
4141

4242
#[rustc_layout_scalar_valid_range_start(0suffix)]
4343
//~^ ERROR invalid suffix `suffix` for number literal
44+
//~| ERROR malformed `rustc_layout_scalar_valid_range_start` attribute input
4445
struct S;

tests/ui/parser/bad-lit-suffixes.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,15 @@ LL | 1.0e10suffix;
150150
|
151151
= help: valid suffixes are `f32` and `f64`
152152

153-
error: aborting due to 20 previous errors; 2 warnings emitted
153+
error[E0805]: malformed `rustc_layout_scalar_valid_range_start` attribute input
154+
--> $DIR/bad-lit-suffixes.rs:42:1
155+
|
156+
LL | #[rustc_layout_scalar_valid_range_start(0suffix)]
157+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------^
158+
| | |
159+
| | expected a single argument here
160+
| help: must be of the form: `#[rustc_layout_scalar_valid_range_start(start)]`
161+
162+
error: aborting due to 21 previous errors; 2 warnings emitted
154163

164+
For more information about this error, try `rustc --explain E0805`.

0 commit comments

Comments
 (0)