use pattern matching for slices destructuring
This commit is contained in:
@@ -33,8 +33,8 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt<'_>,
|
||||
span: Span,
|
||||
token_tree: &[TokenTree])
|
||||
-> Box<dyn MacResult+'cx> {
|
||||
let code = match (token_tree.len(), token_tree.get(0)) {
|
||||
(1, Some(&TokenTree::Token(Token { kind: token::Ident(code, _), .. }))) => code,
|
||||
let code = match token_tree {
|
||||
&[TokenTree::Token(Token { kind: token::Ident(code, _), .. })] => code,
|
||||
_ => unreachable!()
|
||||
};
|
||||
|
||||
@@ -66,22 +66,15 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>,
|
||||
span: Span,
|
||||
token_tree: &[TokenTree])
|
||||
-> Box<dyn MacResult+'cx> {
|
||||
let (code, description) = match (
|
||||
token_tree.len(),
|
||||
token_tree.get(0),
|
||||
token_tree.get(1),
|
||||
token_tree.get(2)
|
||||
) {
|
||||
(1, Some(&TokenTree::Token(Token { kind: token::Ident(code, _), .. })), None, None) => {
|
||||
let (code, description) = match token_tree {
|
||||
&[TokenTree::Token(Token { kind: token::Ident(code, _), .. })] => {
|
||||
(code, None)
|
||||
},
|
||||
(3, Some(&TokenTree::Token(Token { kind: token::Ident(code, _), .. })),
|
||||
Some(&TokenTree::Token(Token { kind: token::Comma, .. })),
|
||||
Some(&TokenTree::Token(Token {
|
||||
kind: token::Literal(token::Lit { symbol, .. }), ..
|
||||
}))) => {
|
||||
&[TokenTree::Token(Token { kind: token::Ident(code, _), .. }),
|
||||
TokenTree::Token(Token { kind: token::Comma, .. }),
|
||||
TokenTree::Token(Token { kind: token::Literal(token::Lit { symbol, .. }), ..})] => {
|
||||
(code, Some(symbol))
|
||||
}
|
||||
},
|
||||
_ => unreachable!()
|
||||
};
|
||||
|
||||
|
||||
@@ -424,47 +424,40 @@ mod tests {
|
||||
string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect();
|
||||
let tts: &[TokenTree] = &tts[..];
|
||||
|
||||
match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) {
|
||||
(
|
||||
4,
|
||||
Some(&TokenTree::Token(Token {
|
||||
kind: token::Ident(name_macro_rules, false), ..
|
||||
})),
|
||||
Some(&TokenTree::Token(Token { kind: token::Not, .. })),
|
||||
Some(&TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. })),
|
||||
Some(&TokenTree::Delimited(_, macro_delim, ref macro_tts)),
|
||||
)
|
||||
match tts {
|
||||
&[TokenTree::Token(Token {kind: token::Ident(name_macro_rules, false), ..}),
|
||||
TokenTree::Token(Token { kind: token::Not, .. }),
|
||||
TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. }),
|
||||
TokenTree::Delimited(_, macro_delim, ref macro_tts)
|
||||
]
|
||||
if name_macro_rules == sym::macro_rules && name_zip.as_str() == "zip" => {
|
||||
let tts = ¯o_tts.trees().collect::<Vec<_>>();
|
||||
match (tts.len(), tts.get(0), tts.get(1), tts.get(2)) {
|
||||
(
|
||||
3,
|
||||
Some(&TokenTree::Delimited(_, first_delim, ref first_tts)),
|
||||
Some(&TokenTree::Token(Token { kind: token::FatArrow, .. })),
|
||||
Some(&TokenTree::Delimited(_, second_delim, ref second_tts)),
|
||||
)
|
||||
match tts {
|
||||
&[
|
||||
TokenTree::Delimited(_, first_delim, ref first_tts),
|
||||
TokenTree::Token(Token { kind: token::FatArrow, .. }),
|
||||
TokenTree::Delimited(_, second_delim, ref second_tts),
|
||||
]
|
||||
if macro_delim == token::Paren => {
|
||||
let tts = &first_tts.trees().collect::<Vec<_>>();
|
||||
match (tts.len(), tts.get(0), tts.get(1)) {
|
||||
(
|
||||
2,
|
||||
Some(&TokenTree::Token(Token { kind: token::Dollar, .. })),
|
||||
Some(&TokenTree::Token(Token {
|
||||
match tts {
|
||||
&[
|
||||
TokenTree::Token(Token { kind: token::Dollar, .. }),
|
||||
TokenTree::Token(Token {
|
||||
kind: token::Ident(name, false), ..
|
||||
})),
|
||||
)
|
||||
}),
|
||||
]
|
||||
if first_delim == token::Paren && name.as_str() == "a" => {},
|
||||
_ => panic!("value 3: {:?} {:?}", first_delim, first_tts),
|
||||
}
|
||||
let tts = &second_tts.trees().collect::<Vec<_>>();
|
||||
match (tts.len(), tts.get(0), tts.get(1)) {
|
||||
(
|
||||
2,
|
||||
Some(&TokenTree::Token(Token { kind: token::Dollar, .. })),
|
||||
Some(&TokenTree::Token(Token {
|
||||
match tts {
|
||||
&[
|
||||
TokenTree::Token(Token { kind: token::Dollar, .. }),
|
||||
TokenTree::Token(Token {
|
||||
kind: token::Ident(name, false), ..
|
||||
})),
|
||||
)
|
||||
}),
|
||||
]
|
||||
if second_delim == token::Paren && name.as_str() == "a" => {},
|
||||
_ => panic!("value 4: {:?} {:?}", second_delim, second_tts),
|
||||
}
|
||||
|
||||
@@ -82,8 +82,8 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<
|
||||
// }
|
||||
|
||||
let new = {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
let other_f = match other_fs {
|
||||
[o_f] => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"),
|
||||
};
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt<'_>,
|
||||
-> P<Expr>
|
||||
{
|
||||
let op = |cx: &mut ExtCtxt<'_>, span: Span, self_f: P<Expr>, other_fs: &[P<Expr>]| {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
let other_f = match other_fs {
|
||||
[o_f] => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
|
||||
};
|
||||
|
||||
|
||||
@@ -143,8 +143,8 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
|
||||
// }
|
||||
|
||||
let new = {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
let other_f = match other_fs {
|
||||
[o_f] => o_f,
|
||||
_ => {
|
||||
cx.span_bug(span,
|
||||
"not exactly 2 arguments in `derive(PartialOrd)`")
|
||||
@@ -193,8 +193,8 @@ fn cs_op(less: bool,
|
||||
};
|
||||
|
||||
let par_cmp = |cx: &mut ExtCtxt<'_>, span, self_f: P<Expr>, other_fs: &[P<Expr>], default| {
|
||||
let other_f = match (other_fs.len(), other_fs.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
let other_f = match other_fs {
|
||||
[o_f] => o_f,
|
||||
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
|
||||
};
|
||||
|
||||
|
||||
@@ -52,8 +52,8 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt<'_>,
|
||||
}
|
||||
|
||||
fn hash_substructure(cx: &mut ExtCtxt<'_>, trait_span: Span, substr: &Substructure<'_>) -> P<Expr> {
|
||||
let state_expr = match (substr.nonself_args.len(), substr.nonself_args.get(0)) {
|
||||
(1, Some(o_f)) => o_f,
|
||||
let state_expr = match &substr.nonself_args {
|
||||
&[o_f] => o_f,
|
||||
_ => {
|
||||
cx.span_bug(trait_span,
|
||||
"incorrect number of arguments in `derive(Hash)`")
|
||||
|
||||
@@ -16,11 +16,11 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt<'_>,
|
||||
feature_gate::EXPLAIN_TRACE_MACROS);
|
||||
}
|
||||
|
||||
match (tt.len(), tt.first()) {
|
||||
(1, Some(TokenTree::Token(token))) if token.is_keyword(kw::True) => {
|
||||
match tt {
|
||||
[TokenTree::Token(token)] if token.is_keyword(kw::True) => {
|
||||
cx.set_trace_macros(true);
|
||||
}
|
||||
(1, Some(TokenTree::Token(token))) if token.is_keyword(kw::False) => {
|
||||
[TokenTree::Token(token)] if token.is_keyword(kw::False) => {
|
||||
cx.set_trace_macros(false);
|
||||
}
|
||||
_ => cx.span_err(sp, "trace_macros! accepts only `true` or `false`"),
|
||||
|
||||
Reference in New Issue
Block a user