diff options
| author | bors <bors@rust-lang.org> | 2019-09-06 07:37:41 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-09-06 07:37:41 +0000 |
| commit | 1fb3c4ec7ca37d33bd1e68cce669d171c2752615 (patch) | |
| tree | 48ea5150538790309c68b9f515c4e958724a82a5 /src/libsyntax | |
| parent | 6b5f9b2e973e438fc1726a2d164d046acd80b170 (diff) | |
| parent | 61fcd057d2524175a21e2eb1d49c1d4aec29a0be (diff) | |
| download | rust-1fb3c4ec7ca37d33bd1e68cce669d171c2752615.tar.gz rust-1fb3c4ec7ca37d33bd1e68cce669d171c2752615.zip | |
Auto merge of #64209 - Centril:rollup-x9kvjb7, r=Centril
Rollup of 10 pull requests Successful merges: - #63676 (Use wasi crate for Core API) - #64094 (Improve searching in rustdoc and add tests) - #64111 (or-patterns: Uniformly use `PatKind::Or` in AST & Fix/Cleanup resolve) - #64156 (Assume non-git LLVM is fresh if the stamp file exists) - #64161 (Point at variant on pattern field count mismatch) - #64174 (Add missing code examples on Iterator trait) - #64175 (Fix invalid span generation when it should be div) - #64186 (std: Improve downstream codegen in `Command::env`) - #64190 (fill metadata in rustc_lexer's Cargo.toml) - #64198 (Add Fuchsia to actually_monotonic) Failed merges: r? @ghost
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 29 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/mut_visit.rs | 13 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser/expr.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser/pat.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 21 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 17 |
7 files changed, 43 insertions, 65 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 6be00bcef45..c93e6d11ce7 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -561,29 +561,31 @@ impl Pat { })) } - pub fn walk<F>(&self, it: &mut F) -> bool - where - F: FnMut(&Pat) -> bool, - { + /// Walk top-down and call `it` in each place where a pattern occurs + /// starting with the root pattern `walk` is called on. If `it` returns + /// false then we will descend no further but siblings will be processed. + pub fn walk(&self, it: &mut impl FnMut(&Pat) -> bool) { if !it(self) { - return false; + return; } match &self.node { PatKind::Ident(_, _, Some(p)) => p.walk(it), - PatKind::Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk(it)), + PatKind::Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk(it)), PatKind::TupleStruct(_, s) | PatKind::Tuple(s) | PatKind::Slice(s) - | PatKind::Or(s) => s.iter().all(|p| p.walk(it)), - PatKind::Box(s) | PatKind::Ref(s, _) | PatKind::Paren(s) => s.walk(it), + | PatKind::Or(s) => s.iter().for_each(|p| p.walk(it)), + PatKind::Box(s) + | PatKind::Ref(s, _) + | PatKind::Paren(s) => s.walk(it), PatKind::Wild | PatKind::Rest | PatKind::Lit(_) | PatKind::Range(..) | PatKind::Ident(..) | PatKind::Path(..) - | PatKind::Mac(_) => true, + | PatKind::Mac(_) => {}, } } @@ -928,7 +930,7 @@ pub struct Local { #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct Arm { pub attrs: Vec<Attribute>, - pub pats: Vec<P<Pat>>, + pub pat: P<Pat>, pub guard: Option<P<Expr>>, pub body: P<Expr>, pub span: Span, @@ -1146,12 +1148,9 @@ pub enum ExprKind { Cast(P<Expr>, P<Ty>), /// A type ascription (e.g., `42: usize`). Type(P<Expr>, P<Ty>), - /// A `let pats = expr` expression that is only semantically allowed in the condition + /// A `let pat = expr` expression that is only semantically allowed in the condition /// of `if` / `while` expressions. (e.g., `if let 0 = x { .. }`). - /// - /// The `Vec<P<Pat>>` is for or-patterns at the top level. - /// FIXME(54883): Change this to just `P<Pat>`. - Let(Vec<P<Pat>>, P<Expr>), + Let(P<Pat>, P<Expr>), /// An `if` block, with an optional `else` block. /// /// `if expr { block } else { expr }` diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index e894fd17ff5..dc6cbfcf6ad 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -537,9 +537,9 @@ impl<'a> ExtCtxt<'a> { let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr))); // `Ok(__try_var) => __try_var` - let ok_arm = self.arm(sp, vec![ok_pat], binding_expr); + let ok_arm = self.arm(sp, ok_pat, binding_expr); // `Err(__try_var) => return Err(__try_var)` - let err_arm = self.arm(sp, vec![err_pat], err_expr); + let err_arm = self.arm(sp, err_pat, err_expr); // `match head { Ok() => ..., Err() => ... }` self.expr_match(sp, head, vec![ok_arm, err_arm]) @@ -606,10 +606,10 @@ impl<'a> ExtCtxt<'a> { self.pat_tuple_struct(span, path, vec![pat]) } - pub fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm { + pub fn arm(&self, span: Span, pat: P<ast::Pat>, expr: P<ast::Expr>) -> ast::Arm { ast::Arm { attrs: vec![], - pats, + pat, guard: None, body: expr, span, @@ -618,7 +618,7 @@ impl<'a> ExtCtxt<'a> { } pub fn arm_unreachable(&self, span: Span) -> ast::Arm { - self.arm(span, vec![self.pat_wild(span)], self.expr_unreachable(span)) + self.arm(span, self.pat_wild(span), self.expr_unreachable(span)) } pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> { diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index e14ca4b06a0..6023c5149d0 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -402,14 +402,11 @@ pub fn noop_visit_use_tree<T: MutVisitor>(use_tree: &mut UseTree, vis: &mut T) { vis.visit_span(span); } -pub fn noop_flat_map_arm<T: MutVisitor>( - mut arm: Arm, - vis: &mut T, -) -> SmallVec<[Arm; 1]> { - let Arm { attrs, pats, guard, body, span, id } = &mut arm; +pub fn noop_flat_map_arm<T: MutVisitor>(mut arm: Arm, vis: &mut T) -> SmallVec<[Arm; 1]> { + let Arm { attrs, pat, guard, body, span, id } = &mut arm; visit_attrs(attrs, vis); vis.visit_id(id); - visit_vec(pats, |pat| vis.visit_pat(pat)); + vis.visit_pat(pat); visit_opt(guard, |guard| vis.visit_expr(guard)); vis.visit_expr(body); vis.visit_span(span); @@ -1132,8 +1129,8 @@ pub fn noop_visit_expr<T: MutVisitor>(Expr { node, id, span, attrs }: &mut Expr, vis.visit_ty(ty); } ExprKind::AddrOf(_m, ohs) => vis.visit_expr(ohs), - ExprKind::Let(pats, scrutinee) => { - visit_vec(pats, |pat| vis.visit_pat(pat)); + ExprKind::Let(pat, scrutinee) => { + vis.visit_pat(pat); vis.visit_expr(scrutinee); } ExprKind::If(cond, tr, fl) => { diff --git a/src/libsyntax/parse/parser/expr.rs b/src/libsyntax/parse/parser/expr.rs index e502a08f4b2..3db9c899dba 100644 --- a/src/libsyntax/parse/parser/expr.rs +++ b/src/libsyntax/parse/parser/expr.rs @@ -1250,8 +1250,7 @@ impl<'a> Parser<'a> { /// The `let` token has already been eaten. fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> { let lo = self.prev_span; - // FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead. - let pat = self.parse_top_pat_unpack(GateOr::No)?; + let pat = self.parse_top_pat(GateOr::No)?; self.expect(&token::Eq)?; let expr = self.with_res( Restrictions::NO_STRUCT_LITERAL, @@ -1393,8 +1392,7 @@ impl<'a> Parser<'a> { crate fn parse_arm(&mut self) -> PResult<'a, Arm> { let attrs = self.parse_outer_attributes()?; let lo = self.token.span; - // FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead. - let pat = self.parse_top_pat_unpack(GateOr::No)?; + let pat = self.parse_top_pat(GateOr::No)?; let guard = if self.eat_keyword(kw::If) { Some(self.parse_expr()?) } else { @@ -1455,7 +1453,7 @@ impl<'a> Parser<'a> { Ok(ast::Arm { attrs, - pats: pat, // FIXME(or_patterns, Centril | dlrobertson): this should just be `pat,`. + pat, guard, body: expr, span: lo.to(hi), diff --git a/src/libsyntax/parse/parser/pat.rs b/src/libsyntax/parse/parser/pat.rs index 823f880337d..669f657160b 100644 --- a/src/libsyntax/parse/parser/pat.rs +++ b/src/libsyntax/parse/parser/pat.rs @@ -36,16 +36,6 @@ impl<'a> Parser<'a> { self.parse_pat_with_range_pat(true, expected) } - // FIXME(or_patterns, Centril | dlrobertson): - // remove this and use `parse_top_pat` everywhere it is used instead. - pub(super) fn parse_top_pat_unpack(&mut self, gate_or: GateOr) -> PResult<'a, Vec<P<Pat>>> { - self.parse_top_pat(gate_or) - .map(|pat| pat.and_then(|pat| match pat.node { - PatKind::Or(pats) => pats, - node => vec![self.mk_pat(pat.span, node)], - })) - } - /// Entry point to the main pattern parser. /// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level. pub(super) fn parse_top_pat(&mut self, gate_or: GateOr) -> PResult<'a, P<Pat>> { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 37305055e62..6772bbce21b 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1710,11 +1710,11 @@ impl<'a> State<'a> { self.ann.post(self, AnnNode::Block(blk)) } - /// Print a `let pats = scrutinee` expression. - crate fn print_let(&mut self, pats: &[P<ast::Pat>], scrutinee: &ast::Expr) { + /// Print a `let pat = scrutinee` expression. + crate fn print_let(&mut self, pat: &ast::Pat, scrutinee: &ast::Expr) { self.s.word("let "); - self.print_pats(pats); + self.print_pat(pat); self.s.space(); self.word_space("="); @@ -2040,8 +2040,8 @@ impl<'a> State<'a> { self.word_space(":"); self.print_type(ty); } - ast::ExprKind::Let(ref pats, ref scrutinee) => { - self.print_let(pats, scrutinee); + ast::ExprKind::Let(ref pat, ref scrutinee) => { + self.print_let(pat, scrutinee); } ast::ExprKind::If(ref test, ref blk, ref elseopt) => { self.print_if(test, blk, elseopt.as_ref().map(|e| &**e)); @@ -2451,21 +2451,16 @@ impl<'a> State<'a> { self.ann.post(self, AnnNode::Pat(pat)) } - fn print_pats(&mut self, pats: &[P<ast::Pat>]) { - self.strsep("|", true, Inconsistent, pats, |s, p| s.print_pat(p)); - } - fn print_arm(&mut self, arm: &ast::Arm) { - // I have no idea why this check is necessary, but here it - // is :( + // I have no idea why this check is necessary, but here it is :( if arm.attrs.is_empty() { self.s.space(); } self.cbox(INDENT_UNIT); self.ibox(0); - self.maybe_print_comment(arm.pats[0].span.lo()); + self.maybe_print_comment(arm.pat.span.lo()); self.print_outer_attributes(&arm.attrs); - self.print_pats(&arm.pats); + self.print_pat(&arm.pat); self.s.space(); if let Some(ref e) = arm.guard { self.word_space("if"); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index ce1568316f8..421c327aa41 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -678,9 +678,8 @@ pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonCo } pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) { - for attr in expression.attrs.iter() { - visitor.visit_attribute(attr); - } + walk_list!(visitor, visit_attribute, expression.attrs.iter()); + match expression.node { ExprKind::Box(ref subexpression) => { visitor.visit_expr(subexpression) @@ -719,8 +718,8 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) { visitor.visit_expr(subexpression); visitor.visit_ty(typ) } - ExprKind::Let(ref pats, ref scrutinee) => { - walk_list!(visitor, visit_pat, pats); + ExprKind::Let(ref pat, ref scrutinee) => { + visitor.visit_pat(pat); visitor.visit_expr(scrutinee); } ExprKind::If(ref head_expression, ref if_block, ref optional_else) => { @@ -831,10 +830,10 @@ pub fn walk_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Param) { } pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) { - walk_list!(visitor, visit_pat, &arm.pats); - if let Some(ref e) = &arm.guard { - visitor.visit_expr(e); - } + visitor.visit_pat(&arm.pat); + // NOTE(or_patterns; Centril | dlrobertson): + // If you change this, also change the hack in `lowering.rs`. + walk_list!(visitor, visit_expr, &arm.guard); visitor.visit_expr(&arm.body); walk_list!(visitor, visit_attribute, &arm.attrs); } |
