Skip to content

Commit 135cac8

Browse files
committed
Auto merge of #21099 - sanxiyn:opt-return-ty, r=alexcrichton
This avoids having ast::Ty nodes which have no counterpart in the source.
2 parents bd8a43c + 3f0cc80 commit 135cac8

File tree

14 files changed

+76
-85
lines changed

14 files changed

+76
-85
lines changed

src/librustc/middle/infer/error_reporting.rs

Lines changed: 1 addition & 0 deletions
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
}

src/librustc_trans/trans/debuginfo.rs

Lines changed: 9 additions & 12 deletions
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

src/librustc_trans/trans/foreign.rs

Lines changed: 2 additions & 3 deletions
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
}

src/librustc_typeck/astconv.rs

Lines changed: 13 additions & 5 deletions
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()));

src/librustc_typeck/collect.rs

Lines changed: 3 additions & 1 deletion
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

src/librustdoc/clean/mod.rs

Lines changed: 3 additions & 1 deletion
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
}

src/librustdoc/html/format.rs

Lines changed: 1 addition & 0 deletions
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
}

src/libsyntax/ast.rs

Lines changed: 5 additions & 0 deletions
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
}

src/libsyntax/fold.rs

Lines changed: 2 additions & 8 deletions
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)

src/libsyntax/parse/mod.rs

Lines changed: 1 addition & 3 deletions
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,

0 commit comments

Comments
 (0)