1
Fork 0

Add TyCtxt::is_closure

This commit is contained in:
Shotaro Yamada 2017-11-11 02:20:53 +09:00
parent c3ec175857
commit ec2ff8f734
3 changed files with 11 additions and 9 deletions

View file

@ -621,9 +621,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
result result
} }
pub fn is_closure(self, def_id: DefId) -> bool {
self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr
}
pub fn closure_base_def_id(self, def_id: DefId) -> DefId { pub fn closure_base_def_id(self, def_id: DefId) -> DefId {
let mut def_id = def_id; let mut def_id = def_id;
while self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr { while self.is_closure(def_id) {
def_id = self.parent_def_id(def_id).unwrap_or_else(|| { def_id = self.parent_def_id(def_id).unwrap_or_else(|| {
bug!("closure {:?} has no parent", def_id); bug!("closure {:?} has no parent", def_id);
}); });

View file

@ -15,7 +15,6 @@ use rustc::ty::maps::Providers;
use rustc::ty::{self, TyCtxt}; use rustc::ty::{self, TyCtxt};
use rustc::hir; use rustc::hir;
use rustc::hir::def_id::DefId; use rustc::hir::def_id::DefId;
use rustc::hir::map::DefPathData;
use rustc::lint::builtin::{SAFE_EXTERN_STATICS, UNUSED_UNSAFE}; use rustc::lint::builtin::{SAFE_EXTERN_STATICS, UNUSED_UNSAFE};
use rustc::mir::*; use rustc::mir::*;
use rustc::mir::visit::{LvalueContext, Visitor}; use rustc::mir::visit::{LvalueContext, Visitor};
@ -362,11 +361,11 @@ fn report_unused_unsafe(tcx: TyCtxt, used_unsafe: &FxHashSet<ast::NodeId>, id: a
pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) { pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
debug!("check_unsafety({:?})", def_id); debug!("check_unsafety({:?})", def_id);
match tcx.def_key(def_id).disambiguated_data.data {
// closures are handled by their parent fn. // closures are handled by their parent fn.
DefPathData::ClosureExpr => return, if tcx.is_closure(def_id) {
_ => {} return;
}; }
let UnsafetyCheckResult { let UnsafetyCheckResult {
violations, violations,

View file

@ -20,7 +20,6 @@ use rustc::mir::transform::{MirPass, MirSource};
use rustc::mir::visit::*; use rustc::mir::visit::*;
use rustc::ty::{self, Instance, Ty, TyCtxt, TypeFoldable}; use rustc::ty::{self, Instance, Ty, TyCtxt, TypeFoldable};
use rustc::ty::subst::{Subst,Substs}; use rustc::ty::subst::{Subst,Substs};
use rustc::hir::map::definitions::DefPathData;
use std::collections::VecDeque; use std::collections::VecDeque;
use super::simplify::{remove_dead_blocks, CfgSimplifier}; use super::simplify::{remove_dead_blocks, CfgSimplifier};
@ -561,7 +560,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
// A closure is passed its self-type and a tuple like `(arg1, arg2, ...)`, // A closure is passed its self-type and a tuple like `(arg1, arg2, ...)`,
// hence mappings to tuple fields are needed. // hence mappings to tuple fields are needed.
if tcx.def_key(callsite.callee).disambiguated_data.data == DefPathData::ClosureExpr { if tcx.is_closure(callsite.callee) {
let mut args = args.into_iter(); let mut args = args.into_iter();
let self_ = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_mir); let self_ = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_mir);
let tuple = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_mir); let tuple = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_mir);