Skip to content

Commit 6f9f17f

Browse files
Rollup merge of rust-lang#133746 - oli-obk:push-xwyrylxmrtvq, r=jieyouxu
Change `AttrArgs::Eq` to a struct variant Cleanups for simplifying rust-lang#131808 Basically changes `AttrArgs::Eq` to a struct variant and then avoids several matches on `AttrArgsEq` in favor of methods on it. This will make future refactorings simpler, as they can either keep methods or switch to field accesses without having to restructure code
2 parents 3586e4a + da182b6 commit 6f9f17f

File tree

16 files changed

+68
-52
lines changed

16 files changed

+68
-52
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+36-15
Original file line numberDiff line numberDiff line change
@@ -1731,12 +1731,12 @@ pub enum AttrArgs {
17311731
/// Delimited arguments: `#[attr()/[]/{}]`.
17321732
Delimited(DelimArgs),
17331733
/// Arguments of a key-value attribute: `#[attr = "value"]`.
1734-
Eq(
1734+
Eq {
17351735
/// Span of the `=` token.
1736-
Span,
1737-
/// The "value".
1738-
AttrArgsEq,
1739-
),
1736+
eq_span: Span,
1737+
1738+
value: AttrArgsEq,
1739+
},
17401740
}
17411741

17421742
// The RHS of an `AttrArgs::Eq` starts out as an expression. Once macro
@@ -1748,15 +1748,39 @@ pub enum AttrArgsEq {
17481748
Hir(MetaItemLit),
17491749
}
17501750

1751+
impl AttrArgsEq {
1752+
pub fn span(&self) -> Span {
1753+
match self {
1754+
AttrArgsEq::Ast(p) => p.span,
1755+
AttrArgsEq::Hir(lit) => lit.span,
1756+
}
1757+
}
1758+
1759+
pub fn unwrap_ast(&self) -> &Expr {
1760+
match self {
1761+
AttrArgsEq::Ast(p) => p,
1762+
AttrArgsEq::Hir(lit) => {
1763+
unreachable!("in literal form when getting inner tokens: {lit:?}")
1764+
}
1765+
}
1766+
}
1767+
1768+
pub fn unwrap_ast_mut(&mut self) -> &mut P<Expr> {
1769+
match self {
1770+
AttrArgsEq::Ast(p) => p,
1771+
AttrArgsEq::Hir(lit) => {
1772+
unreachable!("in literal form when getting inner tokens: {lit:?}")
1773+
}
1774+
}
1775+
}
1776+
}
1777+
17511778
impl AttrArgs {
17521779
pub fn span(&self) -> Option<Span> {
17531780
match self {
17541781
AttrArgs::Empty => None,
17551782
AttrArgs::Delimited(args) => Some(args.dspan.entire()),
1756-
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => Some(eq_span.to(expr.span)),
1757-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
1758-
unreachable!("in literal form when getting span: {:?}", lit);
1759-
}
1783+
AttrArgs::Eq { eq_span, value } => Some(eq_span.to(value.span())),
17601784
}
17611785
}
17621786

