Skip to content

Commit 86e05cd

Browse files
committed
Auto merge of #142921 - JonathanBrouwer:rustc_attributes_parser, r=oli-obk
Port `#[rustc_layout_scalar_valid_range_start/end]` to the new attrib… Ports `rustc_layout_scalar_valid_range_start` and `rustc_layout_scalar_valid_range_end` to the new attribute parsing infrastructure for #131229 (comment) r? `@jdonszelmann`
2 parents f46ce66 + f98ea3d commit 86e05cd

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

293+
/// Represents `#[rustc_layout_scalar_valid_range_end]`.
294+
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),
295+
296+
/// Represents `#[rustc_layout_scalar_valid_range_start]`.
297+
RustcLayoutScalarValidRangeStart(Box<u128>, Span),
298+
293299
/// Represents `#[rustc_skip_during_method_dispatch]`.
294300
SkipDuringMethodDispatch { array: bool, boxed_slice: bool, span: Span },
295301

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ impl AttributeKind {
3838
NoMangle(..) => No,
3939
Optimize(..) => No,
4040
PubTransparent(..) => Yes,
41+
RustcLayoutScalarValidRangeEnd(..) => Yes,
42+
RustcLayoutScalarValidRangeStart(..) => Yes,
4143
SkipDuringMethodDispatch { .. } => No,
4244
TrackCaller(..) => Yes,
4345
Used { .. } => No,

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
@@ -36,6 +36,7 @@ pub(crate) mod lint_helpers;
3636
pub(crate) mod loop_match;
3737
pub(crate) mod must_use;
3838
pub(crate) mod repr;
39+
pub(crate) mod rustc_internal;
3940
pub(crate) mod semantics;
4041
pub(crate) mod stability;
4142
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
@@ -27,6 +27,9 @@ use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2727
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2828
use crate::attributes::must_use::MustUseParser;
2929
use crate::attributes::repr::{AlignParser, ReprParser};
30+
use crate::attributes::rustc_internal::{
31+
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
32+
};
3033
use crate::attributes::semantics::MayDangleParser;
3134
use crate::attributes::stability::{
3235
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
@@ -131,6 +134,8 @@ attribute_parsers!(
131134
Single<OptimizeParser>,
132135
Single<PubTransparentParser>,
133136
Single<RustcForceInlineParser>,
137+
Single<RustcLayoutScalarValidRangeEnd>,
138+
Single<RustcLayoutScalarValidRangeStart>,
134139
Single<SkipDuringMethodDispatchParser>,
135140
Single<TrackCallerParser>,
136141
Single<TransparencyParser>,
@@ -274,6 +279,16 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
274279
})
275280
}
276281

282+
pub(crate) fn expected_integer_literal(&self, span: Span) -> ErrorGuaranteed {
283+
self.emit_err(AttributeParseError {
284+
span,
285+
attr_span: self.attr_span,
286+
template: self.template.clone(),
287+
attribute: self.attr_path.clone(),
288+
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
289+
})
290+
}
291+
277292
pub(crate) fn expected_list(&self, span: Span) -> ErrorGuaranteed {
278293
self.emit_err(AttributeParseError {
279294
span,

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ pub(crate) struct NakedFunctionIncompatibleAttribute {
510510
pub(crate) enum AttributeParseErrorReason {
511511
ExpectedNoArgs,
512512
ExpectedStringLiteral { byte_string: Option<Span> },
513+
ExpectedIntegerLiteral,
513514
ExpectedAtLeastOneArgument,
514515
ExpectedSingleArgument,
515516
ExpectedList,
@@ -550,6 +551,9 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
550551
diag.span_label(self.span, "expected a string literal here");
551552
}
552553
}
554+
AttributeParseErrorReason::ExpectedIntegerLiteral => {
555+
diag.span_label(self.span, "expected an integer literal here");
556+
}
553557
AttributeParseErrorReason::ExpectedSingleArgument => {
554558
diag.span_label(self.span, "expected a single argument here");
555559
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};
@@ -186,6 +186,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
186186
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
187187
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
188188
}
189+
Attribute::Parsed(
190+
AttributeKind::RustcLayoutScalarValidRangeStart(_num, attr_span)
191+
| AttributeKind::RustcLayoutScalarValidRangeEnd(_num, attr_span),
192+
) => self.check_rustc_layout_scalar_valid_range(*attr_span, span, target),
189193
Attribute::Parsed(
190194
AttributeKind::BodyStability { .. }
191195
| AttributeKind::ConstStabilityIndirect
@@ -237,10 +241,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
237241
&mut doc_aliases,
238242
),
239243
[sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
240-
[sym::rustc_layout_scalar_valid_range_start, ..]
241-
| [sym::rustc_layout_scalar_valid_range_end, ..] => {
242-
self.check_rustc_layout_scalar_valid_range(attr, span, target)
243-
}
244244
[sym::debugger_visualizer, ..] => self.check_debugger_visualizer(attr, target),
245245
[sym::rustc_std_internal_symbol, ..] => {
246246
self.check_rustc_std_internal_symbol(attr, span, target)
@@ -1678,24 +1678,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16781678
}
16791679
}
16801680

1681-
fn check_rustc_layout_scalar_valid_range(&self, attr: &Attribute, span: Span, target: Target) {
1681+
fn check_rustc_layout_scalar_valid_range(&self, attr_span: Span, span: Span, target: Target) {
16821682
if target != Target::Struct {
1683-
self.dcx().emit_err(errors::RustcLayoutScalarValidRangeNotStruct {
1684-
attr_span: attr.span(),
1685-
span,
1686-
});
1683+
self.dcx().emit_err(errors::RustcLayoutScalarValidRangeNotStruct { attr_span, span });
16871684
return;
16881685
}
1689-
1690-
let Some(list) = attr.meta_item_list() else {
1691-
return;
1692-
};
1693-
1694-
if !matches!(&list[..], &[MetaItemInner::Lit(MetaItemLit { kind: LitKind::Int(..), .. })]) {
1695-
self.tcx
1696-
.dcx()
1697-
.emit_err(errors::RustcLayoutScalarValidRangeArg { attr_span: attr.span() });
1698-
}
16991686
}
17001687

17011688
/// 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)