Implement defaults for associated types
This commit is contained in:
parent
c654a07d29
commit
ae10e478eb
10 changed files with 170 additions and 123 deletions
|
|
@ -252,6 +252,13 @@ fn doc_type<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd) -> Ty<'tcx>
|
|||
|_, did| translate_def_id(cdata, did))
|
||||
}
|
||||
|
||||
fn maybe_doc_type<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd) -> Option<Ty<'tcx>> {
|
||||
reader::maybe_get_doc(doc, tag_items_data_item_type).map(|tp| {
|
||||
parse_ty_data(tp.data, cdata.cnum, tp.start, tcx,
|
||||
|_, did| translate_def_id(cdata, did))
|
||||
})
|
||||
}
|
||||
|
||||
fn doc_method_fty<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>,
|
||||
cdata: Cmd) -> ty::BareFnTy<'tcx> {
|
||||
let tp = reader::get_doc(doc, tag_item_method_fty);
|
||||
|
|
@ -875,24 +882,24 @@ pub fn get_impl_or_trait_item<'tcx>(intr: Rc<IdentInterner>,
|
|||
id: ast::NodeId,
|
||||
tcx: &ty::ctxt<'tcx>)
|
||||
-> ty::ImplOrTraitItem<'tcx> {
|
||||
let method_doc = lookup_item(id, cdata.data());
|
||||
let item_doc = lookup_item(id, cdata.data());
|
||||
|
||||
let def_id = item_def_id(method_doc, cdata);
|
||||
let def_id = item_def_id(item_doc, cdata);
|
||||
|
||||
let container_id = item_require_parent_item(cdata, method_doc);
|
||||
let container_id = item_require_parent_item(cdata, item_doc);
|
||||
let container_doc = lookup_item(container_id.node, cdata.data());
|
||||
let container = match item_family(container_doc) {
|
||||
Trait => TraitContainer(container_id),
|
||||
_ => ImplContainer(container_id),
|
||||
};
|
||||
|
||||
let name = item_name(&*intr, method_doc);
|
||||
let vis = item_visibility(method_doc);
|
||||
let name = item_name(&*intr, item_doc);
|
||||
let vis = item_visibility(item_doc);
|
||||
|
||||
match item_sort(method_doc) {
|
||||
match item_sort(item_doc) {
|
||||
Some('C') => {
|
||||
let ty = doc_type(method_doc, tcx, cdata);
|
||||
let default = get_provided_source(method_doc, cdata);
|
||||
let ty = doc_type(item_doc, tcx, cdata);
|
||||
let default = get_provided_source(item_doc, cdata);
|
||||
ty::ConstTraitItem(Rc::new(ty::AssociatedConst {
|
||||
name: name,
|
||||
ty: ty,
|
||||
|
|
@ -903,11 +910,11 @@ pub fn get_impl_or_trait_item<'tcx>(intr: Rc<IdentInterner>,
|
|||
}))
|
||||
}
|
||||
Some('r') | Some('p') => {
|
||||
let generics = doc_generics(method_doc, tcx, cdata, tag_method_ty_generics);
|
||||
let predicates = doc_predicates(method_doc, tcx, cdata, tag_method_ty_generics);
|
||||
let fty = doc_method_fty(method_doc, tcx, cdata);
|
||||
let explicit_self = get_explicit_self(method_doc);
|
||||
let provided_source = get_provided_source(method_doc, cdata);
|
||||
let generics = doc_generics(item_doc, tcx, cdata, tag_method_ty_generics);
|
||||
let predicates = doc_predicates(item_doc, tcx, cdata, tag_method_ty_generics);
|
||||
let fty = doc_method_fty(item_doc, tcx, cdata);
|
||||
let explicit_self = get_explicit_self(item_doc);
|
||||
let provided_source = get_provided_source(item_doc, cdata);
|
||||
|
||||
ty::MethodTraitItem(Rc::new(ty::Method::new(name,
|
||||
generics,
|
||||
|
|
@ -920,8 +927,10 @@ pub fn get_impl_or_trait_item<'tcx>(intr: Rc<IdentInterner>,
|
|||
provided_source)))
|
||||
}
|
||||
Some('t') => {
|
||||
let ty = maybe_doc_type(item_doc, tcx, cdata);
|
||||
ty::TypeTraitItem(Rc::new(ty::AssociatedType {
|
||||
name: name,
|
||||
ty: ty,
|
||||
vis: vis,
|
||||
def_id: def_id,
|
||||
container: container,
|
||||
|
|
|
|||
|
|
@ -894,12 +894,12 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
|
|||
rbml_w.end_tag();
|
||||
}
|
||||
|
||||
fn encode_info_for_associated_type(ecx: &EncodeContext,
|
||||
rbml_w: &mut Encoder,
|
||||
associated_type: &ty::AssociatedType,
|
||||
impl_path: PathElems,
|
||||
parent_id: NodeId,
|
||||
impl_item_opt: Option<&ast::ImplItem>) {
|
||||
fn encode_info_for_associated_type<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
|
||||
rbml_w: &mut Encoder,
|
||||
associated_type: &ty::AssociatedType<'tcx>,
|
||||
impl_path: PathElems,
|
||||
parent_id: NodeId,
|
||||
impl_item_opt: Option<&ast::ImplItem>) {
|
||||
debug!("encode_info_for_associated_type({:?},{:?})",
|
||||
associated_type.def_id,
|
||||
token::get_name(associated_type.name));
|
||||
|
|
@ -913,8 +913,6 @@ fn encode_info_for_associated_type(ecx: &EncodeContext,
|
|||
encode_parent_item(rbml_w, local_def(parent_id));
|
||||
encode_item_sort(rbml_w, 't');
|
||||
|
||||
encode_bounds_and_type_for_item(rbml_w, ecx, associated_type.def_id.local_id());
|
||||
|
||||
let stab = stability::lookup(ecx.tcx, associated_type.def_id);
|
||||
encode_stability(rbml_w, stab);
|
||||
|
||||
|
|
@ -923,7 +921,14 @@ fn encode_info_for_associated_type(ecx: &EncodeContext,
|
|||
|
||||
if let Some(ii) = impl_item_opt {
|
||||
encode_attributes(rbml_w, &ii.attrs);
|
||||
encode_type(ecx, rbml_w, ty::node_id_to_type(ecx.tcx, ii.id));
|
||||
} else {
|
||||
encode_predicates(rbml_w, ecx,
|
||||
&ty::lookup_predicates(ecx.tcx, associated_type.def_id),
|
||||
tag_item_generics);
|
||||
}
|
||||
|
||||
if let Some(ty) = associated_type.ty {
|
||||
encode_type(ecx, rbml_w, ty);
|
||||
}
|
||||
|
||||
rbml_w.end_tag();
|
||||
|
|
|
|||
|
|
@ -857,37 +857,39 @@ fn confirm_impl_candidate<'cx,'tcx>(
|
|||
-> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
|
||||
{
|
||||
// there don't seem to be nicer accessors to these:
|
||||
let impl_items_map = selcx.tcx().impl_items.borrow();
|
||||
let impl_or_trait_items_map = selcx.tcx().impl_or_trait_items.borrow();
|
||||
|
||||
let impl_items = impl_items_map.get(&impl_vtable.impl_def_id).unwrap();
|
||||
let mut impl_ty = None;
|
||||
for impl_item in impl_items {
|
||||
let assoc_type = match *impl_or_trait_items_map.get(&impl_item.def_id()).unwrap() {
|
||||
ty::TypeTraitItem(ref assoc_type) => assoc_type.clone(),
|
||||
ty::ConstTraitItem(..) | ty::MethodTraitItem(..) => { continue; }
|
||||
};
|
||||
|
||||
if assoc_type.name != obligation.predicate.item_name {
|
||||
continue;
|
||||
}
|
||||
|
||||
let impl_poly_ty = ty::lookup_item_type(selcx.tcx(), assoc_type.def_id);
|
||||
impl_ty = Some(impl_poly_ty.ty.subst(selcx.tcx(), &impl_vtable.substs));
|
||||
break;
|
||||
}
|
||||
|
||||
match impl_ty {
|
||||
Some(ty) => (ty, impl_vtable.nested.into_vec()),
|
||||
None => {
|
||||
// This means that the impl is missing a
|
||||
// definition for the associated type. This error
|
||||
// ought to be reported by the type checker method
|
||||
// `check_impl_items_against_trait`, so here we
|
||||
// just return ty_err.
|
||||
(selcx.tcx().types.err, vec!())
|
||||
for impl_item in &selcx.tcx().impl_items.borrow()[&impl_vtable.impl_def_id] {
|
||||
if let ty::TypeTraitItem(ref assoc_ty) = impl_or_trait_items_map[&impl_item.def_id()] {
|
||||
if assoc_ty.name == obligation.predicate.item_name {
|
||||
return (assoc_ty.ty.unwrap().subst(selcx.tcx(), &impl_vtable.substs),
|
||||
impl_vtable.nested.into_vec());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let trait_ref = obligation.predicate.trait_ref;
|
||||
for trait_item in ty::trait_items(selcx.tcx(), trait_ref.def_id).iter() {
|
||||
if let &ty::TypeTraitItem(ref assoc_ty) = trait_item {
|
||||
if assoc_ty.name == obligation.predicate.item_name {
|
||||
if let Some(ty) = assoc_ty.ty {
|
||||
return (ty.subst(selcx.tcx(), trait_ref.substs),
|
||||
impl_vtable.nested.into_vec());
|
||||
} else {
|
||||
// This means that the impl is missing a
|
||||
// definition for the associated type. This error
|
||||
// ought to be reported by the type checker method
|
||||
// `check_impl_items_against_trait`, so here we
|
||||
// just return ty_err.
|
||||
return (selcx.tcx().types.err, vec!());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
selcx.tcx().sess.span_bug(obligation.cause.span,
|
||||
&format!("No associated type for {}",
|
||||
trait_ref.repr(selcx.tcx())));
|
||||
}
|
||||
|
||||
impl<'tcx> Repr<'tcx> for ProjectionTyError<'tcx> {
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ impl ImplOrTraitItemContainer {
|
|||
pub enum ImplOrTraitItem<'tcx> {
|
||||
ConstTraitItem(Rc<AssociatedConst<'tcx>>),
|
||||
MethodTraitItem(Rc<Method<'tcx>>),
|
||||
TypeTraitItem(Rc<AssociatedType>),
|
||||
TypeTraitItem(Rc<AssociatedType<'tcx>>),
|
||||
}
|
||||
|
||||
impl<'tcx> ImplOrTraitItem<'tcx> {
|
||||
|
|
@ -267,8 +267,9 @@ pub struct AssociatedConst<'tcx> {
|
|||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct AssociatedType {
|
||||
pub struct AssociatedType<'tcx> {
|
||||
pub name: ast::Name,
|
||||
pub ty: Option<Ty<'tcx>>,
|
||||
pub vis: ast::Visibility,
|
||||
pub def_id: ast::DefId,
|
||||
pub container: ImplOrTraitItemContainer,
|
||||
|
|
|
|||
|
|
@ -1077,7 +1077,7 @@ impl<'tcx> Repr<'tcx> for ty::AssociatedConst<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Repr<'tcx> for ty::AssociatedType {
|
||||
impl<'tcx> Repr<'tcx> for ty::AssociatedType<'tcx> {
|
||||
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
|
||||
format!("AssociatedType(name: {}, vis: {}, def_id: {})",
|
||||
self.name.repr(tcx),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue