From 2ea61204f6bae64955f57dda7d3768bc9ba2dd55 Mon Sep 17 00:00:00 2001 From: Michael Sullivan Date: Mon, 15 Jul 2013 15:24:16 -0700 Subject: [PATCH] Get rid of resolve::MethodInfo. Closes #4946. --- src/librustc/metadata/csearch.rs | 10 ++-- src/librustc/metadata/decoder.rs | 23 ++++----- src/librustc/metadata/encoder.rs | 2 +- src/librustc/middle/resolve.rs | 18 ------- src/librustc/middle/trans/meth.rs | 4 +- src/librustc/middle/ty.rs | 7 ++- src/librustc/middle/typeck/check/method.rs | 4 +- src/librustc/middle/typeck/coherence.rs | 60 +++++++--------------- 8 files changed, 44 insertions(+), 84 deletions(-) diff --git a/src/librustc/metadata/csearch.rs b/src/librustc/metadata/csearch.rs index f336b0f4e4c..993649492a4 100644 --- a/src/librustc/metadata/csearch.rs +++ b/src/librustc/metadata/csearch.rs @@ -15,7 +15,7 @@ use metadata::cstore; use metadata::decoder; use metadata; -use middle::{ty, resolve}; +use middle::ty; use std::vec; use reader = extra::ebml::reader; @@ -97,10 +97,10 @@ pub fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id) } /// Returns information about the given implementation. -pub fn get_impl(cstore: @mut cstore::CStore, impl_def_id: ast::def_id) - -> resolve::Impl { - let cdata = cstore::get_crate_data(cstore, impl_def_id.crate); - decoder::get_impl(cstore.intr, cdata, impl_def_id.node) +pub fn get_impl(tcx: ty::ctxt, impl_def_id: ast::def_id) + -> ty::Impl { + let cdata = cstore::get_crate_data(tcx.cstore, impl_def_id.crate); + decoder::get_impl(tcx.cstore.intr, cdata, impl_def_id.node, tcx) } pub fn get_method(tcx: ty::ctxt, def: ast::def_id) -> ty::Method { diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index caa170605de..7d515fe62b5 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -20,7 +20,7 @@ use metadata::tydecode::{parse_ty_data, parse_def_id, parse_type_param_def_data, parse_bare_fn_ty_data, parse_trait_ref_data}; -use middle::{ty, resolve}; +use middle::ty; use std::hash::HashUtil; use std::int; @@ -795,34 +795,29 @@ fn get_mutability(ch: u8) -> ast::mutability { } fn item_impl_methods(intr: @ident_interner, cdata: cmd, item: ebml::Doc, - base_tps: uint) -> ~[@resolve::MethodInfo] { + tcx: ty::ctxt) -> ~[@ty::Method] { let mut rslt = ~[]; for reader::tagged_docs(item, tag_item_impl_method) |doc| { let m_did = reader::with_doc_data(doc, parse_def_id); - let mth_item = lookup_item(m_did.node, cdata.data); - let explicit_self = get_explicit_self(mth_item); - rslt.push(@resolve::MethodInfo { - did: translate_def_id(cdata, m_did), - n_tps: item_ty_param_count(mth_item) - base_tps, - ident: item_name(intr, mth_item), - explicit_self: explicit_self}); + rslt.push(@get_method(intr, cdata, m_did.node, tcx)); } + rslt } /// Returns information about the given implementation. -pub fn get_impl(intr: @ident_interner, cdata: cmd, impl_id: ast::node_id) - -> resolve::Impl { +pub fn get_impl(intr: @ident_interner, cdata: cmd, impl_id: ast::node_id, + tcx: ty::ctxt) + -> ty::Impl { let data = cdata.data; let impl_item = lookup_item(impl_id, data); - let base_tps = item_ty_param_count(impl_item); - resolve::Impl { + ty::Impl { did: ast::def_id { crate: cdata.cnum, node: impl_id, }, ident: item_name(intr, impl_item), - methods: item_impl_methods(intr, cdata, impl_item, base_tps), + methods: item_impl_methods(intr, cdata, impl_item, tcx), } } diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index 37c4b183178..722f8ff9580 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -402,7 +402,7 @@ fn encode_reexported_static_base_methods(ecx: &EncodeContext, for base_impl.methods.iter().advance |&m| { if m.explicit_self == ast::sty_static { encode_reexported_static_method(ecx, ebml_w, exp, - m.did, m.ident); + m.def_id, m.ident); } } } diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 80078eba239..6d457b46d4f 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -55,24 +55,6 @@ pub struct binding_info { // Map from the name in a pattern to its binding mode. pub type BindingMap = HashMap; -// Implementation resolution -// -// FIXME #4946: This kind of duplicates information kept in -// ty::method. Maybe it should go away. - -pub struct MethodInfo { - did: def_id, - n_tps: uint, - ident: ident, - explicit_self: explicit_self_ -} - -pub struct Impl { - did: def_id, - ident: ident, - methods: ~[@MethodInfo] -} - // Trait method resolution pub type TraitMap = HashMap; diff --git a/src/librustc/middle/trans/meth.rs b/src/librustc/middle/trans/meth.rs index 1da56767602..03dbe0869fb 100644 --- a/src/librustc/middle/trans/meth.rs +++ b/src/librustc/middle/trans/meth.rs @@ -344,8 +344,8 @@ pub fn method_with_name(ccx: &mut CrateContext, let meth = imp.methods.iter().find_(|m| m.ident == name) .expect("could not find method while translating"); - ccx.impl_method_cache.insert((impl_id, name), meth.did); - meth.did + ccx.impl_method_cache.insert((impl_id, name), meth.def_id); + meth.def_id } pub fn trans_monomorphized_callee(bcx: block, diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index cd8160d900e..6f3c3d9037e 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -16,7 +16,6 @@ use middle::lang_items::{TyDescStructLangItem, TyVisitorTraitLangItem}; use middle::lang_items::OpaqueStructLangItem; use middle::freevars; -use middle::resolve::{Impl, MethodInfo}; use middle::resolve; use middle::ty; use middle::subst::Subst; @@ -96,6 +95,12 @@ pub fn new(ident: ast::ident, } } +pub struct Impl { + did: def_id, + ident: ident, + methods: ~[@Method] +} + #[deriving(Clone, Eq, IterBytes)] pub struct mt { ty: t, diff --git a/src/librustc/middle/typeck/check/method.rs b/src/librustc/middle/typeck/check/method.rs index 06ce6420123..4a32e8bf952 100644 --- a/src/librustc/middle/typeck/check/method.rs +++ b/src/librustc/middle/typeck/check/method.rs @@ -527,7 +527,7 @@ pub fn push_inherent_impl_candidates_for_type(&self, did: def_id) { pub fn push_candidates_from_impl(&self, candidates: &mut ~[Candidate], - impl_info: &resolve::Impl) { + impl_info: &ty::Impl) { if !self.impl_dups.insert(impl_info.did) { return; // already visited } @@ -543,7 +543,7 @@ pub fn push_candidates_from_impl(&self, } }; - let method = ty::method(self.tcx(), impl_info.methods[idx].did); + let method = ty::method(self.tcx(), impl_info.methods[idx].def_id); // determine the `self` of the impl with fresh // variables for each parameter: diff --git a/src/librustc/middle/typeck/coherence.rs b/src/librustc/middle/typeck/coherence.rs index a0be0d201f4..2bdb2f2932a 100644 --- a/src/librustc/middle/typeck/coherence.rs +++ b/src/librustc/middle/typeck/coherence.rs @@ -17,9 +17,8 @@ use metadata::csearch::{each_path, get_impl_trait}; use metadata::csearch; -use metadata::cstore::{CStore, iter_crate_data}; +use metadata::cstore::iter_crate_data; use metadata::decoder::{dl_def, dl_field, dl_impl}; -use middle::resolve::{Impl, MethodInfo}; use middle::ty::{ProvidedMethodSource, get}; use middle::ty::{lookup_item_type, subst}; use middle::ty::{substs, t, ty_bool, ty_bot, ty_box, ty_enum, ty_err}; @@ -31,6 +30,7 @@ use middle::ty::{type_is_ty_var}; use middle::subst::Subst; use middle::ty; +use middle::ty::{Impl, Method}; use middle::typeck::CrateCtxt; use middle::typeck::infer::combine::Combine; use middle::typeck::infer::InferCtxt; @@ -38,7 +38,7 @@ use middle::typeck::infer; use syntax::ast::{crate, def_id, def_struct, def_ty}; use syntax::ast::{item, item_enum, item_impl, item_mod, item_struct}; -use syntax::ast::{local_crate, method, trait_ref, ty_path}; +use syntax::ast::{local_crate, trait_ref, ty_path}; use syntax::ast; use syntax::ast_map::node_item; use syntax::ast_map; @@ -149,16 +149,6 @@ pub fn get_base_type_def_id(inference_context: @mut InferCtxt, } } - -pub fn method_to_MethodInfo(ast_method: @method) -> @MethodInfo { - @MethodInfo { - did: local_def(ast_method.id), - n_tps: ast_method.generics.ty_params.len(), - ident: ast_method.ident, - explicit_self: ast_method.explicit_self.node - } -} - pub fn CoherenceChecker(crate_context: @mut CrateCtxt) -> CoherenceChecker { CoherenceChecker { crate_context: crate_context, @@ -291,7 +281,7 @@ pub fn check_implementation(&self, pub fn instantiate_default_methods(&self, impl_id: ast::def_id, trait_ref: &ty::TraitRef, - all_methods: &mut ~[@MethodInfo]) { + all_methods: &mut ~[@Method]) { let tcx = self.crate_context.tcx; debug!("instantiate_default_methods(impl_id=%?, trait_ref=%s)", impl_id, trait_ref.repr(tcx)); @@ -316,6 +306,7 @@ pub fn instantiate_default_methods(&self, *trait_method); debug!("new_method_ty=%s", new_method_ty.repr(tcx)); + all_methods.push(new_method_ty); // construct the polytype for the method based on the method_ty let new_generics = ty::Generics { @@ -344,14 +335,6 @@ pub fn instantiate_default_methods(&self, self.crate_context.tcx.provided_method_sources.insert(new_did, source); - - let method_info = @MethodInfo { - did: new_did, - n_tps: trait_method.generics.type_param_defs.len(), - ident: trait_method.ident, - explicit_self: trait_method.explicit_self - }; - all_methods.push(method_info); } } @@ -563,7 +546,7 @@ pub fn trait_ref_to_trait_def_id(&self, trait_ref: &trait_ref) -> def_id { // here for historical reasons pub fn check_trait_methods_are_implemented( &self, - all_methods: &mut ~[@MethodInfo], + all_methods: &mut ~[@Method], trait_did: def_id, trait_ref_span: span) { @@ -628,11 +611,12 @@ pub fn ast_type_is_defined_in_local_crate(&self, original_type: &ast::Ty) // Converts an implementation in the AST to an Impl structure. pub fn create_impl_from_item(&self, item: @item) -> @Impl { + let tcx = self.crate_context.tcx; match item.node { item_impl(_, ref trait_refs, _, ref ast_methods) => { let mut methods = ~[]; for ast_methods.iter().advance |ast_method| { - methods.push(method_to_MethodInfo(*ast_method)); + methods.push(ty::method(tcx, local_def(ast_method.id))); } for trait_refs.iter().advance |trait_ref| { @@ -682,12 +666,12 @@ pub fn span_of_impl(&self, implementation: @Impl) -> span { pub fn add_external_impl(&self, impls_seen: &mut HashSet, - crate_store: @mut CStore, impl_def_id: def_id) { - let implementation = csearch::get_impl(crate_store, impl_def_id); + let tcx = self.crate_context.tcx; + let implementation = csearch::get_impl(tcx, impl_def_id); debug!("coherence: adding impl from external crate: %s", - ty::item_path_str(self.crate_context.tcx, implementation.did)); + ty::item_path_str(tcx, implementation.did)); // Make sure we don't visit the same implementation multiple times. if !impls_seen.insert(implementation.did) { @@ -696,9 +680,8 @@ pub fn add_external_impl(&self, } // Good. Continue. - let self_type = lookup_item_type(self.crate_context.tcx, - implementation.did); - let associated_traits = get_impl_trait(self.crate_context.tcx, + let self_type = lookup_item_type(tcx, implementation.did); + let associated_traits = get_impl_trait(tcx, implementation.did); // Do a sanity check to make sure that inherent methods have base @@ -708,12 +691,10 @@ pub fn add_external_impl(&self, dummy_sp(), self_type.ty) { None => { - let session = self.crate_context.tcx.sess; - session.bug(fmt!("no base type for external impl with no \ + tcx.sess.bug(fmt!("no base type for external impl with no \ trait: %s (type %s)!", - session.str_of(implementation.ident), - ty_to_str(self.crate_context.tcx, - self_type.ty))); + tcx.sess.str_of(implementation.ident), + ty_to_str(tcx, self_type.ty))); } Some(_) => {} // Nothing to do. } @@ -756,8 +737,7 @@ pub fn add_external_impl(&self, } } - self.crate_context.tcx.impls.insert(implementation.did, - implementation); + tcx.impls.insert(implementation.did, implementation); } // Adds implementations and traits from external crates to the coherence @@ -770,9 +750,7 @@ pub fn add_external_crates(&self) { for each_path(crate_store, crate_number) |_, def_like, _| { match def_like { dl_impl(def_id) => { - self.add_external_impl(&mut impls_seen, - crate_store, - def_id) + self.add_external_impl(&mut impls_seen, def_id) } dl_def(_) | dl_field => loop, // Skip this. } @@ -802,7 +780,7 @@ pub fn populate_destructor_table(&self) { // We'll error out later. For now, just don't ICE. loop; } - let method_def_id = impl_info.methods[0].did; + let method_def_id = impl_info.methods[0].def_id; let self_type = self.get_self_type_for_implementation(*impl_info); match ty::get(self_type.ty).sty {