1
Fork 0

Deduplicate some matches that always panic in one arm

This commit is contained in:
Oli Scherer 2024-12-02 10:56:36 +00:00
parent c0b532277b
commit da182b6d95
4 changed files with 24 additions and 17 deletions

View file

@ -1757,6 +1757,24 @@ impl AttrArgsEq {
AttrArgsEq::Hir(lit) => lit.span, AttrArgsEq::Hir(lit) => lit.span,
} }
} }
pub fn unwrap_ast(&self) -> &Expr {
match self {
AttrArgsEq::Ast(p) => p,
AttrArgsEq::Hir(lit) => {
unreachable!("in literal form when getting inner tokens: {lit:?}")
}
}
}
pub fn unwrap_ast_mut(&mut self) -> &mut P<Expr> {
match self {
AttrArgsEq::Ast(p) => p,
AttrArgsEq::Hir(lit) => {
unreachable!("in literal form when getting inner tokens: {lit:?}")
}
}
}
} }
impl AttrArgs { impl AttrArgs {
@ -1774,10 +1792,7 @@ impl AttrArgs {
match self { match self {
AttrArgs::Empty => TokenStream::default(), AttrArgs::Empty => TokenStream::default(),
AttrArgs::Delimited(args) => args.tokens.clone(), AttrArgs::Delimited(args) => args.tokens.clone(),
AttrArgs::Eq { value: AttrArgsEq::Ast(expr), .. } => TokenStream::from_ast(expr), AttrArgs::Eq { value, .. } => TokenStream::from_ast(value.unwrap_ast()),
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
unreachable!("in literal form when getting inner tokens: {:?}", lit)
}
} }
} }
} }

View file

@ -451,13 +451,10 @@ fn visit_attr_args<T: MutVisitor>(vis: &mut T, args: &mut AttrArgs) {
match args { match args {
AttrArgs::Empty => {} AttrArgs::Empty => {}
AttrArgs::Delimited(args) => visit_delim_args(vis, args), AttrArgs::Delimited(args) => visit_delim_args(vis, args),
AttrArgs::Eq { eq_span, value: AttrArgsEq::Ast(expr) } => { AttrArgs::Eq { eq_span, value } => {
vis.visit_expr(expr); vis.visit_expr(value.unwrap_ast_mut());
vis.visit_span(eq_span); vis.visit_span(eq_span);
} }
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
unreachable!("in literal form when visiting mac args eq: {:?}", lit)
}
} }
} }

View file

@ -1273,10 +1273,7 @@ pub fn walk_attr_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a AttrArgs) -
match args { match args {
AttrArgs::Empty => {} AttrArgs::Empty => {}
AttrArgs::Delimited(_args) => {} AttrArgs::Delimited(_args) => {}
AttrArgs::Eq { value: AttrArgsEq::Ast(expr), .. } => try_visit!(visitor.visit_expr(expr)), AttrArgs::Eq { value, .. } => try_visit!(visitor.visit_expr(value.unwrap_ast())),
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
unreachable!("in literal form when walking mac args eq: {:?}", lit)
}
} }
V::Result::output() V::Result::output()
} }

View file

@ -889,7 +889,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// This is an inert key-value attribute - it will never be visible to macros // This is an inert key-value attribute - it will never be visible to macros
// after it gets lowered to HIR. Therefore, we can extract literals to handle // after it gets lowered to HIR. Therefore, we can extract literals to handle
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`). // nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
&AttrArgs::Eq { eq_span, value: AttrArgsEq::Ast(ref expr) } => { &AttrArgs::Eq { eq_span, ref value } => {
let expr = value.unwrap_ast();
// In valid code the value always ends up as a single literal. Otherwise, a dummy // In valid code the value always ends up as a single literal. Otherwise, a dummy
// literal suffices because the error is handled elsewhere. // literal suffices because the error is handled elsewhere.
let lit = if let ExprKind::Lit(token_lit) = expr.kind let lit = if let ExprKind::Lit(token_lit) = expr.kind
@ -907,9 +908,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}; };
AttrArgs::Eq { eq_span, value: AttrArgsEq::Hir(lit) } AttrArgs::Eq { eq_span, value: AttrArgsEq::Hir(lit) }
} }
AttrArgs::Eq { value: AttrArgsEq::Hir(lit), .. } => {
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
}
} }
} }