@@ -1766,10 +1790,7 @@ impl AttrArgs {
17661790
match self {
17671791
AttrArgs::Empty => TokenStream::default(),
17681792
AttrArgs::Delimited(args) => args.tokens.clone(),
1769-
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => TokenStream::from_ast(expr),
1770-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
1771-
unreachable!("in literal form when getting inner tokens: {:?}", lit)
1772-
}
1793+
AttrArgs::Eq { value, .. } => TokenStream::from_ast(value.unwrap_ast()),
17731794
}
17741795
}
17751796
}
@@ -1783,10 +1804,10 @@ where
17831804
match self {
17841805
AttrArgs::Empty => {}
17851806
AttrArgs::Delimited(args) => args.hash_stable(ctx, hasher),
1786-
AttrArgs::Eq(_eq_span, AttrArgsEq::Ast(expr)) => {
1807+
AttrArgs::Eq { value: AttrArgsEq::Ast(expr), .. } => {
17871808
unreachable!("hash_stable {:?}", expr);
17881809
}
1789-
AttrArgs::Eq(eq_span, AttrArgsEq::Hir(lit)) => {
1810+
AttrArgs::Eq { eq_span, value: AttrArgsEq::Hir(lit) } => {
17901811
eq_span.hash_stable(ctx, hasher);
17911812
lit.hash_stable(ctx, hasher);
17921813
}

Diff for: compiler/rustc_ast/src/attr/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl AttrItem {
250250
AttrArgs::Delimited(args) if args.delim == Delimiter::Parenthesis => {
251251
MetaItemKind::list_from_tokens(args.tokens.clone())
252252
}
253-
AttrArgs::Delimited(_) | AttrArgs::Eq(..) | AttrArgs::Empty => None,
253+
AttrArgs::Delimited(_) | AttrArgs::Eq { .. } | AttrArgs::Empty => None,
254254
}
255255
}
256256

@@ -268,7 +268,7 @@ impl AttrItem {
268268
/// ```
269269
fn value_str(&self) -> Option<Symbol> {
270270
match &self.args {
271-
AttrArgs::Eq(_, args) => args.value_str(),
271+
AttrArgs::Eq { value, .. } => value.value_str(),
272272
AttrArgs::Delimited(_) | AttrArgs::Empty => None,
273273
}
274274
}
@@ -492,7 +492,7 @@ impl MetaItemKind {
492492
MetaItemKind::list_from_tokens(tokens.clone()).map(MetaItemKind::List)
493493
}
494494
AttrArgs::Delimited(..) => None,
495-
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
495+
AttrArgs::Eq { value: AttrArgsEq::Ast(expr), .. } => match expr.kind {
496496
ExprKind::Lit(token_lit) => {
497497
// Turn failures to `None`, we'll get parse errors elsewhere.
498498
MetaItemLit::from_token_lit(token_lit, expr.span)
@@ -501,7 +501,9 @@ impl MetaItemKind {
501501
}
502502
_ => None,
503503
},
504-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())),
504+
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
505+
Some(MetaItemKind::NameValue(lit.clone()))
506+
}
505507
}
506508
}
507509
}
@@ -702,7 +704,7 @@ pub fn mk_attr_name_value_str(
702704
tokens: None,
703705
});
704706
let path = Path::from_ident(Ident::new(name, span));
705-
let args = AttrArgs::Eq(span, AttrArgsEq::Ast(expr));
707+
let args = AttrArgs::Eq { eq_span: span, value: AttrArgsEq::Ast(expr) };
706708
mk_attr(g, style, unsafety, path, args, span)
707709
}
708710

Diff for: compiler/rustc_ast/src/mut_visit.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,10 @@ fn visit_attr_args<T: MutVisitor>(vis: &mut T, args: &mut AttrArgs) {
451451
match args {
452452
AttrArgs::Empty => {}
453453
AttrArgs::Delimited(args) => visit_delim_args(vis, args),
454-
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
455-
vis.visit_expr(expr);
454+
AttrArgs::Eq { eq_span, value } => {
455+
vis.visit_expr(value.unwrap_ast_mut());
456456
vis.visit_span(eq_span);
457457
}
458-
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(lit)) => {
459-
unreachable!("in literal form when visiting mac args eq: {:?}", lit)
460-
}
461458
}
462459
}
463460

Diff for: compiler/rustc_ast/src/visit.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1273,10 +1273,7 @@ pub fn walk_attr_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a AttrArgs) -
12731273
match args {
12741274
AttrArgs::Empty => {}
12751275
AttrArgs::Delimited(_args) => {}
1276-
AttrArgs::Eq(_eq_span, AttrArgsEq::Ast(expr)) => try_visit!(visitor.visit_expr(expr)),
1277-
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(lit)) => {
1278-
unreachable!("in literal form when walking mac args eq: {:?}", lit)
1279-
}
1276+
AttrArgs::Eq { value, .. } => try_visit!(visitor.visit_expr(value.unwrap_ast())),
12801277
}
12811278
V::Result::output()
12821279
}

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
889889
// This is an inert key-value attribute - it will never be visible to macros
890890
// after it gets lowered to HIR. Therefore, we can extract literals to handle
891891
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
892-
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
892+
&AttrArgs::Eq { eq_span, ref value } => {
893+
let expr = value.unwrap_ast();
893894
// In valid code the value always ends up as a single literal. Otherwise, a dummy
894895
// literal suffices because the error is handled elsewhere.
895896
let lit = if let ExprKind::Lit(token_lit) = expr.kind
@@ -905,10 +906,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
905906
span: DUMMY_SP,
906907
}
907908
};
908-
AttrArgs::Eq(*eq_span, AttrArgsEq::Hir(lit))
909-
}
910-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
911-
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
909+
AttrArgs::Eq { eq_span, value: AttrArgsEq::Hir(lit) }
912910
}
913911
}
914912
}

