Skip to content

Commit b05556e

Browse files
committed
trans: Rename MonoId to Instance and start using it in more places.
1 parent d6e72c4 commit b05556e

File tree

16 files changed

+230
-186
lines changed

16 files changed

+230
-186
lines changed

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub trait CrateStore<'tcx> : Any {
182182
fn trait_of_item(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
183183
-> Option<DefId>;
184184
fn impl_or_trait_item(&self, tcx: &TyCtxt<'tcx>, def: DefId)
185-
-> ty::ImplOrTraitItem<'tcx>;
185+
-> Option<ty::ImplOrTraitItem<'tcx>>;
186186

187187
// flags
188188
fn is_const_fn(&self, did: DefId) -> bool;
@@ -353,7 +353,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
353353
fn trait_of_item(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
354354
-> Option<DefId> { unimplemented!() }
355355
fn impl_or_trait_item(&self, tcx: &TyCtxt<'tcx>, def: DefId)
356-
-> ty::ImplOrTraitItem<'tcx> { unimplemented!() }
356+
-> Option<ty::ImplOrTraitItem<'tcx>> { unimplemented!() }
357357

358358
// flags
359359
fn is_const_fn(&self, did: DefId) -> bool { unimplemented!() }

src/librustc/middle/ty/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,8 @@ impl<'tcx> TyCtxt<'tcx> {
21822182
pub fn impl_or_trait_item(&self, id: DefId) -> ImplOrTraitItem<'tcx> {
21832183
lookup_locally_or_in_crate_store(
21842184
"impl_or_trait_items", id, &self.impl_or_trait_items,
2185-
|| self.sess.cstore.impl_or_trait_item(self, id))
2185+
|| self.sess.cstore.impl_or_trait_item(self, id)
2186+
.expect("missing ImplOrTraitItem in metadata"))
21862187
}
21872188

21882189
pub fn trait_item_def_ids(&self, id: DefId) -> Rc<Vec<ImplOrTraitItemId>> {
@@ -2502,10 +2503,12 @@ impl<'tcx> TyCtxt<'tcx> {
25022503
/// ID of the impl that the method belongs to. Otherwise, return `None`.
25032504
pub fn impl_of_method(&self, def_id: DefId) -> Option<DefId> {
25042505
if def_id.krate != LOCAL_CRATE {
2505-
return match self.sess.cstore.impl_or_trait_item(self, def_id).container() {
2506-
TraitContainer(_) => None,
2507-
ImplContainer(def_id) => Some(def_id),
2508-
};
2506+
return self.sess.cstore.impl_or_trait_item(self, def_id).and_then(|item| {
2507+
match item.container() {
2508+
TraitContainer(_) => None,
2509+
ImplContainer(def_id) => Some(def_id),
2510+
}
2511+
});
25092512
}
25102513
match self.impl_or_trait_items.borrow().get(&def_id).cloned() {
25112514
Some(trait_item) => {

src/librustc/mir/repr.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_const_eval::{ConstUsize, ConstInt};
1414
use middle::def_id::DefId;
1515
use middle::subst::Substs;
1616
use middle::ty::{self, AdtDef, ClosureSubsts, FnOutput, Region, Ty};
17+
use util::ppaux;
1718
use rustc_back::slice;
1819
use rustc_front::hir::InlineAsm;
1920
use std::ascii;
@@ -775,8 +776,8 @@ impl<'tcx> Debug for Rvalue<'tcx> {
775776
Aggregate(ref kind, ref lvs) => {
776777
use self::AggregateKind::*;
777778

778-
fn fmt_tuple(fmt: &mut Formatter, name: &str, lvs: &[Operand]) -> fmt::Result {
779-
let mut tuple_fmt = fmt.debug_tuple(name);
779+
fn fmt_tuple(fmt: &mut Formatter, lvs: &[Operand]) -> fmt::Result {
780+
let mut tuple_fmt = fmt.debug_tuple("");
780781
for lv in lvs {
781782
tuple_fmt.field(lv);
782783
}
@@ -790,19 +791,24 @@ impl<'tcx> Debug for Rvalue<'tcx> {
790791
match lvs.len() {
791792
0 => write!(fmt, "()"),
792793
1 => write!(fmt, "({:?},)", lvs[0]),
793-
_ => fmt_tuple(fmt, "", lvs),
794+
_ => fmt_tuple(fmt, lvs),
794795
}
795796
}
796797

797-
Adt(adt_def, variant, _) => {
798+
Adt(adt_def, variant, substs) => {
798799
let variant_def = &adt_def.variants[variant];
799-
let name = ty::tls::with(|tcx| tcx.item_path_str(variant_def.did));
800+
801+
try!(ppaux::parameterized(fmt, substs, variant_def.did,
802+
ppaux::Ns::Value, &[],
803+
|tcx| {
804+
tcx.lookup_item_type(variant_def.did).generics
805+
}));
800806

801807
match variant_def.kind() {
802-
ty::VariantKind::Unit => write!(fmt, "{}", name),
803-
ty::VariantKind::Tuple => fmt_tuple(fmt, &name, lvs),
808+
ty::VariantKind::Unit => Ok(()),
809+
ty::VariantKind::Tuple => fmt_tuple(fmt, lvs),
804810
ty::VariantKind::Struct => {
805-
let mut struct_fmt = fmt.debug_struct(&name);
811+
let mut struct_fmt = fmt.debug_struct("");
806812
for (field, lv) in variant_def.fields.iter().zip(lvs) {
807813
struct_fmt.field(&field.name.as_str(), lv);
808814
}
@@ -882,8 +888,10 @@ impl<'tcx> Debug for Literal<'tcx> {
882888
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
883889
use self::Literal::*;
884890
match *self {
885-
Item { def_id, .. } =>
886-
write!(fmt, "{}", item_path_str(def_id)),
891+
Item { def_id, substs } => {
892+
ppaux::parameterized(fmt, substs, def_id, ppaux::Ns::Value, &[],
893+
|tcx| tcx.lookup_item_type(def_id).generics)
894+
}
887895
Value { ref value } => {
888896
try!(write!(fmt, "const "));
889897
fmt_const_val(fmt, value)

src/librustc/util/ppaux.rs

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,37 @@ fn fn_sig(f: &mut fmt::Formatter,
6060
}
6161
}
6262

63-
fn parameterized<GG>(f: &mut fmt::Formatter,
64-
substs: &subst::Substs,
65-
did: DefId,
66-
projections: &[ty::ProjectionPredicate],
67-
get_generics: GG)
68-
-> fmt::Result
63+
/// Namespace of the path given to parameterized to print.
64+
#[derive(Copy, Clone, PartialEq)]
65+
pub enum Ns {
66+
Type,
67+
Value
68+
}
69+
70+
pub fn parameterized<GG>(f: &mut fmt::Formatter,
71+
substs: &subst::Substs,
72+
did: DefId,
73+
ns: Ns,
74+
projections: &[ty::ProjectionPredicate],
75+
get_generics: GG)
76+
-> fmt::Result
6977
where GG: for<'tcx> FnOnce(&TyCtxt<'tcx>) -> ty::Generics<'tcx>
7078
{
71-
let (fn_trait_kind, verbose) = try!(ty::tls::with(|tcx| {
79+
if let (Ns::Value, Some(self_ty)) = (ns, substs.self_ty()) {
80+
try!(write!(f, "<{} as ", self_ty));
81+
}
82+
83+
let (fn_trait_kind, verbose, last_name) = try!(ty::tls::with(|tcx| {
84+
let (did, last_name) = if ns == Ns::Value {
85+
// Try to get the impl/trait parent, if this is an
86+
// associated value item (method or constant).
87+
tcx.trait_of_item(did).or_else(|| tcx.impl_of_method(did))
88+
.map_or((did, None), |parent| (parent, Some(tcx.item_name(did))))
89+
} else {
90+
(did, None)
91+
};
7292
try!(write!(f, "{}", tcx.item_path_str(did)));
73-
Ok((tcx.lang_items.fn_trait_kind(did), tcx.sess.verbose()))
93+
Ok((tcx.lang_items.fn_trait_kind(did), tcx.sess.verbose(), last_name))
7494
}));
7595

7696
let mut empty = true;
@@ -185,7 +205,28 @@ fn parameterized<GG>(f: &mut fmt::Formatter,
185205
projection.ty));
186206
}
187207

188-
start_or_continue(f, "", ">")
208+
try!(start_or_continue(f, "", ">"));
209+
210+
// For values, also print their name and type parameters.
211+
if ns == Ns::Value {
212+
if substs.self_ty().is_some() {
213+
try!(write!(f, ">"));
214+
}
215+
216+
if let Some(name) = last_name {
217+
try!(write!(f, "::{}", name));
218+
}
219+
let tps = substs.types.get_slice(subst::FnSpace);
220+
if !tps.is_empty() {
221+
try!(write!(f, "::<{}", tps[0]));
222+
for ty in &tps[1..] {
223+
try!(write!(f, ", {}", ty));
224+
}
225+
try!(write!(f, ">"));
226+
}
227+
}
228+
229+
Ok(())
189230
}
190231

191232
fn in_binder<'tcx, T, U>(f: &mut fmt::Formatter,
@@ -265,6 +306,7 @@ impl<'tcx> fmt::Display for TraitAndProjections<'tcx> {
265306
let TraitAndProjections(ref trait_ref, ref projection_bounds) = *self;
266307
parameterized(f, trait_ref.substs,
267308
trait_ref.def_id,
309+
Ns::Type,
268310
projection_bounds,
269311
|tcx| tcx.lookup_trait_def(trait_ref.def_id).generics.clone())
270312
}
@@ -769,7 +811,7 @@ impl fmt::Display for ty::Binder<ty::OutlivesPredicate<ty::Region, ty::Region>>
769811

770812
impl<'tcx> fmt::Display for ty::TraitRef<'tcx> {
771813
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
772-
parameterized(f, self.substs, self.def_id, &[],
814+
parameterized(f, self.substs, self.def_id, Ns::Type, &[],
773815
|tcx| tcx.lookup_trait_def(self.def_id).generics.clone())
774816
}
775817
}
@@ -821,19 +863,9 @@ impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
821863
try!(write!(f, "extern {} ", bare_fn.abi));
822864
}
823865

824-
try!(write!(f, "{}", bare_fn.sig.0));
825-
try!(ty::tls::with(|tcx| {
826-
write!(f, " {{{}", tcx.item_path_str(def_id))
827-
}));
828-
829-
let tps = substs.types.get_slice(subst::FnSpace);
830-
if tps.len() >= 1 {
831-
try!(write!(f, "::<{}", tps[0]));
832-
for &ty in &tps[1..] {
833-
try!(write!(f, ", {}", ty));
834-
}
835-
try!(write!(f, ">"));
836-
}
866+
try!(write!(f, "{} {{", bare_fn.sig.0));
867+
try!(parameterized(f, substs, def_id, Ns::Value, &[],
868+
|tcx| tcx.lookup_item_type(def_id).generics));
837869
write!(f, "}}")
838870
}
839871
TyFnPtr(ref bare_fn) => {
@@ -856,7 +888,7 @@ impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
856888
!tcx.tcache.borrow().contains_key(&def.did) {
857889
write!(f, "{}<..>", tcx.item_path_str(def.did))
858890
} else {
859-
parameterized(f, substs, def.did, &[],
891+
parameterized(f, substs, def.did, Ns::Type, &[],
860892
|tcx| tcx.lookup_item_type(def.did).generics)
861893
}
862894
})

src/librustc_metadata/csearch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
236236
}
237237

238238
fn impl_or_trait_item(&self, tcx: &TyCtxt<'tcx>, def: DefId)
239-
-> ty::ImplOrTraitItem<'tcx>
239+
-> Option<ty::ImplOrTraitItem<'tcx>>
240240
{
241241
let cdata = self.get_crate_data(def.krate);
242242
decoder::get_impl_or_trait_item(

src/librustc_metadata/decoder.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -955,12 +955,16 @@ pub fn get_impl_or_trait_item<'tcx>(intr: Rc<IdentInterner>,
955955
cdata: Cmd,
956956
id: DefIndex,
957957
tcx: &TyCtxt<'tcx>)
958-
-> ty::ImplOrTraitItem<'tcx> {
958+
-> Option<ty::ImplOrTraitItem<'tcx>> {
959959
let item_doc = cdata.lookup_item(id);
960960

961961
let def_id = item_def_id(item_doc, cdata);
962962

963-
let container_id = item_require_parent_item(cdata, item_doc);
963+
let container_id = if let Some(id) = item_parent_item(cdata, item_doc) {
964+
id
965+
} else {
966+
return None;
967+
};
964968
let container_doc = cdata.lookup_item(container_id.index);
965969
let container = match item_family(container_doc) {
966970
Trait => TraitContainer(container_id),
@@ -971,7 +975,7 @@ pub fn get_impl_or_trait_item<'tcx>(intr: Rc<IdentInterner>,
971975
let vis = item_visibility(item_doc);
972976
let defaultness = item_defaultness(item_doc);
973977

974-
match item_sort(item_doc) {
978+
Some(match item_sort(item_doc) {
975979
sort @ Some('C') | sort @ Some('c') => {
976980
let ty = doc_type(item_doc, tcx, cdata);
977981
ty::ConstTraitItem(Rc::new(ty::AssociatedConst {
@@ -1017,8 +1021,8 @@ pub fn get_impl_or_trait_item<'tcx>(intr: Rc<IdentInterner>,
10171021
container: container,
10181022
}))
10191023
}
1020-
_ => panic!("unknown impl/trait item sort"),
1021-
}
1024+
_ => return None
1025+
})
10221026
}
10231027

10241028
pub fn get_trait_item_def_ids(cdata: Cmd, id: DefIndex)
@@ -1058,7 +1062,7 @@ pub fn get_provided_trait_methods<'tcx>(intr: Rc<IdentInterner>,
10581062
cdata,
10591063
did.index,
10601064
tcx);
1061-
if let ty::MethodTraitItem(ref method) = trait_item {
1065+
if let Some(ty::MethodTraitItem(ref method)) = trait_item {
10621066
Some((*method).clone())
10631067
} else {
10641068
None
@@ -1087,7 +1091,7 @@ pub fn get_associated_consts<'tcx>(intr: Rc<IdentInterner>,
10871091
cdata,
10881092
did.index,
10891093
tcx);
1090-
if let ty::ConstTraitItem(ref ac) = trait_item {
1094+
if let Some(ty::ConstTraitItem(ref ac)) = trait_item {
10911095
Some((*ac).clone())
10921096
} else {
10931097
None

src/librustc_trans/trans/base.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use trans::machine;
8282
use trans::machine::{llsize_of, llsize_of_real};
8383
use trans::meth;
8484
use trans::mir;
85-
use trans::monomorphize;
85+
use trans::monomorphize::{self, Instance};
8686
use trans::tvec;
8787
use trans::type_::Type;
8888
use trans::type_of;
@@ -2077,10 +2077,10 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
20772077
.unwrap_or_else(|| ccx.tcx().map.local_def_id(node_id)),
20782078
};
20792079

2080-
ccx.record_translation_item_as_generated(TransItem::Fn{
2081-
def_id: def_id,
2082-
substs: ccx.tcx().mk_substs(ccx.tcx().erase_regions(param_substs)),
2083-
});
2080+
ccx.record_translation_item_as_generated(TransItem::Fn(Instance {
2081+
def: def_id,
2082+
params: &param_substs.types,
2083+
}));
20842084
}
20852085
}
20862086

src/librustc_trans/trans/closure.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use trans::datum::{self, Datum, rvalue_scratch_datum, Rvalue};
2525
use trans::debuginfo::{self, DebugLoc};
2626
use trans::declare;
2727
use trans::expr;
28-
use trans::monomorphize::{MonoId};
28+
use trans::monomorphize::{Instance};
2929
use trans::type_of::*;
3030
use trans::value::Value;
3131
use trans::Disr;
@@ -140,7 +140,7 @@ pub fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
140140
// Normalize type so differences in regions and typedefs don't cause
141141
// duplicate declarations
142142
let substs = ccx.tcx().erase_regions(substs);
143-
let mono_id = MonoId {
143+
let mono_id = Instance {
144144
def: closure_id,
145145
params: &substs.func_substs.types
146146
};

0 commit comments

Comments
 (0)