Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit aac5ba5

Browse files
committed
resolve instances to ty::Instance directly
This removes the duplication between collector, callee, and (eventually) MIRI.
1 parent bf80fec commit aac5ba5

File tree

15 files changed

+389
-495
lines changed

15 files changed

+389
-495
lines changed

src/librustc/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use self::select::{EvaluationCache, SelectionContext, SelectionCache};
4040
pub use self::select::{MethodMatchResult, MethodMatched, MethodAmbiguous, MethodDidNotMatch};
4141
pub use self::select::{MethodMatchedData}; // intentionally don't export variants
4242
pub use self::specialize::{OverlapError, specialization_graph, specializes, translate_substs};
43-
pub use self::specialize::{SpecializesCache, find_method};
43+
pub use self::specialize::{SpecializesCache, find_associated_item};
4444
pub use self::util::elaborate_predicates;
4545
pub use self::util::supertraits;
4646
pub use self::util::Supertraits;

src/librustc/traits/specialize/mod.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ use traits::{self, Reveal, ObligationCause};
2929
use ty::{self, TyCtxt, TypeFoldable};
3030
use syntax_pos::DUMMY_SP;
3131

32-
use syntax::ast;
33-
3432
pub mod specialization_graph;
3533

3634
/// Information pertinent to an overlapping impl error.
@@ -106,22 +104,23 @@ pub fn translate_substs<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
106104
}
107105

