From 3f0cc8011aef3f530663302d525bd2d8cb493db5 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Sun, 18 Jan 2015 22:49:19 +0900 Subject: Make output type in ast::FnDecl optional --- src/libsyntax/ast.rs | 5 +++++ src/libsyntax/fold.rs | 10 ++-------- src/libsyntax/parse/mod.rs | 4 +--- src/libsyntax/parse/parser.rs | 29 +++++------------------------ src/libsyntax/print/pprust.rs | 28 ++++++++++------------------ src/libsyntax/test.rs | 14 ++++---------- 6 files changed, 27 insertions(+), 63 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 0ea429116b0..fcf80410da2 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1390,6 +1390,10 @@ pub enum FunctionRetTy { /// Functions with return type ! that always /// raise an error or exit (i.e. never return to the caller) NoReturn(Span), + /// Return type is not specified. Functions default to () and + /// closures default to inference. Span points to where return + /// type would be inserted. + DefaultReturn(Span), /// Everything else Return(P), } @@ -1398,6 +1402,7 @@ impl FunctionRetTy { pub fn span(&self) -> Span { match *self { NoReturn(span) => span, + DefaultReturn(span) => span, Return(ref ty) => ty.span } } diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 16c29c9b5eb..f484650ad5b 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -726,6 +726,7 @@ pub fn noop_fold_fn_decl(decl: P, fld: &mut T) -> P { inputs: inputs.move_map(|x| fld.fold_arg(x)), output: match output { Return(ty) => Return(fld.fold_ty(ty)), + DefaultReturn(span) => DefaultReturn(span), NoReturn(span) => NoReturn(span) }, variadic: variadic @@ -1189,14 +1190,7 @@ pub fn noop_fold_foreign_item(ni: P, folder: &mut T) -> attrs: attrs.move_map(|x| folder.fold_attribute(x)), node: match node { ForeignItemFn(fdec, generics) => { - ForeignItemFn(fdec.map(|FnDecl {inputs, output, variadic}| FnDecl { - inputs: inputs.move_map(|a| folder.fold_arg(a)), - output: match output { - Return(ty) => Return(folder.fold_ty(ty)), - NoReturn(span) => NoReturn(span) - }, - variadic: variadic - }), folder.fold_generics(generics)) + ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics)) } ForeignItemStatic(t, m) => { ForeignItemStatic(folder.fold_ty(t), m) diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index f1f547ba0c7..90e236dfde3 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -1066,9 +1066,7 @@ mod test { }), id: ast::DUMMY_NODE_ID }), - output: ast::Return(P(ast::Ty{id: ast::DUMMY_NODE_ID, - node: ast::TyTup(vec![]), - span:sp(15,15)})), // not sure + output: ast::DefaultReturn(sp(15, 15)), variadic: false }), ast::Unsafety::Normal, diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 30cc9836374..d4a02146c5e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -19,7 +19,8 @@ use ast::{Mod, BiAdd, Arg, Arm, Attribute, BindByRef, BindByValue}; use ast::{BiBitAnd, BiBitOr, BiBitXor, BiRem, BiLt, BiGt, Block}; use ast::{BlockCheckMode, CaptureByRef, CaptureByValue, CaptureClause}; use ast::{Crate, CrateConfig, Decl, DeclItem}; -use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf}; +use ast::{DeclLocal, DefaultBlock, DefaultReturn}; +use ast::{UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf}; use ast::{Expr, Expr_, ExprAddrOf, ExprMatch, ExprAgain}; use ast::{ExprAssign, ExprAssignOp, ExprBinary, ExprBlock, ExprBox}; use ast::{ExprBreak, ExprCall, ExprCast}; @@ -1426,11 +1427,7 @@ impl<'a> Parser<'a> { } } else { let pos = self.span.lo; - Return(P(Ty { - id: ast::DUMMY_NODE_ID, - node: TyTup(vec![]), - span: mk_sp(pos, pos), - })) + DefaultReturn(mk_sp(pos, pos)) } } @@ -4548,15 +4545,7 @@ impl<'a> Parser<'a> { (optional_unboxed_closure_kind, args) } }; - let output = if self.check(&token::RArrow) { - self.parse_ret_ty() - } else { - Return(P(Ty { - id: ast::DUMMY_NODE_ID, - node: TyInfer, - span: self.span, - })) - }; + let output = self.parse_ret_ty(); (P(FnDecl { inputs: inputs_captures, @@ -4573,15 +4562,7 @@ impl<'a> Parser<'a> { seq_sep_trailing_allowed(token::Comma), |p| p.parse_fn_block_arg()); - let output = if self.check(&token::RArrow) { - self.parse_ret_ty() - } else { - Return(P(Ty { - id: ast::DUMMY_NODE_ID, - node: TyInfer, - span: self.span, - })) - }; + let output = self.parse_ret_ty(); P(FnDecl { inputs: inputs, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 5d76dc71006..b59e770c6ba 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -2351,10 +2351,8 @@ impl<'a> State<'a> { try!(self.print_fn_args(decl, None)); try!(word(&mut self.s, "|")); - if let ast::Return(ref ty) = decl.output { - if ty.node == ast::TyInfer { - return self.maybe_print_comment(ty.span.lo); - } + if let ast::DefaultReturn(..) = decl.output { + return Ok(()); } try!(self.space_if_not_bol()); @@ -2364,6 +2362,7 @@ impl<'a> State<'a> { try!(self.print_type(&**ty)); self.maybe_print_comment(ty.span.lo) } + ast::DefaultReturn(..) => unreachable!(), ast::NoReturn(span) => { try!(self.word_nbsp("!")); self.maybe_print_comment(span.lo) @@ -2385,10 +2384,8 @@ impl<'a> State<'a> { try!(self.print_fn_args(decl, None)); try!(word(&mut self.s, ")")); - if let ast::Return(ref ty) = decl.output { - if ty.node == ast::TyInfer { - return self.maybe_print_comment(ty.span.lo); - } + if let ast::DefaultReturn(..) = decl.output { + return Ok(()); } try!(self.space_if_not_bol()); @@ -2398,6 +2395,7 @@ impl<'a> State<'a> { try!(self.print_type(&**ty)); self.maybe_print_comment(ty.span.lo) } + ast::DefaultReturn(..) => unreachable!(), ast::NoReturn(span) => { try!(self.word_nbsp("!")); self.maybe_print_comment(span.lo) @@ -2684,13 +2682,8 @@ impl<'a> State<'a> { } pub fn print_fn_output(&mut self, decl: &ast::FnDecl) -> IoResult<()> { - if let ast::Return(ref ty) = decl.output { - match ty.node { - ast::TyTup(ref tys) if tys.is_empty() => { - return self.maybe_print_comment(ty.span.lo); - } - _ => () - } + if let ast::DefaultReturn(..) = decl.output { + return Ok(()); } try!(self.space_if_not_bol()); @@ -2699,6 +2692,7 @@ impl<'a> State<'a> { match decl.output { ast::NoReturn(_) => try!(self.word_nbsp("!")), + ast::DefaultReturn(..) => unreachable!(), ast::Return(ref ty) => try!(self.print_type(&**ty)) } @@ -3071,9 +3065,7 @@ mod test { let decl = ast::FnDecl { inputs: Vec::new(), - output: ast::Return(P(ast::Ty {id: 0, - node: ast::TyTup(vec![]), - span: codemap::DUMMY_SP})), + output: ast::DefaultReturn(codemap::DUMMY_SP), variadic: false }; let generics = ast_util::empty_generics(); diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 895268f96c8..5f869d5093f 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -297,11 +297,8 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool { match &i.node { &ast::ItemFn(ref decl, _, _, ref generics, _) => { let no_output = match decl.output { - ast::Return(ref ret_ty) => match ret_ty.node { - ast::TyTup(ref tys) if tys.is_empty() => true, - _ => false, - }, - ast::NoReturn(_) => false + ast::DefaultReturn(..) => true, + _ => false }; if decl.inputs.is_empty() && no_output @@ -336,11 +333,8 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool { ast::ItemFn(ref decl, _, _, ref generics, _) => { let input_cnt = decl.inputs.len(); let no_output = match decl.output { - ast::Return(ref ret_ty) => match ret_ty.node { - ast::TyTup(ref tys) if tys.is_empty() => true, - _ => false, - }, - ast::NoReturn(_) => false + ast::DefaultReturn(..) => true, + _ => false }; let tparm_cnt = generics.ty_params.len(); // NB: inadequate check, but we're running -- cgit 1.4.1-3-g733a5