Skip to content

Commit

Permalink
Rollup merge of rust-lang#61654 - Electron-libre:use_slice_patterns_i…
Browse files Browse the repository at this point in the history
…n_rustc, r=oli-obk,Centril

use pattern matching for slices destructuring

refs rust-lang#61542

Use slices pattern where it seems to make sense .
  • Loading branch information
Centril committed Jun 12, 2019
2 parents 6933034 + 0a4504d commit 9f22708
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 67 deletions.
33 changes: 16 additions & 17 deletions src/libsyntax/diagnostics/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ 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!()
};

Expand Down Expand Up @@ -66,22 +68,19 @@ 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) => {
(code, None)
let (code, description) = match token_tree {
[
TokenTree::Token(Token { kind: token::Ident(code, _), .. })
] => {
(*code, None)
},
[
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))
},
(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, .. }), ..
}))) => {
(code, Some(symbol))
}
_ => unreachable!()
};

Expand Down
64 changes: 27 additions & 37 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,48 +424,38 @@ 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)),
)
if name_macro_rules == sym::macro_rules && name_zip.as_str() == "zip" => {
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, macro_tts)
]
if name_macro_rules == &sym::macro_rules && name_zip.as_str() == "zip" => {
let tts = &macro_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)),
)
if macro_delim == token::Paren => {
match &tts[..] {
[
TokenTree::Delimited(_, first_delim, first_tts),
TokenTree::Token(Token { kind: token::FatArrow, .. }),
TokenTree::Delimited(_, second_delim, 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 {
kind: token::Ident(name, false), ..
})),
)
if first_delim == token::Paren && name.as_str() == "a" => {},
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 {
kind: token::Ident(name, false), ..
})),
)
if second_delim == token::Paren && name.as_str() == "a" => {},
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),
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`"),
};

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/deriving/cmp/partial_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`"),
};

Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax_ext/deriving/cmp/partial_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`")
Expand Down Expand Up @@ -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)`"),
};

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/deriving/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`")
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/trace_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`"),
Expand Down

0 comments on commit 9f22708

Please sign in to comment.