Skip to content

Commit 3f0cc80

Browse files
committed
Make output type in ast::FnDecl optional
1 parent a833337 commit 3f0cc80

File tree

14 files changed

+76
-85
lines changed

14 files changed

+76
-85
lines changed

Diff for: src/librustc/middle/infer/error_reporting.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
11771177
ast::Return(ref ret_ty) => ast::Return(
11781178
self.rebuild_arg_ty_or_output(&**ret_ty, lifetime, anon_nums, region_names)
11791179
),
1180+
ast::DefaultReturn(span) => ast::DefaultReturn(span),
11801181
ast::NoReturn(span) => ast::NoReturn(span)
11811182
}
11821183
}

Diff for: src/librustc_trans/trans/debuginfo.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -1450,18 +1450,15 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
14501450
let mut signature = Vec::with_capacity(fn_decl.inputs.len() + 1);
14511451

14521452
// Return type -- llvm::DIBuilder wants this at index 0
1453-
match fn_decl.output {
1454-
ast::Return(ref ret_ty) if ret_ty.node == ast::TyTup(vec![]) =>
1455-
signature.push(ptr::null_mut()),
1456-
_ => {
1457-
assert_type_for_node_id(cx, fn_ast_id, error_reporting_span);
1458-
1459-
let return_type = ty::node_id_to_type(cx.tcx(), fn_ast_id);
1460-
let return_type = monomorphize::apply_param_substs(cx.tcx(),
1461-
param_substs,
1462-
&return_type);
1463-
signature.push(type_metadata(cx, return_type, codemap::DUMMY_SP));
1464-
}
1453+
assert_type_for_node_id(cx, fn_ast_id, error_reporting_span);
1454+
let return_type = ty::node_id_to_type(cx.tcx(), fn_ast_id);
1455+
let return_type = monomorphize::apply_param_substs(cx.tcx(),
1456+
param_substs,
1457+
&return_type);
1458+
if ty::type_is_nil(return_type) {
1459+
signature.push(ptr::null_mut())
1460+
} else {
1461+
signature.push(type_metadata(cx, return_type, codemap::DUMMY_SP));
14651462
}
14661463

14671464
// Arguments types

Diff for: src/librustc_trans/trans/foreign.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,8 @@ fn gate_simd_ffi(tcx: &ty::ctxt, decl: &ast::FnDecl, ty: &ty::BareFnTy) {
445445
for (input, ty) in decl.inputs.iter().zip(sig.inputs.iter()) {
446446
check(&*input.ty, *ty)
447447
}
448-
match decl.output {
449-
ast::NoReturn(_) => {}
450-
ast::Return(ref ty) => check(&**ty, sig.output.unwrap())
448+
if let ast::Return(ref ty) = decl.output {
449+
check(&**ty, sig.output.unwrap())
451450
}
452451
}
453452
}

Diff for: src/librustc_typeck/astconv.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,8 @@ fn ty_of_method_or_bare_fn<'a, 'tcx>(this: &AstConv<'tcx>,
13591359
implied_output_region,
13601360
lifetimes_for_params,
13611361
&**output)),
1362-
ast::NoReturn(_) => ty::FnDiverging
1362+
ast::DefaultReturn(..) => ty::FnConverging(ty::mk_nil(this.tcx())),
1363+
ast::NoReturn(..) => ty::FnDiverging
13631364
};
13641365

