Skip to content

Commit da182b6

Browse files
committed
Deduplicate some matches that always panic in one arm
1 parent c0b5322 commit da182b6

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,24 @@ impl AttrArgsEq {
17571757
AttrArgsEq::Hir(lit) => lit.span,
17581758
}
17591759
}
1760+
1761+
pub fn unwrap_ast(&self) -> &Expr {
1762+
match self {
1763+
AttrArgsEq::Ast(p) => p,
1764+
AttrArgsEq::Hir(lit) => {
1765+
unreachable!("in literal form when getting inner tokens: {lit:?}")
1766+
}
1767+
}
1768+
}
1769+
1770+
pub fn unwrap_ast_mut(&mut self) -> &mut P<Expr> {
1771+
match self {
1772+
AttrArgsEq::Ast(p) => p,
1773+
AttrArgsEq::Hir(lit) => {
1774+
unreachable!("in literal form when getting inner tokens: {lit:?}")
1775+
}
1776+
}
1777+
}
17601778
}
17611779

17621780
impl AttrArgs {
@@ -1774,10 +1792,7 @@ impl AttrArgs {
17741792
match self {
17751793
AttrArgs::Empty => TokenStream::default(),
17761794
AttrArgs::Delimited(args) => args.tokens.clone(),
1777-
AttrArgs::Eq { value: AttrArgsEq::Ast(expr), .. } => TokenStream::from_ast(expr),
1778-
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
1779-
unreachable!("in literal form when getting inner tokens: {:?}", lit)
1780-
}
1795+
AttrArgs::Eq { value, .. } => TokenStream::from_ast(value.unwrap_ast()),
17811796
}
17821797
}
17831798
}

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, value: 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 { value: 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 { value: AttrArgsEq::Ast(expr), .. } => try_visit!(visitor.visit_expr(expr)),
1277-
AttrArgs::Eq { value: 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

+2-4
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, value: AttrArgsEq::Ast(ref 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
@@ -907,9 +908,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
907908
};
908909
AttrArgs::Eq { eq_span, value: AttrArgsEq::Hir(lit) }
909910
}
910-
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
911-
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
912-
}
913911
}
914912
}
915913

0 commit comments

Comments
 (0)