-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement non-capturing closure to fn coercion #40025
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ use llvm::{self, ValueRef}; | |
use rustc::ty::{self, Ty}; | ||
use rustc::ty::cast::{CastTy, IntTy}; | ||
use rustc::ty::layout::Layout; | ||
use rustc::ty::subst::Substs; | ||
use rustc::mir::tcx::LvalueTy; | ||
use rustc::mir; | ||
use middle::lang_items::ExchangeMallocFnLangItem; | ||
|
@@ -190,6 +191,36 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> { | |
} | ||
} | ||
} | ||
mir::CastKind::ClosureFnPointer => { | ||
match operand.ty.sty { | ||
ty::TyClosure(def_id, substs) => { | ||
// Get the def_id for FnOnce::call_once | ||
let fn_once = bcx.tcx().lang_items.fn_once_trait().unwrap(); | ||
let call_once = bcx.tcx() | ||
.global_tcx().associated_items(fn_once) | ||
.find(|it| it.kind == ty::AssociatedKind::Method) | ||
.unwrap().def_id; | ||
// Now create its substs [Closure, Tuple] | ||
let input = bcx.tcx().closure_type(def_id, substs).sig.input(0); | ||
let substs = Substs::for_item(bcx.tcx(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
call_once, | ||
|_, _| {bug!()}, | ||
|def, _| { match def.index { | ||
0 => operand.ty.clone(), | ||
1 => input.skip_binder(), | ||
_ => bug!(), | ||
} } | ||
); | ||
|
||
OperandValue::Immediate( | ||
Callee::def(bcx.ccx, call_once, substs) | ||
.reify(bcx.ccx)) | ||
} | ||
_ => { | ||
bug!("{} cannot be cast to a fn ptr", operand.ty) | ||
} | ||
} | ||
} | ||
mir::CastKind::UnsafeFnPointer => { | ||
// this is a no-op at the LLVM level | ||
operand.val | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,13 +63,18 @@ | |
use check::FnCtxt; | ||
|
||
use rustc::hir; | ||
use rustc::hir::def_id::DefId; | ||
use rustc::infer::{Coercion, InferOk, TypeTrace}; | ||
use rustc::traits::{self, ObligationCause, ObligationCauseCode}; | ||
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow}; | ||
use rustc::ty::{self, LvaluePreference, TypeAndMut, Ty}; | ||
use rustc::ty::{self, LvaluePreference, TypeVariants, TypeAndMut, | ||
Ty, ClosureSubsts}; | ||
use rustc::ty::fold::TypeFoldable; | ||
use rustc::ty::error::TypeError; | ||
use rustc::ty::relate::RelateResult; | ||
use syntax::ast::NodeId; | ||
use syntax::abi; | ||
use syntax::feature_gate; | ||
use util::common::indent; | ||
|
||
use std::cell::RefCell; | ||
|
@@ -196,6 +201,11 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { | |
// unsafe qualifier. | ||
self.coerce_from_fn_pointer(a, a_f, b) | ||
} | ||
ty::TyClosure(def_id_a, substs_a) => { | ||
// Non-capturing closures are coercible to | ||
// function pointers | ||
self.coerce_closure_to_fn(a, def_id_a, substs_a, b) | ||
} | ||
_ => { | ||
// Otherwise, just use unification rules. | ||
self.unify_and_identity(a, b) | ||
|
@@ -551,6 +561,60 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { | |
} | ||
} | ||
|
||
fn coerce_closure_to_fn(&self, | ||
a: Ty<'tcx>, | ||
def_id_a: DefId, | ||
substs_a: ClosureSubsts<'tcx>, | ||
b: Ty<'tcx>) | ||
-> CoerceResult<'tcx> { | ||
//! Attempts to coerce from the type of a non-capturing closure | ||
//! into a function pointer. | ||
//! | ||
|
||
let b = self.shallow_resolve(b); | ||
|
||
let node_id_a :NodeId = self.tcx.hir.as_local_node_id(def_id_a).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type annotations shouldn't be needed. |
||
match b.sty { | ||
ty::TyFnPtr(_) if self.tcx.with_freevars(node_id_a, |v| v.is_empty()) => { | ||
if !self.tcx.sess.features.borrow().closure_to_fn_coercion { | ||
feature_gate::emit_feature_err(&self.tcx.sess.parse_sess, | ||
"closure_to_fn_coercion", | ||
self.cause.span, | ||
feature_gate::GateIssue::Language, | ||
feature_gate::CLOSURE_TO_FN_COERCION); | ||
return self.unify_and_identity(a, b); | ||
} | ||
// We coerce the closure, which has fn type | ||
// `extern "rust-call" fn((arg0,arg1,...)) -> _` | ||
// to | ||
// `fn(arg0,arg1,...) -> _` | ||
let sig = self.closure_type(def_id_a, substs_a).sig; | ||
let converted_sig = sig.input(0).map_bound(|v| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant |
||
let params_iter = match v.sty { | ||
TypeVariants::TyTuple(params, _) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
params.into_iter().cloned() | ||
} | ||
_ => bug!(), | ||
}; | ||
self.tcx.mk_fn_sig(params_iter, | ||
sig.output().skip_binder(), | ||
sig.variadic()) | ||
}); | ||
let fn_ty = self.tcx.mk_bare_fn(ty::BareFnTy { | ||
unsafety: hir::Unsafety::Normal, | ||
abi: abi::Abi::Rust, | ||
sig: converted_sig, | ||
}); | ||
let pointer_ty = self.tcx.mk_fn_ptr(&fn_ty); | ||
debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})", | ||
a, b, pointer_ty); | ||
self.unify_and_identity(pointer_ty, b) | ||
.map(|(ty, _)| (ty, Adjust::ClosureFnPointer)) | ||
} | ||
_ => self.unify_and_identity(a, b), | ||
} | ||
} | ||
|
||
fn coerce_unsafe_ptr(&self, | ||
a: Ty<'tcx>, | ||
b: Ty<'tcx>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2017 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. | ||
|
||
// Ensure that capturing closures are never coerced to fns | ||
// Especially interesting as non-capturing closures can be. | ||
|
||
fn main() { | ||
let mut a = 0u8; | ||
let foo :fn(u8) -> u8 = |v: u8| { a += v; a }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
//~^ ERROR mismatched types | ||
let b = 0u8; | ||
let bar :fn() -> u8 = || { b }; | ||
//~^ ERROR mismatched types | ||
let baz :fn() -> u8 = || { b } as fn() -> u8; | ||
//~^ ERROR mismatched types | ||
//~^^ ERROR non-scalar cast | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just make
Substs
by doingtcx.mk_substs([operand.ty, input.skip_binder()].iter().cloned())
IIRC.