Skip to content

Commit b79fa8e

Browse files
committed
Reuse existing limit parser
1 parent 3b29ae1 commit b79fa8e

File tree

7 files changed

+46
-46
lines changed

7 files changed

+46
-46
lines changed

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,4 @@
1-
use std::num::IntErrorKind;
2-
3-
use rustc_hir::limit::Limit;
4-
51
use super::prelude::*;
6-
use crate::session_diagnostics::LimitInvalid;
7-
8-
impl<S: Stage> AcceptContext<'_, '_, S> {
9-
fn parse_limit_int(&self, nv: &NameValueParser) -> Option<Limit> {
10-
let Some(limit) = nv.value_as_str() else {
11-
self.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
12-
return None;
13-
};
14-
15-
let error_str = match limit.as_str().parse() {
16-
Ok(i) => return Some(Limit::new(i)),
17-
Err(e) => match e.kind() {
18-
IntErrorKind::PosOverflow => "`limit` is too large",
19-
IntErrorKind::Empty => "`limit` must be a non-negative integer",
20-
IntErrorKind::InvalidDigit => "not a valid integer",
21-
IntErrorKind::NegOverflow => {
22-
panic!(
23-
"`limit` should never negatively overflow since we're parsing into a usize and we'd get Empty instead"
24-
)
25-
}
26-
IntErrorKind::Zero => {
27-
panic!("zero is a valid `limit` so should have returned Ok() when parsing")
28-
}
29-
kind => panic!("unimplemented IntErrorKind variant: {:?}", kind),
30-
},
31-
};
32-
33-
self.emit_err(LimitInvalid { span: self.attr_span, value_span: nv.value_span, error_str });
34-
35-
None
36-
}
37-
}
382

393
pub(crate) struct CrateNameParser;
404

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use rustc_hir::limit::Limit;
2-
31
use super::prelude::*;
42
use super::util::parse_single_integer;
53

@@ -59,10 +57,13 @@ impl<S: Stage> SingleAttributeParser<S> for RustcSimdMonomorphizeLaneLimitParser
5957
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
6058
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
6159
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
62-
const TEMPLATE: AttributeTemplate = template!(List: &["N"]);
60+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
6361

6462
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
65-
parse_single_integer(cx, args)
66-
.map(|n| AttributeKind::RustcSimdMonomorphizeLaneLimit(Limit::new(n as usize)))
63+
let ArgParser::NameValue(nv) = args else {
64+
cx.expected_name_value(cx.attr_span, None);
65+
return None;
66+
};
67+
Some(AttributeKind::RustcSimdMonomorphizeLaneLimit(cx.parse_limit_int(nv)?))
6768
}
6869
}

compiler/rustc_attr_parsing/src/attributes/util.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
use std::num::IntErrorKind;
2+
13
use rustc_ast::LitKind;
24
use rustc_ast::attr::AttributeExt;
35
use rustc_feature::is_builtin_attr_name;
46
use rustc_hir::RustcVersion;
7+
use rustc_hir::limit::Limit;
58
use rustc_span::{Symbol, sym};
69

710
use crate::context::{AcceptContext, Stage};
8-
use crate::parser::ArgParser;
11+
use crate::parser::{ArgParser, NameValueParser};
12+
use crate::session_diagnostics::LimitInvalid;
913

1014
/// Parse a rustc version number written inside string literal in an attribute,
1115
/// like appears in `since = "1.0.0"`. Suffixes like "-dev" and "-nightly" are
@@ -85,3 +89,34 @@ pub(crate) fn parse_single_integer<S: Stage>(
8589
};
8690
Some(num.0)
8791
}
92+
93+
impl<S: Stage> AcceptContext<'_, '_, S> {
94+
pub(crate) fn parse_limit_int(&self, nv: &NameValueParser) -> Option<Limit> {
95+
let Some(limit) = nv.value_as_str() else {
96+
self.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
97+
return None;
98+
};
99+
100+
let error_str = match limit.as_str().parse() {
101+
Ok(i) => return Some(Limit::new(i)),
102+
Err(e) => match e.kind() {
103+
IntErrorKind::PosOverflow => "`limit` is too large",
104+
IntErrorKind::Empty => "`limit` must be a non-negative integer",
105+
IntErrorKind::InvalidDigit => "not a valid integer",
106+
IntErrorKind::NegOverflow => {
107+
panic!(
108+
"`limit` should never negatively overflow since we're parsing into a usize and we'd get Empty instead"
109+
)
110+
}
111+
IntErrorKind::Zero => {
112+
panic!("zero is a valid `limit` so should have returned Ok() when parsing")
113+
}
114+
kind => panic!("unimplemented IntErrorKind variant: {:?}", kind),
115+
},
116+
};
117+
118+
self.emit_err(LimitInvalid { span: self.attr_span, value_span: nv.value_span, error_str });
119+
120+
None
121+
}
122+
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ pub enum AttributeKind {
642642
/// Represents `#[rustc_object_lifetime_default]`.
643643
RustcObjectLifetimeDefault,
644644

645-
/// Represents `#[rustc_simd_monomorphize_lane_limit(N)]`.
645+
/// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`.
646646
RustcSimdMonomorphizeLaneLimit(Limit),
647647

648648
/// Represents `#[sanitize]`

tests/ui/simd/simd-lane-limit-err-npow2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//@ build-fail
44

55
#[repr(simd, packed)]
6-
#[rustc_simd_monomorphize_lane_limit(4)]
6+
#[rustc_simd_monomorphize_lane_limit = "4"]
77
struct V<T, const N: usize>([T; N]);
88

99
fn main() {

tests/ui/simd/simd-lane-limit-err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//@ build-fail
44

55
#[repr(simd, packed)]
6-
#[rustc_simd_monomorphize_lane_limit(4)]
6+
#[rustc_simd_monomorphize_lane_limit = "4"]
77
struct V<T, const N: usize>([T; N]);
88

99
fn main() {

tests/ui/simd/simd-lane-limit-ok.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//@ build-pass
44

55
#[repr(simd, packed)]
6-
#[rustc_simd_monomorphize_lane_limit(8)]
6+
#[rustc_simd_monomorphize_lane_limit = "8"]
77
struct V<T, const N: usize>([T; N]);
88

99
const LANES: usize = 4;

0 commit comments

Comments
 (0)