13651366
(ty::BareFnTy {
@@ -1486,14 +1487,21 @@ pub fn ty_of_closure<'tcx>(
14861487

14871488
let expected_ret_ty = expected_sig.map(|e| e.output);
14881489

1490+
let is_infer = match decl.output {
1491+
ast::Return(ref output) if output.node == ast::TyInfer => true,
1492+
ast::DefaultReturn(..) => true,
1493+
_ => false
1494+
};
1495+
14891496
let output_ty = match decl.output {
1490-
ast::Return(ref output) if output.node == ast::TyInfer && expected_ret_ty.is_some() =>
1497+
_ if is_infer && expected_ret_ty.is_some() =>
14911498
expected_ret_ty.unwrap(),
1492-
ast::Return(ref output) if output.node == ast::TyInfer =>
1493-
ty::FnConverging(this.ty_infer(output.span)),
1499+
_ if is_infer =>
1500+
ty::FnConverging(this.ty_infer(decl.output.span())),
14941501
ast::Return(ref output) =>
14951502
ty::FnConverging(ast_ty_to_ty(this, &rb, &**output)),
1496-
ast::NoReturn(_) => ty::FnDiverging
1503+
ast::DefaultReturn(..) => unreachable!(),
1504+
ast::NoReturn(..) => ty::FnDiverging
14971505
};
14981506

14991507
debug!("ty_of_closure: input_tys={}", input_tys.repr(this.tcx()));

Diff for: src/librustc_typeck/collect.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,9 @@ fn ty_of_foreign_fn_decl<'a, 'tcx>(ccx: &CollectCtxt<'a, 'tcx>,
14881488
let output = match decl.output {
14891489
ast::Return(ref ty) =>
14901490
ty::FnConverging(ast_ty_to_ty(ccx, &rb, &**ty)),
1491-
ast::NoReturn(_) =>
1491+
ast::DefaultReturn(..) =>
1492+
ty::FnConverging(ty::mk_nil(ccx.tcx)),
1493+
ast::NoReturn(..) =>
14921494
ty::FnDiverging
14931495
};
14941496

Diff for: src/librustdoc/clean/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1141,14 +1141,16 @@ impl Clean<Argument> for ast::Arg {
11411141
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Show)]
11421142
pub enum FunctionRetTy {
11431143
Return(Type),
1144+
DefaultReturn,
11441145
NoReturn
11451146
}
11461147

11471148
impl Clean<FunctionRetTy> for ast::FunctionRetTy {
11481149
fn clean(&self, cx: &DocContext) -> FunctionRetTy {
11491150
match *self {
11501151
ast::Return(ref typ) => Return(typ.clean(cx)),
1151-
ast::NoReturn(_) => NoReturn
1152+
ast::DefaultReturn(..) => DefaultReturn,
1153+
ast::NoReturn(..) => NoReturn
11521154
}
11531155
}
11541156
}

Diff for: src/librustdoc/html/format.rs

+1
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ impl fmt::String for clean::FunctionRetTy {
557557
match *self {
558558
clean::Return(clean::Tuple(ref tys)) if tys.is_empty() => Ok(()),
559559
clean::Return(ref ty) => write!(f, " -&gt; {}", ty),
560+
clean::DefaultReturn => Ok(()),
560561
clean::NoReturn => write!(f, " -&gt; !")
561562
}
562563
}

Diff for: src/libsyntax/ast.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,10 @@ pub enum FunctionRetTy {
13901390
/// Functions with return type ! that always
13911391
/// raise an error or exit (i.e. never return to the caller)
13921392
NoReturn(Span),
1393+
/// Return type is not specified. Functions default to () and
1394+
/// closures default to inference. Span points to where return
1395+
/// type would be inserted.
1396+
DefaultReturn(Span),
13931397
/// Everything else
13941398
Return(P<Ty>),
13951399
}
@@ -1398,6 +1402,7 @@ impl FunctionRetTy {
13981402
pub fn span(&self) -> Span {
13991403
match *self {
14001404
NoReturn(span) => span,
1405+
DefaultReturn(span) => span,
14011406
Return(ref ty) => ty.span
14021407
}
14031408
}

Diff for: src/libsyntax/fold.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> {
726726
inputs: inputs.move_map(|x| fld.fold_arg(x)),
727727
output: match output {
728728
Return(ty) => Return(fld.fold_ty(ty)),
729+
DefaultReturn(span) => DefaultReturn(span),
729730
NoReturn(span) => NoReturn(span)
730731
},
731732
variadic: variadic
@@ -1189,14 +1190,7 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) ->
11891190
attrs: attrs.move_map(|x| folder.fold_attribute(x)),
11901191
node: match node {
11911192
ForeignItemFn(fdec, generics) => {
1192-
ForeignItemFn(fdec.map(|FnDecl {inputs, output, variadic}| FnDecl {
1193-
inputs: inputs.move_map(|a| folder.fold_arg(a)),
1194-
output: match output {
1195-
Return(ty) => Return(folder.fold_ty(ty)),
1196-
NoReturn(span) => NoReturn(span)
1197-
},
1198-
variadic: variadic
1199-
}), folder.fold_generics(generics))
1193+
ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
12001194
}
12011195
ForeignItemStatic(t, m) => {
12021196
ForeignItemStatic(folder.fold_ty(t), m)

Diff for: src/libsyntax/parse/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,7 @@ mod test {
10661066
}),
10671067
id: ast::DUMMY_NODE_ID
10681068
}),
1069-
output: ast::Return(P(ast::Ty{id: ast::DUMMY_NODE_ID,
1070-
node: ast::TyTup(vec![]),
1071-
span:sp(15,15)})), // not sure
1069+
output: ast::DefaultReturn(sp(15, 15)),
10721070
variadic: false
10731071
}),
10741072
ast::Unsafety::Normal,

