Skip to content

Commit 37abd08

Browse files
Port #[rustc_layout_scalar_valid_range_start/end] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 42245d3 commit 37abd08

File tree

13 files changed

+144
-69
lines changed

13 files changed

+144
-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
@@ -259,6 +259,12 @@ pub enum AttributeKind {
259259
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
260260
Repr(ThinVec<(ReprAttr, Span)>),
261261

262+
/// Represents `#[rustc_layout_scalar_valid_range_end]`.
263+
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),
264+
265+
/// Represents `#[rustc_layout_scalar_valid_range_start]`.
266+
RustcLayoutScalarValidRangeStart(Box<u128>, Span),
267+
262268
/// Represents `#[stable]`, `#[unstable]` and `#[rustc_allowed_through_unstable_modules]`.
263269
Stability {
264270
stability: Stability,

compiler/rustc_attr_data_structures/src/lib.rs

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

50+
impl PrintAttribute for u128 {
51+
fn should_render(&self) -> bool {
52+
true
53+
}
54+
55+
fn print_attribute(&self, p: &mut Printer) {
56+
p.word(self.to_string())
57+
}
58+
}
59+
5060
impl<T: PrintAttribute> PrintAttribute for &T {
5161
fn should_render(&self) -> bool {
5262
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
@@ -34,6 +34,7 @@ pub(crate) mod inline;
3434
pub(crate) mod lint_helpers;
3535
pub(crate) mod must_use;
3636
pub(crate) mod repr;
37+
pub(crate) mod rustc_internal;
3738
pub(crate) mod semantics;
3839
pub(crate) mod stability;
3940
pub(crate) mod transparency;
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
@@ -22,6 +22,9 @@ use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
2222
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2323
use crate::attributes::must_use::MustUseParser;
2424
use crate::attributes::repr::{AlignParser, ReprParser};
25+
use crate::attributes::rustc_internal::{
26+
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
27+
};
2528
use crate::attributes::semantics::MayDangleParser;
2629
use crate::attributes::stability::{
2730
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
@@ -119,6 +122,8 @@ attribute_parsers!(
119122
Single<OptimizeParser>,
120123
Single<PubTransparentParser>,
121124
Single<RustcForceInlineParser>,
125+
Single<RustcLayoutScalarValidRangeEnd>,
126+
Single<RustcLayoutScalarValidRangeStart>,
122127
Single<TransparencyParser>,
123128
// tidy-alphabetical-end
124129
];
@@ -260,6 +265,16 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
260265
})
261266
}
262267

268+
pub(crate) fn expected_integer_literal(&self, span: Span) -> ErrorGuaranteed {
269+
self.emit_err(AttributeParseError {
270+
span,
271+
attr_span: self.attr_span,
272+
template: self.template.clone(),
273+
attribute: self.attr_path.clone(),
274+
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
275+
})
276+
}
277+
263278
pub(crate) fn expected_list(&self, span: Span) -> ErrorGuaranteed {
264279
self.emit_err(AttributeParseError {
265280
span,

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ pub(crate) struct NakedFunctionIncompatibleAttribute {
496496
pub(crate) enum AttributeParseErrorReason {
497497
ExpectedNoArgs,
498498
ExpectedStringLiteral { byte_string: Option<Span> },
499+
ExpectedIntegerLiteral,
499500
ExpectedSingleArgument,
500501
ExpectedList,
501502
UnexpectedLiteral,
@@ -535,6 +536,9 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
535536
diag.span_label(self.span, "expected a string literal here");
536537
}
537538
}
539+
AttributeParseErrorReason::ExpectedIntegerLiteral => {
540+
diag.span_label(self.span, "expected an integer literal here");
541+
}
538542
AttributeParseErrorReason::ExpectedSingleArgument => {
539543
diag.span_label(self.span, "expected a single argument here");
540544
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;
@@ -1649,32 +1650,9 @@ impl<'tcx> TyCtxt<'tcx> {
16491650
/// `rustc_layout_scalar_valid_range` attribute.
16501651
// FIXME(eddyb) this is an awkward spot for this method, maybe move it?
16511652
pub fn layout_scalar_valid_range(self, def_id: DefId) -> (Bound<u128>, Bound<u128>) {
1652-
let get = |name| {
1653-
let Some(attr) = self.get_attr(def_id, name) else {
1654-
return Bound::Unbounded;
1655-
};
1656-
debug!("layout_scalar_valid_range: attr={:?}", attr);
1657-
if let Some(
1658-
&[
1659-
ast::MetaItemInner::Lit(ast::MetaItemLit {
1660-
kind: ast::LitKind::Int(a, _), ..
1661-
}),
1662-
],
1663-
) = attr.meta_item_list().as_deref()
1664-
{
1665-
Bound::Included(a.get())
1666-
} else {
1667-
self.dcx().span_delayed_bug(
1668-
attr.span(),
1669-
"invalid rustc_layout_scalar_valid_range attribute",
1670-
);
1671-
Bound::Unbounded
1672-
}
1673-
};
1674-
(
1675-
get(sym::rustc_layout_scalar_valid_range_start),
1676-
get(sym::rustc_layout_scalar_valid_range_end),
1677-
)
1653+
let start = find_attr!(self.get_all_attrs(def_id), AttributeKind::RustcLayoutScalarValidRangeStart(n, _) => Bound::Included(**n)).unwrap_or(Bound::Unbounded);
1654+
let end = find_attr!(self.get_all_attrs(def_id), AttributeKind::RustcLayoutScalarValidRangeEnd(n, _) => Bound::Included(**n)).unwrap_or(Bound::Unbounded);
1655+
(start, end)
16781656
}
16791657

16801658
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
@@ -606,9 +606,6 @@ passes_rustc_force_inline_coro =
606606
attribute cannot be applied to a `async`, `gen` or `async gen` function
607607
.label = `async`, `gen` or `async gen` function
608608
609-
passes_rustc_layout_scalar_valid_range_arg =
610-
expected exactly one integer literal argument
611-
612609
passes_rustc_layout_scalar_valid_range_not_struct =
613610
attribute should be applied to a struct
614611
.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};
@@ -163,6 +163,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
163163
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
164164
self.check_naked(hir_id, *attr_span, span, target)
165165
}
166+
Attribute::Parsed(
167+
AttributeKind::RustcLayoutScalarValidRangeStart(_num, attr_span)
168+
| AttributeKind::RustcLayoutScalarValidRangeEnd(_num, attr_span),
169+
) => self.check_rustc_layout_scalar_valid_range(*attr_span, span, target),
166170
Attribute::Parsed(
167171
AttributeKind::BodyStability { .. }
168172
| AttributeKind::ConstStabilityIndirect
@@ -212,10 +216,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
212216
),
213217
[sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
214218
[sym::export_name, ..] => self.check_export_name(hir_id, attr, span, target),
215-
[sym::rustc_layout_scalar_valid_range_start, ..]
216-
| [sym::rustc_layout_scalar_valid_range_end, ..] => {
217-
self.check_rustc_layout_scalar_valid_range(attr, span, target)
218-
}
219219
[sym::debugger_visualizer, ..] => self.check_debugger_visualizer(attr, target),
220220
[sym::rustc_std_internal_symbol, ..] => {
221221
self.check_rustc_std_internal_symbol(attr, span, target)
@@ -1660,24 +1660,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16601660
}
16611661
}
16621662

1663-
fn check_rustc_layout_scalar_valid_range(&self, attr: &Attribute, span: Span, target: Target) {
1663+
fn check_rustc_layout_scalar_valid_range(&self, attr_span: Span, span: Span, target: Target) {
16641664
if target != Target::Struct {
1665-
self.dcx().emit_err(errors::RustcLayoutScalarValidRangeNotStruct {
1666-
attr_span: attr.span(),
1667-
span,
1668-
});
1665+
self.dcx().emit_err(errors::RustcLayoutScalarValidRangeNotStruct { attr_span, span });
16691666
return;
16701667
}
1671-
1672-
let Some(list) = attr.meta_item_list() else {
1673-
return;
1674-
};
1675-
1676-
if !matches!(&list[..], &[MetaItemInner::Lit(MetaItemLit { kind: LitKind::Int(..), .. })]) {
1677-
self.tcx
1678-
.dcx()
1679-
.emit_err(errors::RustcLayoutScalarValidRangeArg { attr_span: attr.span() });
1680-
}
16811668
}
16821669

16831670
/// 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
@@ -505,13 +505,6 @@ pub(crate) struct RustcLayoutScalarValidRangeNotStruct {
505505
pub span: Span,
506506
}
507507

508-
#[derive(Diagnostic)]
509-
#[diag(passes_rustc_layout_scalar_valid_range_arg)]
510-
pub(crate) struct RustcLayoutScalarValidRangeArg {
511-
#[primary_span]
512-
pub attr_span: Span,
513-
}
514-
515508
#[derive(Diagnostic)]
516509
#[diag(passes_rustc_legacy_const_generics_only)]
517510
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)