1
Fork 0

Move common.rs functionality into TyCtxt

This commit is contained in:
Maik Klein 2017-10-25 17:27:27 +02:00 committed by Ariel Ben-Yehuda
parent 98b9eba316
commit 531c27d805
7 changed files with 14 additions and 103 deletions

View file

@ -45,6 +45,15 @@ pub enum InstanceDef<'tcx> {
CloneShim(DefId, Ty<'tcx>),
}
impl<'a, 'tcx> Instance<'tcx> {
pub fn ty(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> Ty<'tcx>
{
self.def.def_ty(tcx).subst(tcx, self.substs)
}
}
impl<'tcx> InstanceDef<'tcx> {
#[inline]
pub fn def_id(&self) -> DefId {

View file

@ -2471,11 +2471,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
pub fn is_sized(self, ty: Ty<'tcx>) -> bool {
ty.is_sized(self, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
}
}
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn with_freevars<T, F>(self, fid: NodeId, f: F) -> T where
F: FnOnce(&[hir::Freevar]) -> T,

View file

@ -203,7 +203,6 @@ use rustc::ty::adjustment::CustomCoerceUnsized;
use rustc::mir::{self, Location};
use rustc::mir::visit::Visitor as MirVisitor;
use common::{def_ty, instance_ty, type_has_metadata};
use monomorphize::{self, Instance};
use rustc::util::nodemap::{FxHashSet, FxHashMap, DefIdMap};
@ -371,7 +370,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// Sanity check whether this ended up being collected accidentally
debug_assert!(should_trans_locally(tcx, &instance));
let ty = instance_ty(tcx, &instance);
let ty = instance.ty(tcx);
visit_drop_use(tcx, ty, true, &mut neighbors);
recursion_depth_reset = None;
@ -916,7 +915,7 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
debug!("RootCollector: ADT drop-glue for {}",
def_id_to_string(self.tcx, def_id));
let ty = def_ty(self.tcx, def_id, Substs::empty());
let ty = Instance::new(def_id, Substs::empty()).ty(self.tcx);
visit_drop_use(self.tcx, ty, true, self.output);
}
}

View file

@ -14,7 +14,6 @@
//! item-path. This is used for unit testing the code that generates
//! paths etc in all kinds of annoying scenarios.
use rustc_trans_utils::common;
use monomorphize::Instance;
use rustc::hir;
use rustc::hir::def_id::DefId;
@ -98,7 +97,7 @@ pub trait TransItemExt<'a, 'tcx>: fmt::Debug {
// If this function isn't inlined or otherwise has explicit
// linkage, then we'll be creating a globally shared version.
if self.explicit_linkage(tcx).is_some() ||
!common::requests_inline(tcx, instance)
!tcx.requires_local_instance(instance)
{
return InstantiationMode::GloballyShared { may_conflict: false }
}

View file

@ -1,92 +0,0 @@
// Copyright 2012-2014 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.
#![allow(non_camel_case_types, non_snake_case)]
//! Code that is useful in various trans modules.
use rustc::hir::def_id::DefId;
use rustc::hir::map::DefPathData;
use rustc::traits;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
use syntax::attr;
use syntax_pos::DUMMY_SP;
pub fn type_is_sized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
ty.is_sized(tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
}
pub fn type_has_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
if type_is_sized(tcx, ty) {
return false;
}
let tail = tcx.struct_tail(ty);
match tail.sty {
ty::TyForeign(..) => false,
ty::TyStr | ty::TySlice(..) | ty::TyDynamic(..) => true,
_ => bug!("unexpected unsized tail: {:?}", tail.sty),
}
}
pub fn requests_inline<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
instance: &ty::Instance<'tcx>
) -> bool {
if is_inline_instance(tcx, instance) {
return true
}
if let ty::InstanceDef::DropGlue(..) = instance.def {
// Drop glue wants to be instantiated at every translation
// unit, but without an #[inline] hint. We should make this
// available to normal end-users.
return true
}
attr::requests_inline(&instance.def.attrs(tcx)[..]) ||
tcx.is_const_fn(instance.def.def_id())
}
pub fn is_inline_instance<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
instance: &ty::Instance<'tcx>
) -> bool {
let def_id = match instance.def {
ty::InstanceDef::Item(def_id) => def_id,
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
_ => return true
};
match tcx.def_key(def_id).disambiguated_data.data {
DefPathData::StructCtor |
DefPathData::EnumVariant(..) |
DefPathData::ClosureExpr => true,
_ => false
}
}
/// Given a DefId and some Substs, produces the monomorphic item type.
pub fn def_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
substs: &'tcx Substs<'tcx>)
-> Ty<'tcx>
{
let ty = tcx.type_of(def_id);
tcx.trans_apply_param_substs(substs, &ty)
}
/// Return the substituted type of an instance.
pub fn instance_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
instance: &ty::Instance<'tcx>)
-> Ty<'tcx>
{
let ty = instance.def.def_ty(tcx);
tcx.trans_apply_param_substs(instance.substs, &ty)
}

View file

@ -44,7 +44,8 @@ use rustc::hir::def_id::LOCAL_CRATE;
use rustc::hir::map as hir_map;
use rustc::util::nodemap::NodeSet;
pub mod common;
use syntax::attr;
pub mod link;
pub mod trans_crate;