Skip to content

Commit 8232734

Browse files
committed
Fall back to main -> () when termination trait language item is not enabled
1 parent f842f75 commit 8232734

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,20 +1066,25 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
10661066
}
10671067
fcx.demand_suptype(span, ret_ty, actual_return_ty);
10681068

1069-
if let Some((id, _)) = *fcx.tcx.sess.entry_fn.borrow() {
1070-
if id == fn_id {
1071-
match fcx.sess().entry_type.get() {
1072-
Some(config::EntryMain) => {
1073-
let term_id = fcx.tcx.require_lang_item(TerminationTraitLangItem);
1074-
1075-
let substs = fcx.tcx.mk_substs(iter::once(Kind::from(ret_ty)));
1076-
let trait_ref = ty::TraitRef::new(term_id, substs);
1077-
let cause = traits::ObligationCause::new(span, fn_id,
1078-
ObligationCauseCode::MainFunctionType);
1079-
inherited.register_predicate(
1080-
traits::Obligation::new(cause, param_env, trait_ref.to_predicate()));
1081-
},
1082-
_ => {},
1069+
// If the termination trait language item is activated, check that the main return type
1070+
// implements the termination trait.
1071+
if fcx.tcx.lang_items().termination().is_some() {
1072+
if let Some((id, _)) = *fcx.tcx.sess.entry_fn.borrow() {
1073+
if id == fn_id {
1074+
match fcx.sess().entry_type.get() {
1075+
Some(config::EntryMain) => {
1076+
let term_id = fcx.tcx.require_lang_item(TerminationTraitLangItem);
1077+
1078+
let substs = fcx.tcx.mk_substs(iter::once(Kind::from(ret_ty)));
1079+
let trait_ref = ty::TraitRef::new(term_id, substs);
1080+
let cause = traits::ObligationCause::new(
1081+
span, fn_id, ObligationCauseCode::MainFunctionType);
1082+
1083+
inherited.register_predicate(
1084+
traits::Obligation::new(cause, param_env, trait_ref.to_predicate()));
1085+
},
1086+
_ => {},
1087+
}
10831088
}
10841089
}
10851090
}

src/librustc_typeck/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,19 @@ fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
203203
}
204204

205205
let actual = tcx.fn_sig(main_def_id);
206+
let expected_return_type = if tcx.lang_items().termination().is_some() {
207+
// we take the return type of the given main function, the real check is done
208+
// in `check_fn`
209+
actual.output().skip_binder()
210+
} else {
211+
// standard () main return type
212+
tcx.mk_nil()
213+
};
206214

207215
let se_ty = tcx.mk_fn_ptr(ty::Binder(
208216
tcx.mk_fn_sig(
209217
iter::empty(),
210-
// the return type is checked in `check_fn`
211-
actual.output().skip_binder(),
218+
expected_return_type,
212219
false,
213220
hir::Unsafety::Normal,
214221
Abi::Rust

0 commit comments

Comments
 (0)