Skip to content

Commit b198946

Browse files
committed
matching on parsers
1 parent 291c884 commit b198946

File tree

31 files changed

+235
-361
lines changed

31 files changed

+235
-361
lines changed

,

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4 } else if let Some((feature, level)) = parse_stability(cx, args) { │ 3 (<unsize>&*&[sym::unstable], <closure-to-fn-pointer>|this: &mut StabilityGroup, cx: &AttributeAcceptContext<_ args:

compiler/rustc_attr_data_structures/src/attributes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_abi::Align;
2-
32
use rustc_ast::token::CommentKind;
43
use rustc_ast::{self as ast, AttrStyle};
54
use rustc_macros::{Decodable, Encodable, HashStable_Generic};

compiler/rustc_attr_parsing/messages.ftl

+6-7
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ attr_parsing_invalid_repr_hint_no_paren =
6666
attr_parsing_invalid_repr_hint_no_value =
6767
invalid representation hint: `{$name}` does not take a value
6868
69-
attr_parsing_unrecognized_repr_hint =
70-
unrecognized representation hint
71-
.help = valid reprs are `Rust` (default), `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
72-
7369
attr_parsing_invalid_since =
7470
'since' must be a Rust version number, such as "1.31.0"
7571
@@ -94,6 +90,8 @@ attr_parsing_multiple_stability_levels =
9490
attr_parsing_non_ident_feature =
9591
'feature' is not an identifier
9692
93+
attr_parsing_repr_ident =
94+
meta item in `repr` must be an identifier
9795
attr_parsing_rustc_allowed_unstable_pairing =
9896
`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute
9997
@@ -112,6 +110,10 @@ attr_parsing_unknown_meta_item =
112110
attr_parsing_unknown_version_literal =
113111
unknown version literal format, assuming it refers to a future version
114112
113+
attr_parsing_unrecognized_repr_hint =
114+
unrecognized representation hint
115+
.help = valid reprs are `Rust` (default), `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
116+
115117
attr_parsing_unstable_cfg_target_compact =
116118
compact `cfg(target(..))` is experimental and subject to change
117119
@@ -132,6 +134,3 @@ attr_parsing_unused_multiple =
132134
multiple `{$name}` attributes
133135
.suggestion = remove this attribute
134136
.note = attribute also specified here
135-
136-
attr_parsing_repr_ident =
137-
meta item in `repr` must be an identifier

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_span::{Symbol, sym};
33

44
use super::{CombineAttributeGroup, ConvertFn};
55
use crate::context::AttributeAcceptContext;
6-
use crate::parser::{ArgParser, GenericArgParser, MetaItemParser};
6+
use crate::parser::ArgParser;
77
use crate::session_diagnostics;
88

99
pub(crate) struct AllowInternalUnstableGroup;
@@ -14,7 +14,7 @@ impl CombineAttributeGroup for AllowInternalUnstableGroup {
1414

1515
fn extend<'a>(
1616
cx: &'a AttributeAcceptContext<'a>,
17-
args: &'a GenericArgParser<'a, rustc_ast::Expr>,
17+
args: &'a ArgParser<'a>,
1818
) -> impl IntoIterator<Item = Self::Item> + 'a {
1919
parse_unstable(cx, args, Self::PATH[0])
2020
}
@@ -28,15 +28,15 @@ impl CombineAttributeGroup for AllowConstFnUnstableGroup {
2828

2929
fn extend<'a>(
3030
cx: &'a AttributeAcceptContext<'a>,
31-
args: &'a GenericArgParser<'a, rustc_ast::Expr>,
31+
args: &'a ArgParser<'a>,
3232
) -> impl IntoIterator<Item = Self::Item> + 'a {
3333
parse_unstable(cx, args, Self::PATH[0])
3434
}
3535
}
3636

3737
fn parse_unstable<'a>(
3838
cx: &AttributeAcceptContext<'_>,
39-
args: &'a impl ArgParser<'a>,
39+
args: &'a ArgParser<'a>,
4040
symbol: Symbol,
4141
) -> impl IntoIterator<Item = Symbol> {
4242
let mut res = Vec::new();

compiler/rustc_attr_parsing/src/attributes/cfg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc_session::parse::feature_err;
1111
use rustc_span::symbol::kw;
1212
use rustc_span::{Span, Symbol, sym};
1313

14-
use crate::{fluent_generated, parse_version};
1514
use crate::session_diagnostics::{self, UnsupportedLiteralReason};
15+
use crate::{fluent_generated, parse_version};
1616

1717
#[derive(Clone, Debug)]
1818
pub struct Condition {

compiler/rustc_attr_parsing/src/attributes/confusables.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use thin_vec::ThinVec;
55

66
use super::{AttributeGroup, AttributeMapping};
77
use crate::context::AttributeGroupContext;
8-
use crate::parser::ArgParser;
98
use crate::session_diagnostics;
109

1110
// TODO: turn into CombineGroup?

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use rustc_span::{Span, Symbol, sym};
55
use super::SingleAttributeGroup;
66
use super::util::parse_version;
77
use crate::context::AttributeAcceptContext;
8-
use crate::parser::{ArgParser, GenericArgParser, MetaItemParser, NameValueParser};
8+
use crate::parser::ArgParser;
99
use crate::session_diagnostics;
1010
use crate::session_diagnostics::UnsupportedLiteralReason;
1111

1212
pub(crate) struct DeprecationGroup;
1313

14-
fn get<'a>(
14+
fn get(
1515
cx: &AttributeAcceptContext<'_>,
1616
ident: Ident,
1717
param_span: Span,
18-
arg: impl ArgParser<'a>,
18+
arg: &ArgParser<'_>,
1919
item: &mut Option<Symbol>,
2020
) -> bool {
2121
if item.is_some() {
@@ -32,7 +32,7 @@ fn get<'a>(
3232
} else {
3333
let lit = v.value_as_lit();
3434
cx.dcx().emit_err(session_diagnostics::UnsupportedLiteral {
35-
span: v.value_span(),
35+
span: v.value_span,
3636
reason: UnsupportedLiteralReason::DeprecatedString,
3737
is_bytestr: lit.kind.is_bytestr(),
3838
start_point_span: cx.sess().source_map().start_point(lit.span),
@@ -52,7 +52,7 @@ fn get<'a>(
5252
impl SingleAttributeGroup for DeprecationGroup {
5353
const PATH: &'static [rustc_span::Symbol] = &[sym::deprecated];
5454

55-
fn on_duplicate(cx: &crate::context::AttributeAcceptContext<'_>, first_span: rustc_span::Span) {
55+
fn on_duplicate(cx: &AttributeAcceptContext<'_>, first_span: rustc_span::Span) {
5656
// FIXME(jdonszelmann): merge with errors from check_attrs.rs
5757
cx.dcx().emit_err(session_diagnostics::UnusedMultiple {
5858
this: cx.attr_span,
@@ -61,10 +61,7 @@ impl SingleAttributeGroup for DeprecationGroup {
6161
});
6262
}
6363

64-
fn convert(
65-
cx: &AttributeAcceptContext<'_>,
66-
args: &GenericArgParser<'_, rustc_ast::Expr>,
67-
) -> Option<AttributeKind> {
64+
fn convert(cx: &AttributeAcceptContext<'_>, args: &ArgParser<'_>) -> Option<AttributeKind> {
6865
let features = cx.features();
6966

7067
let mut since = None;

compiler/rustc_attr_parsing/src/attributes/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
1818
use std::marker::PhantomData;
1919

20-
use rustc_ast::Expr;
2120
use rustc_attr_data_structures::AttributeKind;
2221
use rustc_span::Span;
2322
use thin_vec::ThinVec;
2423

2524
use crate::context::{AttributeAcceptContext, AttributeGroupContext};
26-
use crate::parser::GenericArgParser;
25+
use crate::parser::ArgParser;
2726

2827
pub(crate) mod allow_unstable;
2928
pub(crate) mod cfg;
@@ -34,7 +33,7 @@ pub(crate) mod stability;
3433
pub(crate) mod transparency;
3534
pub(crate) mod util;
3635

37-
type AttributeHandler<T> = fn(&mut T, &AttributeAcceptContext<'_>, &GenericArgParser<'_, Expr>);
36+
type AttributeHandler<T> = fn(&mut T, &AttributeAcceptContext<'_>, &ArgParser<'_>);
3837
type AttributeMapping<T> = &'static [(&'static [rustc_span::Symbol], AttributeHandler<T>)];
3938

4039
/// An [`AttributeGroup`] is a type which searches for syntactic attributes.
@@ -75,10 +74,7 @@ pub(crate) trait SingleAttributeGroup: 'static {
7574

7675
/// The extractor has gotten a chance to accept the attributes on an item,
7776
/// now produce an attribute.
78-
fn convert(
79-
cx: &AttributeAcceptContext<'_>,
80-
args: &GenericArgParser<'_, Expr>,
81-
) -> Option<AttributeKind>;
77+
fn convert(cx: &AttributeAcceptContext<'_>, args: &ArgParser<'_>) -> Option<AttributeKind>;
8278
}
8379

8480
pub(crate) struct Single<T: SingleAttributeGroup>(PhantomData<T>, Option<(AttributeKind, Span)>);
@@ -125,7 +121,7 @@ pub(crate) trait CombineAttributeGroup: 'static {
125121
/// now produce an attribute.
126122
fn extend<'a>(
127123
cx: &'a AttributeAcceptContext<'a>,
128-
args: &'a GenericArgParser<'a, Expr>,
124+
args: &'a ArgParser<'a>,
129125
) -> impl IntoIterator<Item = Self::Item> + 'a;
130126
}
131127

compiler/rustc_attr_parsing/src/attributes/repr.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use rustc_span::{Span, Symbol, sym};
66

77
use super::{CombineAttributeGroup, ConvertFn};
88
use crate::context::AttributeAcceptContext;
9-
use crate::parser::{
10-
ArgParser, GenericArgParser, MetaItemListParser, MetaItemOrLitParser, MetaItemParser,
11-
NameValueParser,
12-
};
9+
use crate::parser::{ArgParser, MetaItemListParser, MetaItemOrLitParser, MetaItemParser};
1310
use crate::session_diagnostics;
1411
use crate::session_diagnostics::IncorrectReprFormatGenericCause;
1512

@@ -30,7 +27,7 @@ impl CombineAttributeGroup for ReprGroup {
3027

3128
fn extend<'a>(
3229
cx: &'a AttributeAcceptContext<'a>,
33-
args: &'a GenericArgParser<'a, rustc_ast::Expr>,
30+
args: &'a ArgParser<'a>,
3431
) -> impl IntoIterator<Item = Self::Item> + 'a {
3532
let mut reprs = Vec::new();
3633

@@ -49,7 +46,7 @@ impl CombineAttributeGroup for ReprGroup {
4946

5047
fn parse_repr<'a, 'b>(
5148
cx: &AttributeAcceptContext<'_>,
52-
param: &'a impl MetaItemParser<'b>,
49+
param: &'a MetaItemParser<'b>,
5350
) -> Option<ReprAttr> {
5451
// FIXME(jdonszelmann): invert the parsing here to match on the word first and then the
5552
// structure.
@@ -68,10 +65,10 @@ fn parse_repr<'a, 'b>(
6865
}
6966
}
7067

71-
fn parse_list_repr<'b>(
68+
fn parse_list_repr(
7269
cx: &AttributeAcceptContext<'_>,
7370
ident: Ident,
74-
list: MetaItemListParser<'b>,
71+
list: &MetaItemListParser<'_>,
7572
param_span: Span,
7673
) -> Option<ReprAttr> {
7774
if let Some(single) = list.single() {
@@ -156,7 +153,7 @@ fn reject_not_literal_list(
156153
fn reject_name_value_repr(
157154
cx: &AttributeAcceptContext<'_>,
158155
ident: Ident,
159-
value: MetaItemLit,
156+
value: &MetaItemLit,
160157
param_span: Span,
161158
) {
162159
match ident.name {
@@ -193,7 +190,7 @@ fn reject_name_value_repr(
193190
fn parse_singleton_list_repr(
194191
cx: &AttributeAcceptContext<'_>,
195192
ident: Ident,
196-
lit: MetaItemLit,
193+
lit: &MetaItemLit,
197194
param_span: Span,
198195
) -> Option<ReprAttr> {
199196
match ident.name {

compiler/rustc_attr_parsing/src/attributes/stability.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use std::num::NonZero;
22

3-
use rustc_attr_data_structures::{AllowedThroughUnstableModules, AttributeKind, DefaultBodyStability, PartialConstStability, Stability, StabilityLevel, StableSince, UnstableReason, VERSION_PLACEHOLDER};
3+
use rustc_attr_data_structures::{
4+
AllowedThroughUnstableModules, AttributeKind, DefaultBodyStability, PartialConstStability, Stability, StabilityLevel, StableSince, UnstableReason, VERSION_PLACEHOLDER
5+
};
46
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
57

68
use super::util::parse_version;
79
use super::{AttributeGroup, AttributeMapping, SingleAttributeGroup};
810
use crate::context::{AttributeAcceptContext, AttributeGroupContext};
9-
use crate::parser::{ArgParser, MetaItemParser, NameValueParser};
11+
use crate::parser::{ArgParser, MetaItemParser};
1012
use crate::session_diagnostics::{self, UnsupportedLiteralReason};
1113

1214
#[derive(Default)]
@@ -112,10 +114,7 @@ impl SingleAttributeGroup for ConstStabilityIndirectGroup {
112114
// ignore
113115
fn on_duplicate(_cx: &AttributeAcceptContext<'_>, _first_span: Span) {}
114116

115-
fn convert(
116-
_cx: &AttributeAcceptContext<'_>,
117-
_args: &super::GenericArgParser<'_, rustc_ast::Expr>,
118-
) -> Option<AttributeKind> {
117+
fn convert(_cx: &AttributeAcceptContext<'_>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
119118
Some(AttributeKind::ConstStabilityIndirect)
120119
}
121120
}
@@ -190,9 +189,9 @@ impl AttributeGroup for ConstStabilityGroup {
190189
///
191190
/// Emits an error when either the option was already Some, or the arguments weren't of form
192191
/// `name = value`
193-
fn insert_value_into_option_or_error<'a>(
192+
fn insert_value_into_option_or_error(
194193
cx: &AttributeAcceptContext<'_>,
195-
param: &impl MetaItemParser<'a>,
194+
param: &MetaItemParser<'_>,
196195
item: &mut Option<Symbol>,
197196
) -> Option<()> {
198197
if item.is_some() {
@@ -217,9 +216,9 @@ fn insert_value_into_option_or_error<'a>(
217216

218217
/// Read the content of a `stable`/`rustc_const_stable` attribute, and return the feature name and
219218
/// its stability information.
220-
pub(crate) fn parse_stability<'a>(
219+
pub(crate) fn parse_stability(
221220
cx: &AttributeAcceptContext<'_>,
222-
args: &'a impl ArgParser<'a>,
221+
args: &ArgParser<'_>,
223222
) -> Option<(Symbol, StabilityLevel)> {
224223
let mut feature = None;
225224
let mut since = None;
@@ -283,9 +282,9 @@ pub(crate) fn parse_stability<'a>(
283282

284283
// Read the content of a `unstable`/`rustc_const_unstable`/`rustc_default_body_unstable`
285284
/// attribute, and return the feature name and its stability information.
286-
pub(crate) fn parse_unstability<'a>(
285+
pub(crate) fn parse_unstability(
287286
cx: &AttributeAcceptContext<'_>,
288-
args: &'a impl ArgParser<'a>,
287+
args: &ArgParser<'_>,
289288
) -> Option<(Symbol, StabilityLevel)> {
290289
let mut feature = None;
291290
let mut reason = None;

compiler/rustc_attr_parsing/src/attributes/transparency.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_attr_data_structures::AttributeKind;
22
use rustc_span::hygiene::Transparency;
33
use rustc_span::sym;
44

5-
use super::SingleAttributeGroup;
6-
use crate::parser::{ArgParser, NameValueParser};
5+
use super::{AttributeAcceptContext, SingleAttributeGroup};
6+
use crate::parser::ArgParser;
77

88
pub(crate) struct TransparencyGroup;
99

@@ -17,10 +17,7 @@ impl SingleAttributeGroup for TransparencyGroup {
1717
cx.dcx().span_err(vec![first_span, cx.attr_span], "multiple macro transparency attributes");
1818
}
1919

20-
fn convert(
21-
cx: &crate::context::AttributeAcceptContext<'_>,
22-
args: &crate::parser::GenericArgParser<'_, rustc_ast::Expr>,
23-
) -> Option<AttributeKind> {
20+
fn convert(cx: &AttributeAcceptContext<'_>, args: &ArgParser<'_>) -> Option<AttributeKind> {
2421
match args.name_value().and_then(|nv| nv.value_as_str()) {
2522
Some(sym::transparent) => Some(Transparency::Transparent),
2623
Some(sym::semitransparent) => Some(Transparency::SemiTransparent),

compiler/rustc_attr_parsing/src/context.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use std::ops::Deref;
44
use std::sync::LazyLock;
55

66
use rustc_ast::{self as ast, DelimArgs};
7+
use rustc_attr_data_structures::AttributeKind;
78
use rustc_errors::DiagCtxtHandle;
89
use rustc_feature::Features;
910
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId};
10-
use rustc_attr_data_structures::AttributeKind;
1111
use rustc_session::Session;
1212
use rustc_span::symbol::kw;
1313
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
@@ -21,17 +21,17 @@ use crate::attributes::stability::{
2121
};
2222
use crate::attributes::transparency::TransparencyGroup;
2323
use crate::attributes::{AttributeGroup, Combine, Single};
24-
use crate::parser::{GenericArgParser, GenericMetaItemParser, MetaItemParser};
24+
use crate::parser::{ArgParser, MetaItemParser};
2525

2626
macro_rules! attribute_groups {
2727
(
2828
pub(crate) static $name: ident = [$($names: ty),* $(,)?];
2929
) => {
3030
pub(crate) static $name: LazyLock<(
31-
BTreeMap<&'static [Symbol], Vec<Box<dyn Fn(&AttributeAcceptContext<'_>, &GenericArgParser<'_, ast::Expr>) + Send + Sync>>>,
31+
BTreeMap<&'static [Symbol], Vec<Box<dyn Fn(&AttributeAcceptContext<'_>, &ArgParser<'_>) + Send + Sync>>>,
3232
Vec<Box<dyn Send + Sync + Fn(&AttributeGroupContext<'_>) -> Option<AttributeKind>>>
3333
)> = LazyLock::new(|| {
34-
let mut accepts = BTreeMap::<_, Vec<Box<dyn Fn(&AttributeAcceptContext<'_>, &GenericArgParser<'_, ast::Expr>) + Send + Sync>>>::new();
34+
let mut accepts = BTreeMap::<_, Vec<Box<dyn Fn(&AttributeAcceptContext<'_>, &ArgParser<'_>) + Send + Sync>>>::new();
3535
let mut finalizes = Vec::<Box<dyn Send + Sync + Fn(&AttributeGroupContext<'_>) -> Option<AttributeKind>>>::new();
3636

3737
$(
@@ -238,7 +238,7 @@ impl<'sess> AttributeParseContext<'sess> {
238238
// }))
239239
// }
240240
ast::AttrKind::Normal(n) => {
241-
let parser = GenericMetaItemParser::from_attr(&n, self.dcx());
241+
let parser = MetaItemParser::from_attr(n, self.dcx());
242242
let (path, args) = parser.deconstruct();
243243
let parts = path.segments().map(|i| i.name).collect::<Vec<_>>();
244244

0 commit comments

Comments
 (0)