]> git.lizzy.rs Git - rust.git/blobdiff - src/libsyntax/print/pprust.rs
auto merge of #15999 : Kimundi/rust/fix_folder, r=nikomatsakis
[rust.git] / src / libsyntax / print / pprust.rs
index 305e67a916eb986a295d4dd33afef214a7385362..675588a4460fffd3ba82f3cfbaefed12040bd5c1 100644 (file)
@@ -18,7 +18,6 @@
 use codemap::{CodeMap, BytePos};
 use codemap;
 use diagnostic;
-use parse::classify::expr_is_simple_block;
 use parse::token;
 use parse::lexer::comments;
 use parse;
@@ -137,7 +136,7 @@ pub fn to_string(f: |&mut State| -> IoResult<()>) -> String {
         // downcasts.
         let (_, wr): (uint, Box<MemWriter>) = mem::transmute_copy(&s.s.out);
         let result =
-            String::from_utf8(Vec::from_slice(wr.get_ref())).unwrap();
+            String::from_utf8(Vec::from_slice(wr.get_ref().as_slice())).unwrap();
         mem::forget(wr);
         result.to_string()
     }
@@ -151,6 +150,10 @@ pub fn pat_to_string(pat: &ast::Pat) -> String {
     to_string(|s| s.print_pat(pat))
 }
 
+pub fn arm_to_string(arm: &ast::Arm) -> String {
+    to_string(|s| s.print_arm(arm))
+}
+
 pub fn expr_to_string(e: &ast::Expr) -> String {
     to_string(|s| s.print_expr(e))
 }
@@ -1402,53 +1405,8 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> {
                 try!(self.print_expr(&**expr));
                 try!(space(&mut self.s));
                 try!(self.bopen());
-                let len = arms.len();
-                for (i, arm) in arms.iter().enumerate() {
-                    // I have no idea why this check is necessary, but here it
-                    // is :(
-                    if arm.attrs.is_empty() {
-                        try!(space(&mut self.s));
-                    }
-                    try!(self.cbox(indent_unit));
-                    try!(self.ibox(0u));
-                    try!(self.print_outer_attributes(arm.attrs.as_slice()));
-                    let mut first = true;
-                    for p in arm.pats.iter() {
-                        if first {
-                            first = false;
-                        } else {
-                            try!(space(&mut self.s));
-                            try!(self.word_space("|"));
-                        }
-                        try!(self.print_pat(&**p));
-                    }
-                    try!(space(&mut self.s));
-                    match arm.guard {
-                        Some(ref e) => {
-                            try!(self.word_space("if"));
-                            try!(self.print_expr(&**e));
-                            try!(space(&mut self.s));
-                        }
-                        None => ()
-                    }
-                    try!(self.word_space("=>"));
-
-                    match arm.body.node {
-                        ast::ExprBlock(ref blk) => {
-                            // the block will close the pattern's ibox
-                            try!(self.print_block_unclosed_indent(&**blk,
-                                                                  indent_unit));
-                        }
-                        _ => {
-                            try!(self.end()); // close the ibox for the pattern
-                            try!(self.print_expr(&*arm.body));
-                        }
-                    }
-                    if !expr_is_simple_block(expr.clone())
-                        && i < len - 1 {
-                        try!(word(&mut self.s, ","));
-                    }
-                    try!(self.end()); // close enclosing cbox
+                for arm in arms.iter() {
+                    try!(self.print_arm(arm));
                 }
                 try!(self.bclose_(expr.span, indent_unit));
             }
@@ -1882,6 +1840,51 @@ pub fn print_pat(&mut self, pat: &ast::Pat) -> IoResult<()> {
         self.ann.post(self, NodePat(pat))
     }
 
+    fn print_arm(&mut self, arm: &ast::Arm) -> IoResult<()> {
+        // I have no idea why this check is necessary, but here it
+        // is :(
+        if arm.attrs.is_empty() {
+            try!(space(&mut self.s));
+        }
+        try!(self.cbox(indent_unit));
+        try!(self.ibox(0u));
+        try!(self.print_outer_attributes(arm.attrs.as_slice()));
+        let mut first = true;
+        for p in arm.pats.iter() {
+            if first {
+                first = false;
+            } else {
+                try!(space(&mut self.s));
+                try!(self.word_space("|"));
+            }
+            try!(self.print_pat(&**p));
+        }
+        try!(space(&mut self.s));
+        match arm.guard {
+            Some(ref e) => {
+                try!(self.word_space("if"));
+                try!(self.print_expr(&**e));
+                try!(space(&mut self.s));
+            }
+            None => ()
+        }
+        try!(self.word_space("=>"));
+
+        match arm.body.node {
+            ast::ExprBlock(ref blk) => {
+                // the block will close the pattern's ibox
+                try!(self.print_block_unclosed_indent(&**blk,
+                                                      indent_unit));
+            }
+            _ => {
+                try!(self.end()); // close the ibox for the pattern
+                try!(self.print_expr(&*arm.body));
+                try!(word(&mut self.s, ","));
+            }
+        }
+        self.end() // close enclosing cbox
+    }
+
     // Returns whether it printed anything
     fn print_explicit_self(&mut self,
                            explicit_self: ast::ExplicitSelf_,
@@ -1892,9 +1895,6 @@ fn print_explicit_self(&mut self,
             ast::SelfValue(_) => {
                 try!(word(&mut self.s, "self"));
             }
-            ast::SelfUniq(_) => {
-                try!(word(&mut self.s, "~self"));
-            }
             ast::SelfRegion(ref lt, m, _) => {
                 try!(word(&mut self.s, "&"));
                 try!(self.print_opt_lifetime(lt));