Diff for: src/libsyntax/parse/parser.rs

+5-24
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use ast::{Mod, BiAdd, Arg, Arm, Attribute, BindByRef, BindByValue};
1919
use ast::{BiBitAnd, BiBitOr, BiBitXor, BiRem, BiLt, BiGt, Block};
2020
use ast::{BlockCheckMode, CaptureByRef, CaptureByValue, CaptureClause};
2121
use ast::{Crate, CrateConfig, Decl, DeclItem};
22-
use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf};
22+
use ast::{DeclLocal, DefaultBlock, DefaultReturn};
23+
use ast::{UnDeref, BiDiv, EMPTY_CTXT, EnumDef, ExplicitSelf};
2324
use ast::{Expr, Expr_, ExprAddrOf, ExprMatch, ExprAgain};
2425
use ast::{ExprAssign, ExprAssignOp, ExprBinary, ExprBlock, ExprBox};
2526
use ast::{ExprBreak, ExprCall, ExprCast};
@@ -1426,11 +1427,7 @@ impl<'a> Parser<'a> {
14261427
}
14271428
} else {
14281429
let pos = self.span.lo;
1429-
Return(P(Ty {
1430-
id: ast::DUMMY_NODE_ID,
1431-
node: TyTup(vec![]),
1432-
span: mk_sp(pos, pos),
1433-
}))
1430+
DefaultReturn(mk_sp(pos, pos))
14341431
}
14351432
}
14361433

@@ -4548,15 +4545,7 @@ impl<'a> Parser<'a> {
45484545
(optional_unboxed_closure_kind, args)
45494546
}
45504547
};
4551-
let output = if self.check(&token::RArrow) {
4552-
self.parse_ret_ty()
4553-
} else {
4554-
Return(P(Ty {
4555-
id: ast::DUMMY_NODE_ID,
4556-
node: TyInfer,
4557-
span: self.span,
4558-
}))
4559-
};
4548+
let output = self.parse_ret_ty();
45604549

