Skip to content

Commit bc41cc0

Browse files
committed
Make unboxed_closure_kind return Option to allow for the possibility that its value is not yet known.
1 parent 53c1956 commit bc41cc0

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

src/librustc/middle/mem_categorization.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,16 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
594594
let ty = try!(self.node_ty(fn_node_id));
595595
match ty.sty {
596596
ty::ty_closure(closure_id, _, _) => {
597-
let kind = self.typer.closure_kind(closure_id);
598-
self.cat_upvar(id, span, var_id, fn_node_id, kind)
597+
match self.typer.closure_kind(closure_id) {
598+
Some(kind) => {
599+
self.cat_upvar(id, span, var_id, fn_node_id, kind)
600+
}
601+
None => {
602+
self.tcx().sess.span_bug(
603+
span,
604+
&*format!("No closure kind for {:?}", closure_id));
605+
}
606+
}
599607
}
600608
_ => {
601609
self.tcx().sess.span_bug(

src/librustc/middle/traits/select.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10241024
kind,
10251025
obligation.repr(self.tcx()));
10261026

1027-
let closure_kind = self.closure_typer.closure_kind(closure_def_id);
1028-
1029-
debug!("closure_kind = {:?}", closure_kind);
1030-
1031-
if closure_kind == kind {
1032-
candidates.vec.push(ClosureCandidate(closure_def_id, substs.clone()));
1027+
match self.closure_typer.closure_kind(closure_def_id) {
1028+
Some(closure_kind) => {
1029+
debug!("assemble_unboxed_candidates: closure_kind = {:?}", closure_kind);
1030+
if closure_kind == kind {
1031+
candidates.vec.push(ClosureCandidate(closure_def_id, substs.clone()));
1032+
}
1033+
}
1034+
None => {
1035+
debug!("assemble_unboxed_candidates: closure_kind not yet known");
1036+
candidates.ambiguous = true;
1037+
}
10331038
}
10341039

10351040
Ok(())

src/librustc/middle/ty.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,14 +2288,22 @@ impl ClosureKind {
22882288
pub trait ClosureTyper<'tcx> {
22892289
fn param_env<'a>(&'a self) -> &'a ty::ParameterEnvironment<'a, 'tcx>;
22902290

2291-
fn closure_kind(&self, def_id: ast::DefId) -> ty::ClosureKind;
2292-
2291+
/// Is this a `Fn`, `FnMut` or `FnOnce` closure? During typeck,
2292+
/// returns `None` if the kind of this closure has not yet been
2293+
/// inferred.
2294+
fn closure_kind(&self,
2295+
def_id: ast::DefId)
2296+
-> Option<ty::ClosureKind>;
2297+
2298+
/// Returns the argument/return types of this closure.
22932299
fn closure_type(&self,
22942300
def_id: ast::DefId,
22952301
substs: &subst::Substs<'tcx>)
22962302
-> ty::ClosureTy<'tcx>;
22972303

2298-
// Returns `None` if the upvar types cannot yet be definitively determined.
2304+
/// Returns the set of all upvars and their transformed
2305+
/// types. During typeck, maybe return `None` if the upvar types
2306+
/// have not yet been inferred.
22992307
fn closure_upvars(&self,
23002308
def_id: ast::DefId,
23012309
substs: &Substs<'tcx>)
@@ -6473,8 +6481,11 @@ impl<'a,'tcx> ClosureTyper<'tcx> for ty::ParameterEnvironment<'a,'tcx> {
64736481
self
64746482
}
64756483

6476-
fn closure_kind(&self, def_id: ast::DefId) -> ty::ClosureKind {
6477-
self.tcx.closure_kind(def_id)
6484+
fn closure_kind(&self,
6485+
def_id: ast::DefId)
6486+
-> Option<ty::ClosureKind>
6487+
{
6488+
Some(self.tcx.closure_kind(def_id))
64786489
}
64796490

64806491
fn closure_type(&self,

src/librustc_trans/trans/common.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,10 @@ impl<'blk, 'tcx> ty::ClosureTyper<'tcx> for BlockS<'blk, 'tcx> {
693693
&self.fcx.param_env
694694
}
695695

696-
fn closure_kind(&self, def_id: ast::DefId) -> ty::ClosureKind {
696+
fn closure_kind(&self,
697+
def_id: ast::DefId)
698+
-> Option<ty::ClosureKind>
699+
{
697700
let typer = NormalizingClosureTyper::new(self.tcx());
698701
typer.closure_kind(def_id)
699702
}
@@ -1065,8 +1068,11 @@ impl<'a,'tcx> ty::ClosureTyper<'tcx> for NormalizingClosureTyper<'a,'tcx> {
10651068
&self.param_env
10661069
}
10671070

1068-
fn closure_kind(&self, def_id: ast::DefId) -> ty::ClosureKind {
1069-
self.param_env.tcx.closure_kind(def_id)
1071+
fn closure_kind(&self,
1072+
def_id: ast::DefId)
1073+
-> Option<ty::ClosureKind>
1074+
{
1075+
self.param_env.closure_kind(def_id)
10701076
}
10711077

10721078
fn closure_type(&self,

src/librustc_typeck/check/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,11 @@ impl<'a, 'tcx> ty::ClosureTyper<'tcx> for FnCtxt<'a, 'tcx> {
348348
&self.inh.param_env
349349
}
350350

351-
fn closure_kind(&self, def_id: ast::DefId) -> ty::ClosureKind {
352-
self.inh.closures.borrow()[def_id].kind
351+
fn closure_kind(&self,
352+
def_id: ast::DefId)
353+
-> Option<ty::ClosureKind>
354+
{
355+
Some(self.inh.closures.borrow()[def_id].kind)
353356
}
354357

355358
fn closure_type(&self,

0 commit comments

Comments
 (0)