Diff for: compiler/rustc_ast_pretty/src/pprust/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
648648
AttrArgs::Empty => {
649649
self.print_path(&item.path, false, 0);
650650
}
651-
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => {
651+
AttrArgs::Eq { value: AttrArgsEq::Ast(expr), .. } => {
652652
self.print_path(&item.path, false, 0);
653653
self.space();
654654
self.word_space("=");
655655
let token_str = self.expr_to_string(expr);
656656
self.word(token_str);
657657
}
658-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
658+
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
659659
self.print_path(&item.path, false, 0);
660660
self.space();
661661
self.word_space("=");

Diff for: compiler/rustc_expand/src/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
732732
_ => item.to_tokens(),
733733
};
734734
let attr_item = attr.unwrap_normal_item();
735-
if let AttrArgs::Eq(..) = attr_item.args {
735+
if let AttrArgs::Eq { .. } = attr_item.args {
736736
self.cx.dcx().emit_err(UnsupportedKeyValue { span });
737737
}
738738
let inner_tokens = attr_item.args.inner_tokens();

Diff for: compiler/rustc_parse/src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ impl<'a> Parser<'a> {
13761376
AttrArgs::Delimited(args)
13771377
} else if self.eat(&token::Eq) {
13781378
let eq_span = self.prev_token.span;
1379-
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(self.parse_expr_force_collect()?))
1379+
AttrArgs::Eq { eq_span, value: AttrArgsEq::Ast(self.parse_expr_force_collect()?) }
13801380
} else {
13811381
AttrArgs::Empty
13821382
})

Diff for: compiler/rustc_parse/src/validate_attr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
4343
}
4444
}
4545
_ => {
46-
if let AttrArgs::Eq(..) = attr_item.args {
46+
if let AttrArgs::Eq { .. } = attr_item.args {
4747
// All key-value attributes are restricted to meta-item syntax.
4848
match parse_meta(psess, attr) {
4949
Ok(_) => {}
@@ -70,7 +70,7 @@ pub fn parse_meta<'a>(psess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Met
7070
parse_in(psess, tokens.clone(), "meta list", |p| p.parse_meta_seq_top())?;
7171
MetaItemKind::List(nmis)
7272
}
73-
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => {
73+
AttrArgs::Eq { value: AttrArgsEq::Ast(expr), .. } => {
7474
if let ast::ExprKind::Lit(token_lit) = expr.kind {
7575
let res = ast::MetaItemLit::from_token_lit(token_lit, expr.span);
7676
let res = match res {
@@ -116,7 +116,9 @@ pub fn parse_meta<'a>(psess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Met
116116
return Err(err);
117117
}
118118
}
119-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => MetaItemKind::NameValue(lit.clone()),
119+
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
120+
MetaItemKind::NameValue(lit.clone())
121+
}
120122
},
121123
})
122124
}

Diff for: compiler/rustc_resolve/src/rustdoc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ pub fn attrs_to_doc_fragments<'a>(
220220

