diff options
| author | bors <bors@rust-lang.org> | 2015-01-19 08:22:58 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-01-19 08:22:58 +0000 |
| commit | 135cac852822afe822cc1e4eb9546b96eb2cb35d (patch) | |
| tree | cc6382f0c7d92c7755151887657ad80a53dd8694 /src/libsyntax | |
| parent | bd8a43c668ba93d29e9671c0c8dc6b67428bf492 (diff) | |
| parent | 3f0cc8011aef3f530663302d525bd2d8cb493db5 (diff) | |
| download | rust-135cac852822afe822cc1e4eb9546b96eb2cb35d.tar.gz rust-135cac852822afe822cc1e4eb9546b96eb2cb35d.zip | |
Auto merge of #21099 - sanxiyn:opt-return-ty, r=alexcrichton
This avoids having ast::Ty nodes which have no counterpart in the source.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 29 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 28 | ||||
| -rw-r--r-- | src/libsyntax/test.rs | 14 |
6 files changed, 27 insertions, 63 deletions
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<Ty>), } @@ -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<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> { 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<T: Folder>(ni: P<ForeignItem>, 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 130972b4582..b2f6938f513 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)) } } @@ -4550,15 +4547,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, @@ -4575,15 +4564,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 |