108106
/// Given a selected impl described by `impl_data`, returns the
109-
/// definition and substitions for the method with the name `name`,
110-
/// and trait method substitutions `substs`, in that impl, a less
111-
/// specialized impl, or the trait default, whichever applies.
112-
pub fn find_method<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
113-
name: ast::Name,
114-
substs: &'tcx Substs<'tcx>,
115-
impl_data: &super::VtableImplData<'tcx, ()>)
116-
-> (DefId, &'tcx Substs<'tcx>)
117-
{
107+
/// definition and substitions for the method with the name `name`
108+
/// the kind `kind`, and trait method substitutions `substs`, in
109+
/// that impl, a less specialized impl, or the trait default,
110+
/// whichever applies.
111+
pub fn find_associated_item<'a, 'tcx>(
112+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
113+
item: &ty::AssociatedItem,
114+
substs: &'tcx Substs<'tcx>,
115+
impl_data: &super::VtableImplData<'tcx, ()>,
116+
) -> (DefId, &'tcx Substs<'tcx>) {
118117
assert!(!substs.needs_infer());
119118

120119
let trait_def_id = tcx.trait_id_of_impl(impl_data.impl_def_id).unwrap();
121120
let trait_def = tcx.lookup_trait_def(trait_def_id);
122121

123122
let ancestors = trait_def.ancestors(impl_data.impl_def_id);
124-
match ancestors.defs(tcx, name, ty::AssociatedKind::Method).next() {
123+
match ancestors.defs(tcx, item.name, item.kind).next() {
125124
Some(node_item) => {
126125
let substs = tcx.infer_ctxt((), Reveal::All).enter(|infcx| {
127126
let substs = substs.rebase_onto(tcx, trait_def_id, impl_data.substs);
@@ -137,7 +136,7 @@ pub fn find_method<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
137136
(node_item.item.def_id, substs)
138137
}
139138
None => {
140-
bug!("method {:?} not found in {:?}", name, impl_data.impl_def_id)
139+
bug!("{:?} not found in {:?}", item, impl_data.impl_def_id)
141140
}
142141
}
143142
}

src/librustc/ty/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,15 @@ impl<T, R> InternIteratorElement<T, R> for T {
14691469
}
14701470
}
14711471

1472+
impl<'a, T, R> InternIteratorElement<T, R> for &'a T
1473+
where T: Clone + 'a
1474+
{
1475+
type Output = R;
1476+
fn intern_with<I: Iterator<Item=Self>, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output {
1477+
f(&iter.cloned().collect::<AccumulateVec<[_; 8]>>())
1478+
}
1479+
}
1480+
14721481
impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
14731482
type Output = Result<R, E>;
14741483
fn intern_with<I: Iterator<Item=Self>, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output {

src/librustc/ty/instance.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,30 @@ pub struct Instance<'tcx> {
2727
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
2828
pub enum InstanceDef<'tcx> {
2929
Item(DefId),
30+
Intrinsic(DefId),
3031
// <fn() as FnTrait>::call_*
32+
// def-id is FnTrait::call_*
3133
FnPtrShim(DefId, Ty<'tcx>),
34+
// <Trait as Trait>::fn
35+
Virtual(DefId, usize),
36+
// <[mut closure] as FnOnce>::call_once
37+
ClosureOnceShim {
38+
call_once: DefId,
39+
closure_did: DefId
40+
},
3241
}
3342

3443
impl<'tcx> InstanceDef<'tcx> {
3544
#[inline]
3645
pub fn def_id(&self) -> DefId {
3746
match *self {
3847
InstanceDef::Item(def_id) |
39-
InstanceDef::FnPtrShim(def_id, _)
40-
=> def_id
48+
InstanceDef::FnPtrShim(def_id, _) |
49+
InstanceDef::Virtual(def_id, _) |
50+
InstanceDef::Intrinsic(def_id, ) |
51+
InstanceDef::ClosureOnceShim {
52+
call_once: def_id, closure_did: _
53+
} => def_id
4154
}
4255
}
4356

@@ -73,14 +86,23 @@ impl<'tcx> InstanceDef<'tcx> {
7386

7487
impl<'tcx> fmt::Display for Instance<'tcx> {
7588
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89+
ppaux::parameterized(f, self.substs, self.def_id(), &[])?;
7690
match self.def {
77-
InstanceDef::Item(def) => {
78-
ppaux::parameterized(f, self.substs, def, &[])
91+
InstanceDef::Item(_) => Ok(()),
92+
InstanceDef::Intrinsic(_) => {
93+
write!(f, " - intrinsic")
7994
}
80-
InstanceDef::FnPtrShim(def, ty) => {
81-
ppaux::parameterized(f, self.substs, def, &[])?;
95+
InstanceDef::Virtual(_, num) => {
96+
write!(f, " - shim(#{})", num)
97+
}
98+
InstanceDef::FnPtrShim(_, ty) => {
8299
write!(f, " - shim({:?})", ty)
83100
}
101+
InstanceDef::ClosureOnceShim {
102+
call_once: _, closure_did
103+
} => {
104+
write!(f, " - shim({:?})", closure_did)
105+
}
84106
}
85107
}
86108
}

src/librustc/ty/util.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
408408
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
409409
})
410410
}
411-
412-
413411
}
414412

415413
pub struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a, W> {

src/librustc_mir/shim.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fn make_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
4141
ty::InstanceDef::FnPtrShim(_, ty) => {
4242
build_fn_ptr_shim(tcx, ty, instance.def_ty(tcx))
4343
}
44+
_ => bug!("unknown shim kind")
4445
};
4546
debug!("make_shim({:?}) = {:?}", instance, result);
4647

src/librustc_trans/base.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ use llvm::{Linkage, ValueRef, Vector, get_param};
3636
use llvm;
3737
use rustc::hir::def_id::LOCAL_CRATE;
3838
use middle::lang_items::StartFnLangItem;
39-
use rustc::traits;
4039
use rustc::ty::{self, Ty, TyCtxt};
41-
use rustc::ty::adjustment::CustomCoerceUnsized;
4240
use rustc::dep_graph::{AssertDepGraphSafe, DepNode, WorkProduct};
4341
use rustc::hir::map as hir_map;
4442
use rustc::util::common::time;
@@ -54,7 +52,6 @@ use common::{C_bool, C_bytes_in_context, C_i32, C_uint};
5452
use collector::{self, TransItemCollectionMode};
5553
use common::{C_struct_in_context, C_u64, C_undef};
5654
use common::CrateContext;
57-
use common::{fulfill_obligation};
5855
use common::{type_is_zero_size, val_ty};
5956
use common;
6057
use consts;
@@ -80,7 +77,7 @@ use std::ffi::{CStr, CString};
8077
use std::rc::Rc;
8178
use std::str;
8279
use std::i32;
83-
use syntax_pos::{Span, DUMMY_SP};
80+
use syntax_pos::Span;
8481
use syntax::attr;
8582
use rustc::hir;
8683
use rustc::ty::layout::{self, Layout};
@@ -313,25 +310,6 @@ pub fn coerce_unsized_into<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
313310
}
314311
}
315312

316-
pub fn custom_coerce_unsize_info<'scx, 'tcx>(scx: &SharedCrateContext<'scx, 'tcx>,
317-
source_ty: Ty<'tcx>,
318-
target_ty: Ty<'tcx>)
319-
-> CustomCoerceUnsized {
320-
let trait_ref = ty::Binder(ty::TraitRef {
321-
def_id: scx.tcx().lang_items.coerce_unsized_trait().unwrap(),
322-
substs: scx.tcx().mk_substs_trait(source_ty, &[target_ty])
323-
});
324-
325-
match fulfill_obligation(scx, DUMMY_SP, trait_ref) {
326-
traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {
327-
scx.tcx().custom_coerce_unsized_kind(impl_def_id)
328-
}
329-
vtable => {
330-
bug!("invalid CoerceUnsized vtable: {:?}", vtable);
331-
}
332-
}
333-
}
334-
335313
pub fn cast_shift_expr_rhs(
336314
cx: &Builder, op: hir::BinOp_, lhs: ValueRef, rhs: ValueRef
337315
) -> ValueRef {

0 commit comments

Comments
 (0)