]> git.lizzy.rs Git - rust.git/blobdiff - src/libsyntax/print/pprust.rs
Merge VariantData and VariantData_
[rust.git] / src / libsyntax / print / pprust.rs
index 49b6dbed27e8c2991fe8a9cad3b85cf55e385603..e5a9ce216a92ab5ff1ebe8f76c7d9ab76a7c03dd 100644 (file)
@@ -520,6 +520,18 @@ fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F) -> io::Result<()>
         self.end()
     }
 
+    fn commasep_iter<'it, T: 'it, F, I>(&mut self, b: Breaks, elts: I, mut op: F) -> io::Result<()>
+        where F: FnMut(&mut Self, &T) -> io::Result<()>,
+              I: Iterator<Item=&'it T>,
+    {
+        try!(self.rbox(0, b));
+        let mut first = true;
+        for elt in elts {
+            if first { first = false; } else { try!(self.word_space(",")); }
+            try!(op(self, elt));
+        }
+        self.end()
+    }
 
     fn next_lit(&mut self, pos: BytePos) -> Option<comments::Literal> {
         let mut cur_lit = self.cur_cmnt_and_lit().cur_lit;
@@ -709,7 +721,7 @@ fn print_inner_attributes(&mut self,
         let mut count = 0;
         for attr in attrs {
             match attr.node.style {
-                ast::AttrInner => {
+                ast::AttrStyle::Inner => {
                     try!(self.print_attribute(attr));
                     count += 1;
                 }
@@ -727,7 +739,7 @@ fn print_outer_attributes(&mut self,
         let mut count = 0;
         for attr in attrs {
             match attr.node.style {
-                ast::AttrOuter => {
+                ast::AttrStyle::Outer => {
                     try!(self.print_attribute(attr));
                     count += 1;
                 }
@@ -747,8 +759,8 @@ fn print_attribute(&mut self, attr: &ast::Attribute) -> io::Result<()> {
             word(self.writer(), &attr.value_str().unwrap())
         } else {
             match attr.node.style {
-                ast::AttrInner => try!(word(self.writer(), "#![")),
-                ast::AttrOuter => try!(word(self.writer(), "#[")),
+                ast::AttrStyle::Inner => try!(word(self.writer(), "#![")),
+                ast::AttrStyle::Outer => try!(word(self.writer(), "#[")),
             }
             try!(self.print_meta_item(&*attr.meta()));
             word(self.writer(), "]")
@@ -1223,7 +1235,7 @@ pub fn print_item(&mut self, item: &ast::Item) -> io::Result<()> {
             }
             ast::ItemStruct(ref struct_def, ref generics) => {
                 try!(self.head(&visibility_qualified(item.vis,"struct")));
-                try!(self.print_struct(&**struct_def, generics, item.ident, item.span));
+                try!(self.print_struct(&struct_def, generics, item.ident, item.span, true));
             }
 
             ast::ItemDefaultImpl(unsafety, ref trait_ref) => {
@@ -1385,17 +1397,18 @@ pub fn print_visibility(&mut self, vis: ast::Visibility) -> io::Result<()> {
     }
 
     pub fn print_struct(&mut self,
-                        struct_def: &ast::StructDef,
+                        struct_def: &ast::VariantData,
                         generics: &ast::Generics,
                         ident: ast::Ident,
-                        span: codemap::Span) -> io::Result<()> {
+                        span: codemap::Span,
+                        print_finalizer: bool) -> io::Result<()> {
         try!(self.print_ident(ident));
         try!(self.print_generics(generics));
-        if ast_util::struct_def_is_tuple_like(struct_def) {
-            if !struct_def.fields.is_empty() {
+        if !struct_def.is_struct() {
+            if struct_def.is_tuple() {
                 try!(self.popen());
-                try!(self.commasep(
-                    Inconsistent, &struct_def.fields,
+                try!(self.commasep_iter(
+                    Inconsistent, struct_def.fields(),
                     |s, field| {
                         match field.node.kind {
                             ast::NamedField(..) => panic!("unexpected named field"),
@@ -1410,7 +1423,9 @@ pub fn print_struct(&mut self,
                 try!(self.pclose());
             }
             try!(self.print_where_clause(&generics.where_clause));
-            try!(word(&mut self.s, ";"));
+            if print_finalizer {
+                try!(word(&mut self.s, ";"));
+            }
             try!(self.end());
             self.end() // close the outer-box
         } else {
@@ -1419,7 +1434,7 @@ pub fn print_struct(&mut self,
             try!(self.bopen());
             try!(self.hardbreak_if_not_bol());
 
-            for field in &struct_def.fields {
+            for field in struct_def.fields() {
                 match field.node.kind {
                     ast::UnnamedField(..) => panic!("unexpected unnamed field"),
                     ast::NamedField(ident, visibility) => {
@@ -1505,23 +1520,9 @@ pub fn print_tts(&mut self, tts: &[ast::TokenTree]) -> io::Result<()> {
     }
 
     pub fn print_variant(&mut self, v: &ast::Variant) -> io::Result<()> {
-        match v.node.kind {
-            ast::TupleVariantKind(ref args) => {
-                try!(self.print_ident(v.node.name));
-                if !args.is_empty() {
-                    try!(self.popen());
-                    try!(self.commasep(Consistent,
-                                       &args[..],
-                                       |s, arg| s.print_type(&*arg.ty)));
-                    try!(self.pclose());
-                }
-            }
-            ast::StructVariantKind(ref struct_def) => {
-                try!(self.head(""));
-                let generics = ast_util::empty_generics();
-                try!(self.print_struct(&**struct_def, &generics, v.node.name, v.span));
-            }
-        }
+        try!(self.head(""));
+        let generics = ast_util::empty_generics();
+        try!(self.print_struct(&v.node.data, &generics, v.node.name, v.span, false));
         match v.node.disr_expr {
             Some(ref d) => {
                 try!(space(&mut self.s));
@@ -2045,7 +2046,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> io::Result<()> {
                 try!(space(&mut self.s));
                 try!(self.print_block(&**blk));
             }
-            ast::ExprMatch(ref expr, ref arms, _) => {
+            ast::ExprMatch(ref expr, ref arms) => {
                 try!(self.cbox(indent_unit));
                 try!(self.ibox(4));
                 try!(self.word_nbsp("match"));
@@ -3103,6 +3104,7 @@ mod tests {
     use ast_util;
     use codemap;
     use parse::token;
+    use ptr::P;
 
     #[test]
     fn test_fun_to_string() {
@@ -3129,8 +3131,7 @@ fn test_variant_to_string() {
             name: ident,
             attrs: Vec::new(),
             // making this up as I go.... ?
-            kind: ast::TupleVariantKind(Vec::new()),
-            id: 0,
+            data: P(ast::VariantData::Unit(ast::DUMMY_NODE_ID)),
             disr_expr: None,
         });