221221
fn span_for_value(attr: &ast::Attribute) -> Span {
222222
if let ast::AttrKind::Normal(normal) = &attr.kind
223-
&& let ast::AttrArgs::Eq(_, ast::AttrArgsEq::Hir(meta)) = &normal.item.args
223+
&& let ast::AttrArgs::Eq { value, .. } = &normal.item.args
224224
{
225-
meta.span.with_ctxt(attr.span.ctxt())
225+
value.span().with_ctxt(attr.span.ctxt())
226226
} else {
227227
attr.span
228228
}

Diff for: compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::iter;
22
use std::path::PathBuf;
33

4-
use rustc_ast::{AttrArgs, AttrArgsEq, AttrKind, Attribute, MetaItemInner};
4+
use rustc_ast::{AttrArgs, AttrKind, Attribute, MetaItemInner};
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_errors::codes::*;
77
use rustc_errors::{ErrorGuaranteed, struct_span_code_err};
@@ -639,8 +639,7 @@ impl<'tcx> OnUnimplementedDirective {
639639
let report_span = match &item.args {
640640
AttrArgs::Empty => item.path.span,
641641
AttrArgs::Delimited(args) => args.dspan.entire(),
642-
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => eq_span.to(expr.span),
643-
AttrArgs::Eq(span, AttrArgsEq::Hir(expr)) => span.to(expr.span),
642+
AttrArgs::Eq { eq_span, value } => eq_span.to(value.span()),
644643
};
645644

646645
if let Some(item_def_id) = item_def_id.as_local() {

Diff for: src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2647,7 +2647,7 @@ fn filter_doc_attr(normal: &mut ast::NormalAttr, is_inline: bool) {
26472647
});
26482648
args.tokens = TokenStream::new(tokens);
26492649
}
2650-
ast::AttrArgs::Empty | ast::AttrArgs::Eq(..) => {}
2650+
ast::AttrArgs::Empty | ast::AttrArgs::Eq { .. } => {}
26512651
}
26522652
}
26532653

Diff for: src/tools/clippy/clippy_lints/src/attrs/should_panic_without_expect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_span::sym;
99

1010
pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute) {
1111
if let AttrKind::Normal(normal_attr) = &attr.kind {
12-
if let AttrArgs::Eq(_, AttrArgsEq::Ast(_)) = &normal_attr.item.args {
12+
if let AttrArgs::Eq { value: AttrArgsEq::Ast(_), .. } = &normal_attr.item.args {
1313
// `#[should_panic = ".."]` found, good
1414
return;
1515
}

Diff for: src/tools/clippy/clippy_lints/src/doc/include_in_doc_without_cfg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn check(cx: &LateContext<'_>, attrs: &[Attribute]) {
1212
if !attr.span.from_expansion()
1313
&& let AttrKind::Normal(ref normal) = attr.kind
1414
&& normal.item.path == sym::doc
15-
&& let AttrArgs::Eq(_, AttrArgsEq::Hir(ref meta)) = normal.item.args
15+
&& let AttrArgs::Eq { value: AttrArgsEq::Hir(ref meta), .. } = normal.item.args
1616
&& !attr.span.contains(meta.span)
1717
// Since the `include_str` is already expanded at this point, we can only take the
1818
// whole attribute snippet and then modify for our suggestion.

Diff for: src/tools/clippy/clippy_lints/src/large_include_file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl LateLintPass<'_> for LargeIncludeFile {
9696
&& let AttrKind::Normal(ref normal) = attr.kind
9797
&& let Some(doc) = attr.doc_str()
9898
&& doc.as_str().len() as u64 > self.max_file_size
99-
&& let AttrArgs::Eq(_, AttrArgsEq::Hir(ref meta)) = normal.item.args
99+
&& let AttrArgs::Eq { value: AttrArgsEq::Hir(ref meta), .. } = normal.item.args
100100
&& !attr.span.contains(meta.span)
101101
// Since the `include_str` is already expanded at this point, we can only take the
102102
// whole attribute snippet and then modify for our suggestion.

Diff for: src/tools/clippy/clippy_utils/src/ast_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,8 @@ pub fn eq_attr_args(l: &AttrArgs, r: &AttrArgs) -> bool {
872872
match (l, r) {
873873
(Empty, Empty) => true,
874874
(Delimited(la), Delimited(ra)) => eq_delim_args(la, ra),
875-
(Eq(_, AttrArgsEq::Ast(le)), Eq(_, AttrArgsEq::Ast(re))) => eq_expr(le, re),
876-
(Eq(_, AttrArgsEq::Hir(ll)), Eq(_, AttrArgsEq::Hir(rl))) => ll.kind == rl.kind,
875+
(Eq { value: AttrArgsEq::Ast(le), .. }, Eq{ value: AttrArgsEq::Ast(re), .. }) => eq_expr(le, re),
876+
(Eq { value: AttrArgsEq::Hir(ll), .. }, Eq{ value: AttrArgsEq::Hir(rl), .. }) => ll.kind == rl.kind,
877877
_ => false,
878878
}
879879
}

0 commit comments

Comments
 (0)