Skip to content

Commit

Permalink
Fix diverging closures
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Apr 6, 2015
1 parent 1169693 commit e78f631
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/librustc/middle/traits/util.rs
Expand Up @@ -506,7 +506,7 @@ pub fn closure_trait_ref_and_return_type<'tcx>(
def_id: fn_trait_def_id,
substs: tcx.mk_substs(trait_substs),
});
ty::Binder((trait_ref, sig.0.output.unwrap()))
ty::Binder((trait_ref, sig.0.output.unwrap_or(ty::mk_nil(tcx))))
}

impl<'tcx,O:Repr<'tcx>> Repr<'tcx> for super::Obligation<'tcx, O> {
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/middle/ty.rs
Expand Up @@ -1092,6 +1092,13 @@ impl<'tcx> FnOutput<'tcx> {
ty::FnDiverging => unreachable!()
}
}

pub fn unwrap_or(self, def: Ty<'tcx>) -> Ty<'tcx> {
match self {
ty::FnConverging(t) => t,
ty::FnDiverging => def
}
}
}

pub type PolyFnOutput<'tcx> = Binder<FnOutput<'tcx>>;
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_typeck/check/callee.rs
Expand Up @@ -387,10 +387,11 @@ impl<'tcx> DeferredCallResolution<'tcx> for CallResolution<'tcx> {
demand::eqtype(fcx, self.call_expr.span, self_arg_ty, method_arg_ty);
}

let nilty = ty::mk_nil(fcx.tcx());
demand::eqtype(fcx,
self.call_expr.span,
method_sig.output.unwrap(),
self.fn_sig.output.unwrap());
method_sig.output.unwrap_or(nilty),
self.fn_sig.output.unwrap_or(nilty));

write_overloaded_call_method_map(fcx, self.call_expr, method_callee);
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/run-fail/diverging-closure.rs
@@ -0,0 +1,18 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern:oops

fn main() {
let func = || -> ! {
panic!("oops");
};
func();
}

0 comments on commit e78f631

Please sign in to comment.