diff options
| author | Kevin Mehall <km@kevinmehall.net> | 2014-10-27 00:11:26 -0700 |
|---|---|---|
| committer | Kevin Mehall <km@kevinmehall.net> | 2014-10-27 00:35:35 -0700 |
| commit | e5f709079a2f9c5227e10f5f4cd0371a2fd76ae3 (patch) | |
| tree | a60a4c92c4f9b906cb74846b79b17bafad7f2a7a | |
| parent | f037452447f5f46deb26e1c483fe88fb51a19198 (diff) | |
| download | rust-e5f709079a2f9c5227e10f5f4cd0371a2fd76ae3.tar.gz rust-e5f709079a2f9c5227e10f5f4cd0371a2fd76ae3.zip | |
Preserve struct field pattern shorthand in the prettyprinter.
Use the `is_shorthand` field introduced by #17813 (ead6c4b) to make the prettyprinter output the shorthand form. Fixes a few places that set `is_shorthand: true` when the pattern is not a PatIdent with the same name as the field.
| -rw-r--r-- | src/librustc/middle/check_match.rs | 2 | ||||
| -rw-r--r-- | src/librustc/middle/const_eval.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/generic/mod.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 6 | ||||
| -rw-r--r-- | src/test/pretty/struct-pattern.rs | 15 |
5 files changed, 22 insertions, 5 deletions
diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index 315266dbc84..fe38669ea6c 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -421,7 +421,7 @@ fn construct_witness(cx: &MatchCheckCtxt, ctor: &Constructor, node: FieldPat { ident: Ident::new(field.name), pat: pat, - is_shorthand: true, + is_shorthand: false, } }).collect(); let has_more_fields = field_pats.len() < pats_len; diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs index 9e2f78edb77..3d6b319ac0d 100644 --- a/src/librustc/middle/const_eval.rs +++ b/src/librustc/middle/const_eval.rs @@ -341,7 +341,7 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P<Pat> { node: FieldPat { ident: field.ident.node, pat: const_expr_to_pat(tcx, &*field.expr), - is_shorthand: true, + is_shorthand: false, }, }).collect(); PatStruct(path.clone(), field_pats, false) diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 533a28998bd..7c32b845508 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -1250,7 +1250,7 @@ impl<'a> TraitDef<'a> { // id is guaranteed to be Some codemap::Spanned { span: pat.span, - node: ast::FieldPat { ident: id.unwrap(), pat: pat, is_shorthand: true }, + node: ast::FieldPat { ident: id.unwrap(), pat: pat, is_shorthand: false }, } }).collect(); cx.pat_struct(self.span, matching_path, field_pats) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index b63f9b0120b..ed4a16da013 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1983,8 +1983,10 @@ impl<'a> State<'a> { Consistent, fields.as_slice(), |s, f| { try!(s.cbox(indent_unit)); - try!(s.print_ident(f.node.ident)); - try!(s.word_nbsp(":")); + if !f.node.is_shorthand { + try!(s.print_ident(f.node.ident)); + try!(s.word_nbsp(":")); + } try!(s.print_pat(&*f.node.pat)); s.end() }, diff --git a/src/test/pretty/struct-pattern.rs b/src/test/pretty/struct-pattern.rs new file mode 100644 index 00000000000..b0795bb08f3 --- /dev/null +++ b/src/test/pretty/struct-pattern.rs @@ -0,0 +1,15 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// pp-exact +// pretty-compare-only +// Testing that shorthand struct patterns are preserved + +fn main() { let Foo { a, ref b, mut c, x: y, z: z } = foo; } |
