X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibsyntax%2Fprint%2Fpprust.rs;h=e5a9ce216a92ab5ff1ebe8f76c7d9ab76a7c03dd;hb=46750d0409a9c3ba5214aa20ccb0c9bdbf09ea7e;hp=6de6d32dfb32809569ee4068088cec8507878769;hpb=fd38a75077a4c5efc87413b7f9f7f1b6bc9db9af;p=rust.git diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 6de6d32dfb3..e5a9ce216a9 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -297,7 +297,7 @@ pub fn token_to_string(tok: &Token) -> String { token::NtBlock(ref e) => block_to_string(&**e), token::NtStmt(ref e) => stmt_to_string(&**e), token::NtPat(ref e) => pat_to_string(&**e), - token::NtIdent(ref e, _) => ident_to_string(&**e), + token::NtIdent(ref e, _) => ident_to_string(**e), token::NtTT(ref e) => tt_to_string(&**e), token::NtArm(ref e) => arm_to_string(&*e), token::NtImplItem(ref e) => impl_item_to_string(&**e), @@ -376,8 +376,8 @@ pub fn path_to_string(p: &ast::Path) -> String { to_string(|s| s.print_path(p, false, 0)) } -pub fn ident_to_string(id: &ast::Ident) -> String { - to_string(|s| s.print_ident(*id)) +pub fn ident_to_string(id: ast::Ident) -> String { + to_string(|s| s.print_ident(id)) } pub fn fun_to_string(decl: &ast::FnDecl, @@ -520,6 +520,18 @@ fn commasep(&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, + { + 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 { 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) => { @@ -1307,16 +1319,14 @@ pub fn print_item(&mut self, item: &ast::Item) -> io::Result<()> { } try!(self.bclose(item.span)); } - // I think it's reasonable to hide the context here: - ast::ItemMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _), - ..}) => { + ast::ItemMac(codemap::Spanned { ref node, .. }) => { try!(self.print_visibility(item.vis)); - try!(self.print_path(pth, false, 0)); + try!(self.print_path(&node.path, false, 0)); try!(word(&mut self.s, "! ")); try!(self.print_ident(item.ident)); try!(self.cbox(indent_unit)); try!(self.popen()); - try!(self.print_tts(&tts[..])); + try!(self.print_tts(&node.tts[..])); try!(self.pclose()); try!(word(&mut self.s, ";")); try!(self.end()); @@ -1387,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"), @@ -1412,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 { @@ -1421,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) => { @@ -1507,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)); @@ -1599,14 +1598,13 @@ pub fn print_impl_item(&mut self, ii: &ast::ImplItem) -> io::Result<()> { ast::TypeImplItem(ref ty) => { try!(self.print_associated_type(ii.ident, None, Some(ty))); } - ast::MacImplItem(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _), - ..}) => { + ast::MacImplItem(codemap::Spanned { ref node, .. }) => { // code copied from ItemMac: - try!(self.print_path(pth, false, 0)); + try!(self.print_path(&node.path, false, 0)); try!(word(&mut self.s, "! ")); try!(self.cbox(indent_unit)); try!(self.popen()); - try!(self.print_tts(&tts[..])); + try!(self.print_tts(&node.tts[..])); try!(self.pclose()); try!(word(&mut self.s, ";")); try!(self.end()) @@ -1765,23 +1763,18 @@ pub fn print_if_let(&mut self, pat: &ast::Pat, expr: &ast::Expr, blk: &ast::Bloc pub fn print_mac(&mut self, m: &ast::Mac, delim: token::DelimToken) -> io::Result<()> { - match m.node { - // I think it's reasonable to hide the ctxt here: - ast::MacInvocTT(ref pth, ref tts, _) => { - try!(self.print_path(pth, false, 0)); - try!(word(&mut self.s, "!")); - match delim { - token::Paren => try!(self.popen()), - token::Bracket => try!(word(&mut self.s, "[")), - token::Brace => try!(self.bopen()), - } - try!(self.print_tts(tts)); - match delim { - token::Paren => self.pclose(), - token::Bracket => word(&mut self.s, "]"), - token::Brace => self.bclose(m.span), - } - } + try!(self.print_path(&m.node.path, false, 0)); + try!(word(&mut self.s, "!")); + match delim { + token::Paren => try!(self.popen()), + token::Bracket => try!(word(&mut self.s, "[")), + token::Brace => try!(self.bopen()), + } + try!(self.print_tts(&m.node.tts)); + match delim { + token::Paren => self.pclose(), + token::Bracket => word(&mut self.s, "]"), + token::Brace => self.bclose(m.span), } } @@ -1819,13 +1812,12 @@ pub fn print_expr_maybe_paren(&mut self, expr: &ast::Expr) -> io::Result<()> { Ok(()) } - fn print_expr_box(&mut self, - place: &Option>, - expr: &ast::Expr) -> io::Result<()> { - try!(word(&mut self.s, "box")); - try!(word(&mut self.s, "(")); - try!(place.as_ref().map_or(Ok(()), |e|self.print_expr(&**e))); - try!(self.word_space(")")); + fn print_expr_in_place(&mut self, + place: &ast::Expr, + expr: &ast::Expr) -> io::Result<()> { + try!(self.word_space("in")); + try!(self.print_expr(place)); + try!(space(&mut self.s)); self.print_expr(expr) } @@ -1956,8 +1948,12 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> io::Result<()> { try!(self.ibox(indent_unit)); try!(self.ann.pre(self, NodeExpr(expr))); match expr.node { - ast::ExprBox(ref place, ref expr) => { - try!(self.print_expr_box(place, &**expr)); + ast::ExprBox(ref expr) => { + try!(self.word_space("box")); + try!(self.print_expr(expr)); + } + ast::ExprInPlace(ref place, ref expr) => { + try!(self.print_expr_in_place(place, expr)); } ast::ExprVec(ref exprs) => { try!(self.print_expr_vec(&exprs[..])); @@ -2050,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")); @@ -2217,7 +2213,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> io::Result<()> { if a.alignstack { options.push("alignstack"); } - if a.dialect == ast::AsmDialect::AsmIntel { + if a.dialect == ast::AsmDialect::Intel { options.push("intel"); } @@ -2862,7 +2858,6 @@ pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> io::Result<()> { ast::ViewPathSimple(ident, ref path) => { try!(self.print_path(path, false, 0)); - // FIXME(#6993) can't compare identifiers directly here if path.segments.last().unwrap().identifier.name != ident.name { try!(space(&mut self.s)); @@ -3109,6 +3104,7 @@ mod tests { use ast_util; use codemap; use parse::token; + use ptr::P; #[test] fn test_fun_to_string() { @@ -3135,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, });