45614550
(P(FnDecl {
45624551
inputs: inputs_captures,
@@ -4573,15 +4562,7 @@ impl<'a> Parser<'a> {
45734562
seq_sep_trailing_allowed(token::Comma),
45744563
|p| p.parse_fn_block_arg());
45754564

4576-
let output = if self.check(&token::RArrow) {
4577-
self.parse_ret_ty()
4578-
} else {
4579-
Return(P(Ty {
4580-
id: ast::DUMMY_NODE_ID,
4581-
node: TyInfer,
4582-
span: self.span,
4583-
}))
4584-
};
4565+
let output = self.parse_ret_ty();
45854566

45864567
P(FnDecl {
45874568
inputs: inputs,

Diff for: src/libsyntax/print/pprust.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -2351,10 +2351,8 @@ impl<'a> State<'a> {
23512351
try!(self.print_fn_args(decl, None));
23522352
try!(word(&mut self.s, "|"));
23532353

2354-
if let ast::Return(ref ty) = decl.output {
2355-
if ty.node == ast::TyInfer {
2356-
return self.maybe_print_comment(ty.span.lo);
2357-
}
2354+
if let ast::DefaultReturn(..) = decl.output {
2355+
return Ok(());
23582356
}
23592357

23602358
try!(self.space_if_not_bol());
@@ -2364,6 +2362,7 @@ impl<'a> State<'a> {
23642362
try!(self.print_type(&**ty));
23652363
self.maybe_print_comment(ty.span.lo)
23662364
}
2365+
ast::DefaultReturn(..) => unreachable!(),
23672366
ast::NoReturn(span) => {
23682367
try!(self.word_nbsp("!"));
23692368
self.maybe_print_comment(span.lo)
@@ -2385,10 +2384,8 @@ impl<'a> State<'a> {
23852384
try!(self.print_fn_args(decl, None));
23862385
try!(word(&mut self.s, ")"));
23872386

2388-
if let ast::Return(ref ty) = decl.output {
2389-
if ty.node == ast::TyInfer {
2390-
return self.maybe_print_comment(ty.span.lo);
2391-
}
2387+
if let ast::DefaultReturn(..) = decl.output {
2388+
return Ok(());
23922389
}
23932390

23942391
try!(self.space_if_not_bol());
@@ -2398,6 +2395,7 @@ impl<'a> State<'a> {
23982395
try!(self.print_type(&**ty));
23992396
self.maybe_print_comment(ty.span.lo)
24002397
}
2398+
ast::DefaultReturn(..) => unreachable!(),
24012399
ast::NoReturn(span) => {
24022400
try!(self.word_nbsp("!"));
24032401
self.maybe_print_comment(span.lo)
@@ -2684,13 +2682,8 @@ impl<'a> State<'a> {
26842682
}
26852683

26862684
pub fn print_fn_output(&mut self, decl: &ast::FnDecl) -> IoResult<()> {
2687-
if let ast::Return(ref ty) = decl.output {
2688-
match ty.node {
2689-
ast::TyTup(ref tys) if tys.is_empty() => {
2690-
return self.maybe_print_comment(ty.span.lo);
2691-
}
2692-
_ => ()
2693-
}
2685+
if let ast::DefaultReturn(..) = decl.output {
2686+
return Ok(());
26942687
}
26952688

26962689
try!(self.space_if_not_bol());
@@ -2699,6 +2692,7 @@ impl<'a> State<'a> {
26992692
match decl.output {
27002693
ast::NoReturn(_) =>
27012694
try!(self.word_nbsp("!")),
2695+
ast::DefaultReturn(..) => unreachable!(),
27022696
ast::Return(ref ty) =>
27032697
try!(self.print_type(&**ty))
27042698
}
@@ -3071,9 +3065,7 @@ mod test {
30713065

30723066
let decl = ast::FnDecl {
30733067
inputs: Vec::new(),
3074-
output: ast::Return(P(ast::Ty {id: 0,
3075-
node: ast::TyTup(vec![]),
3076-
span: codemap::DUMMY_SP})),
3068+
output: ast::DefaultReturn(codemap::DUMMY_SP),
30773069
variadic: false
30783070
};
30793071
let generics = ast_util::empty_generics();

Diff for: src/libsyntax/test.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,8 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
297297
match &i.node {
298298
&ast::ItemFn(ref decl, _, _, ref generics, _) => {
299299
let no_output = match decl.output {
300-
ast::Return(ref ret_ty) => match ret_ty.node {
301-
ast::TyTup(ref tys) if tys.is_empty() => true,
302-
_ => false,
303-
},
304-
ast::NoReturn(_) => false
300+
ast::DefaultReturn(..) => true,
301+
_ => false
305302
};
306303
if decl.inputs.is_empty()
307304
&& no_output
@@ -336,11 +333,8 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
336333
ast::ItemFn(ref decl, _, _, ref generics, _) => {
337334
let input_cnt = decl.inputs.len();
338335
let no_output = match decl.output {
339-
ast::Return(ref ret_ty) => match ret_ty.node {
340-
ast::TyTup(ref tys) if tys.is_empty() => true,
341-
_ => false,
342-
},
343-
ast::NoReturn(_) => false
336+
ast::DefaultReturn(..) => true,
337+
_ => false
344338
};
345339
let tparm_cnt = generics.ty_params.len();
346340
// NB: inadequate check, but we're running

Diff for: src/test/pretty/fn-return.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// pp-exact
12+
13+
// Check that `fn f() -> () { }` does not print as `fn f() { }`.
14+
15+
fn f() -> () { }
16+
17+
fn main() { }

0 commit comments

Comments
 (0)