提交 a8419099 编写于 作者: B Bastian Kauschke

InstanceDef::Item

上级 178c6507
......@@ -249,9 +249,9 @@ fn exported_symbols_provider_local(
}
match *mono_item {
MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) => {
MonoItem::Fn(Instance { def: InstanceDef::Item(def), substs }) => {
if substs.non_erasable_generics().next().is_some() {
let symbol = ExportedSymbol::Generic(def_id, substs);
let symbol = ExportedSymbol::Generic(def.did, substs);
symbols.push((symbol, SymbolExportLevel::Rust));
}
}
......
......@@ -346,8 +346,8 @@ fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey {
// instances into account. The others don't matter for
// the codegen tests and can even make item order
// unstable.
InstanceDef::Item(def_id) => {
def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id))
InstanceDef::Item(def) => {
def.did.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id))
}
InstanceDef::VtableShim(..)
| InstanceDef::ReifyShim(..)
......
......@@ -29,7 +29,7 @@ pub enum InstanceDef<'tcx> {
/// - `fn` items
/// - closures
/// - generators
Item(DefId),
Item(ty::WithOptParam<DefId>),
/// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI).
///
......@@ -160,8 +160,8 @@ pub fn upstream_monomorphization(&self, tcx: TyCtxt<'tcx>) -> Option<CrateNum> {
self.substs.non_erasable_generics().next()?;
match self.def {
InstanceDef::Item(def_id) => tcx
.upstream_monomorphizations_for(def_id)
InstanceDef::Item(def) => tcx
.upstream_monomorphizations_for(def.did)
.and_then(|monos| monos.get(&self.substs).cloned()),
InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs),
_ => None,
......@@ -171,10 +171,10 @@ pub fn upstream_monomorphization(&self, tcx: TyCtxt<'tcx>) -> Option<CrateNum> {
impl<'tcx> InstanceDef<'tcx> {
#[inline]
pub fn def_id(&self) -> DefId {
match *self {
InstanceDef::Item(def_id)
| InstanceDef::VtableShim(def_id)
pub fn def_id(self) -> DefId {
match self {
InstanceDef::Item(def) => def.did,
InstanceDef::VtableShim(def_id)
| InstanceDef::ReifyShim(def_id)
| InstanceDef::FnPtrShim(def_id, _)
| InstanceDef::Virtual(def_id, _)
......@@ -185,6 +185,21 @@ pub fn def_id(&self) -> DefId {
}
}
#[inline]
pub fn with_opt_param(self) -> ty::WithOptParam<DefId> {
match self {
InstanceDef::Item(def) => def,
InstanceDef::VtableShim(def_id)
| InstanceDef::ReifyShim(def_id)
| InstanceDef::FnPtrShim(def_id, _)
| InstanceDef::Virtual(def_id, _)
| InstanceDef::Intrinsic(def_id)
| InstanceDef::ClosureOnceShim { call_once: def_id }
| InstanceDef::DropGlue(def_id, _)
| InstanceDef::CloneShim(def_id, _) => ty::WithOptParam::dummy(def_id),
}
}
#[inline]
pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
tcx.get_attrs(self.def_id())
......@@ -198,7 +213,7 @@ pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
use rustc_hir::definitions::DefPathData;
let def_id = match *self {
ty::InstanceDef::Item(def_id) => def_id,
ty::InstanceDef::Item(def) => def.did,
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
_ => return true,
};
......@@ -244,8 +259,8 @@ pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
match *self {
InstanceDef::Item(def_id) => {
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
InstanceDef::Item(def) => {
tcx.codegen_fn_attrs(def.did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
}
_ => false,
}
......@@ -283,7 +298,7 @@ pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> {
def_id,
substs
);
Instance { def: InstanceDef::Item(def_id), substs }
Instance { def: InstanceDef::Item(ty::WithOptParam::dummy(def_id)), substs }
}
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
......@@ -356,9 +371,9 @@ pub fn resolve_for_fn_ptr(
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
match resolved.def {
InstanceDef::Item(def_id) if resolved.def.requires_caller_location(tcx) => {
InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => {
debug!(" => fn pointer created for function with #[track_caller]");
resolved.def = InstanceDef::ReifyShim(def_id);
resolved.def = InstanceDef::ReifyShim(def.did);
}
InstanceDef::Virtual(def_id, _) => {
debug!(" => fn pointer created for virtual call");
......
......@@ -2880,7 +2880,7 @@ pub fn item_name(self, id: DefId) -> Symbol {
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
match instance {
ty::InstanceDef::Item(did) => self.optimized_mir(did),
ty::InstanceDef::Item(def) => self.optimized_mir(def.did),
ty::InstanceDef::VtableShim(..)
| ty::InstanceDef::ReifyShim(..)
| ty::InstanceDef::Intrinsic(..)
......
......@@ -838,7 +838,7 @@ fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
Self {
substs: self.substs.fold_with(folder),
def: match self.def {
Item(did) => Item(did.fold_with(folder)),
Item(def) => Item(def.fold_with(folder)),
VtableShim(did) => VtableShim(did.fold_with(folder)),
ReifyShim(did) => ReifyShim(did.fold_with(folder)),
Intrinsic(did) => Intrinsic(did.fold_with(folder)),
......@@ -857,7 +857,8 @@ fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
use crate::ty::InstanceDef::*;
self.substs.visit_with(visitor)
|| match self.def {
Item(did) | VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
Item(def) => def.visit_with(visitor),
VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
did.visit_with(visitor)
}
FnPtrShim(did, ty) | CloneShim(did, ty) => {
......
......@@ -288,21 +288,21 @@ pub fn const_eval_raw_provider<'tcx>(
}
let cid = key.value;
let def_id = cid.instance.def.def_id();
let def = cid.instance.def.with_opt_param();
if let Some(def_id) = def_id.as_local() {
if tcx.has_typeck_tables(def_id) {
if let Some(error_reported) = tcx.typeck_tables_of(def_id).tainted_by_errors {
if let Some(def) = def.as_local() {
if tcx.has_typeck_tables(def.did) {
if let Some(error_reported) = tcx.typeck_tables_of_const_arg(def).tainted_by_errors {
return Err(ErrorHandled::Reported(error_reported));
}
}
}
let is_static = tcx.is_static(def_id);
let is_static = tcx.is_static(def.did);
let mut ecx = InterpCx::new(
tcx,
tcx.def_span(cid.instance.def_id()),
tcx.def_span(def.did),
key.param_env,
CompileTimeInterpreter::new(tcx.sess.const_eval_limit()),
MemoryExtra { can_access_statics: is_static },
......@@ -334,9 +334,9 @@ pub fn const_eval_raw_provider<'tcx>(
}
v
} else if let Some(def_id) = def_id.as_local() {
} else if let Some(def) = def.as_local() {
// constant defined in this crate, we can figure out a lint level!
match tcx.def_kind(def_id.to_def_id()) {
match tcx.def_kind(def.did.to_def_id()) {
// constants never produce a hard error at the definition site. Anything else is
// a backwards compatibility hazard (and will break old versions of winapi for
// sure)
......@@ -346,9 +346,9 @@ pub fn const_eval_raw_provider<'tcx>(
// validation thus preventing such a hard error from being a backwards
// compatibility hazard
DefKind::Const | DefKind::AssocConst => {
let hir_id = tcx.hir().as_local_hir_id(def_id);
let hir_id = tcx.hir().as_local_hir_id(def.did);
err.report_as_lint(
tcx.at(tcx.def_span(def_id)),
tcx.at(tcx.def_span(def.did)),
"any use of this value will cause an error",
hir_id,
Some(err.span),
......@@ -359,7 +359,7 @@ pub fn const_eval_raw_provider<'tcx>(
// deny-by-default lint
_ => {
if let Some(p) = cid.promoted {
let span = tcx.promoted_mir(def_id)[p].span;
let span = tcx.promoted_mir(def.did)[p].span;
if let err_inval!(ReferencedConstant) = err.error {
err.report_as_error(
tcx.at(span),
......@@ -369,7 +369,7 @@ pub fn const_eval_raw_provider<'tcx>(
err.report_as_lint(
tcx.at(span),
"reaching this expression at runtime will panic or abort",
tcx.hir().as_local_hir_id(def_id),
tcx.hir().as_local_hir_id(def.did),
Some(err.span),
)
}
......
......@@ -191,11 +191,11 @@ fn find_mir_or_eval_fn(
debug!("find_mir_or_eval_fn: {:?}", instance);
// Only check non-glue functions
if let ty::InstanceDef::Item(def_id) = instance.def {
if let ty::InstanceDef::Item(def) = instance.def {
// Execution might have wandered off into other crates, so we cannot do a stability-
// sensitive check here. But we can at least rule out functions that are not const
// at all.
if ecx.tcx.is_const_fn_raw(def_id) {
if ecx.tcx.is_const_fn_raw(def.did) {
// If this function is a `const fn` then under certain circumstances we
// can evaluate call via the query system, thus memoizing all future calls.
if ecx.try_eval_const_fn_call(instance, ret, args)? {
......
......@@ -394,24 +394,26 @@ pub fn load_mir(
promoted: Option<mir::Promoted>,
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
// do not continue if typeck errors occurred (can only occur in local crate)
let did = instance.def_id();
if let Some(did) = did.as_local() {
if self.tcx.has_typeck_tables(did) {
if let Some(error_reported) = self.tcx.typeck_tables_of(did).tainted_by_errors {
let def = instance.with_opt_param();
if let Some(def) = def.as_local() {
if self.tcx.has_typeck_tables(def.did) {
if let Some(error_reported) =
self.tcx.typeck_tables_of_const_arg(def).tainted_by_errors
{
throw_inval!(TypeckError(error_reported))
}
}
}
trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
if let Some(promoted) = promoted {
return Ok(&self.tcx.promoted_mir(did)[promoted]);
return Ok(&self.tcx.promoted_mir(def.did)[promoted]);
}
match instance {
ty::InstanceDef::Item(def_id) => {
if self.tcx.is_mir_available(did) {
Ok(self.tcx.optimized_mir(did))
ty::InstanceDef::Item(def) => {
if self.tcx.is_mir_available(def.did) {
Ok(self.tcx.optimized_mir(def.did))
} else {
throw_unsup!(NoMirFor(def_id))
throw_unsup!(NoMirFor(def.did))
}
}
_ => Ok(self.tcx.instance_mir(instance)),
......
......@@ -768,8 +768,8 @@ fn visit_instance_use<'tcx>(
// need a mono item.
fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) -> bool {
let def_id = match instance.def {
ty::InstanceDef::Item(def_id) | ty::InstanceDef::DropGlue(def_id, Some(_)) => def_id,
ty::InstanceDef::Item(def) => def.did,
ty::InstanceDef::DropGlue(def_id, Some(_)) => def_id,
ty::InstanceDef::VtableShim(..)
| ty::InstanceDef::ReifyShim(..)
| ty::InstanceDef::ClosureOnceShim { .. }
......
......@@ -314,7 +314,8 @@ fn mono_item_visibility(
};
let def_id = match instance.def {
InstanceDef::Item(def_id) | InstanceDef::DropGlue(def_id, Some(_)) => def_id,
InstanceDef::Item(def) => def.did,
InstanceDef::DropGlue(def_id, Some(_)) => def_id,
// These are all compiler glue and such, never exported, always hidden.
InstanceDef::VtableShim(..)
......@@ -704,7 +705,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
match mono_item {
MonoItem::Fn(instance) => {
let def_id = match instance.def {
ty::InstanceDef::Item(def_id) => def_id,
ty::InstanceDef::Item(def) => def.did,
ty::InstanceDef::VtableShim(..)
| ty::InstanceDef::ReifyShim(..)
| ty::InstanceDef::FnPtrShim(..)
......
......@@ -520,8 +520,8 @@ fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs);
debug!("Resolving ({:?}) -> {:?}", def_id, instance);
if let Ok(Some(func)) = instance {
if let InstanceDef::Item(def_id) = func.def {
if is_const_fn(self.tcx, def_id) {
if let InstanceDef::Item(def) = func.def {
if is_const_fn(self.tcx, def.did) {
return;
}
}
......
......@@ -9,7 +9,7 @@
use rustc_middle::mir::{traversal, Body, ConstQualifs, MirPhase, Promoted};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::steal::Steal;
use rustc_middle::ty::{InstanceDef, TyCtxt, TypeFoldable};
use rustc_middle::ty::{self, InstanceDef, TyCtxt, TypeFoldable};
use rustc_span::{Span, Symbol};
use std::borrow::Cow;
......@@ -116,7 +116,7 @@ pub struct MirSource<'tcx> {
impl<'tcx> MirSource<'tcx> {
pub fn item(def_id: DefId) -> Self {
MirSource { instance: InstanceDef::Item(def_id), promoted: None }
MirSource { instance: InstanceDef::Item(ty::WithOptParam::dummy(def_id)), promoted: None }
}
#[inline]
......@@ -249,7 +249,7 @@ fn mir_const(tcx: TyCtxt<'_>, def_id: DefId) -> Steal<Body<'_>> {
run_passes(
tcx,
&mut body,
InstanceDef::Item(def_id.to_def_id()),
InstanceDef::Item(ty::WithOptParam::dummy(def_id.to_def_id())),
None,
MirPhase::Const,
&[&[
......@@ -284,7 +284,7 @@ fn mir_validated(
run_passes(
tcx,
&mut body,
InstanceDef::Item(def_id.to_def_id()),
InstanceDef::Item(ty::WithOptParam::dummy(def_id.to_def_id())),
None,
MirPhase::Validated,
&[&[
......@@ -350,7 +350,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(
run_passes(
tcx,
body,
InstanceDef::Item(def_id.to_def_id()),
InstanceDef::Item(ty::WithOptParam::dummy(def_id.to_def_id())),
promoted,
MirPhase::DropElab,
&[post_borrowck_cleanup],
......@@ -414,7 +414,7 @@ fn run_optimization_passes<'tcx>(
run_passes(
tcx,
body,
InstanceDef::Item(def_id.to_def_id()),
InstanceDef::Item(ty::WithOptParam::dummy(def_id.to_def_id())),
promoted,
MirPhase::Optimized,
&[
......
......@@ -248,7 +248,10 @@ pub fn write_mir_pretty<'tcx>(
for (i, body) in tcx.promoted_mir(def_id).iter_enumerated() {
writeln!(w)?;
let src = MirSource { instance: ty::InstanceDef::Item(def_id), promoted: Some(i) };
let src = MirSource {
instance: ty::InstanceDef::Item(ty::WithOptParam::dummy(def_id)),
promoted: Some(i),
};
write_mir_fn(tcx, src, body, &mut |_, _| Ok(()), w)?;
}
}
......
......@@ -86,7 +86,7 @@ fn inner_resolve_instance<'tcx>(
}
_ => {
debug!(" => free item");
ty::InstanceDef::Item(def.did)
ty::InstanceDef::Item(def)
}
};
Ok(Some(Instance { def, substs }))
......@@ -215,7 +215,7 @@ fn resolve_associated_item<'tcx>(
Some(ty::Instance::new(leaf_def.item.def_id, substs))
}
traits::ImplSourceGenerator(generator_data) => Some(Instance {
def: ty::InstanceDef::Item(generator_data.generator_def_id),
def: ty::InstanceDef::Item(ty::WithOptParam::dummy(generator_data.generator_def_id)),
substs: generator_data.substs,
}),
traits::ImplSourceClosure(closure_data) => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册