提交 7a3054f5 编写于 作者: E Eduard Burtescu

rustc_resolve: remove the distinction between DefStaticMethod and DefMethod.

上级 0f49254b
......@@ -150,12 +150,9 @@ pub fn get_trait_name(cstore: &cstore::CStore, def: ast::DefId) -> ast::Name {
def.node)
}
pub fn get_trait_item_name_and_kind(cstore: &cstore::CStore, def: ast::DefId)
-> (ast::Name, def::TraitItemKind) {
pub fn is_static_method(cstore: &cstore::CStore, def: ast::DefId) -> bool {
let cdata = cstore.get_crate_data(def.krate);
decoder::get_trait_item_name_and_kind(cstore.intr.clone(),
&*cdata,
def.node)
decoder::is_static_method(&*cdata, def.node)
}
pub fn get_trait_item_def_ids(cstore: &cstore::CStore, def: ast::DefId)
......
......@@ -334,12 +334,7 @@ fn item_to_def_like(item: rbml::Doc, did: ast::DefId, cnum: ast::CrateNum)
def::FromImpl(item_reqd_and_translated_parent_item(cnum,
item))
};
match fam {
// We don't bother to get encode/decode the trait id, we don't need it.
Method => DlDef(def::DefMethod(did, None, provenance)),
StaticMethod => DlDef(def::DefStaticMethod(did, provenance)),
_ => panic!()
}
DlDef(def::DefMethod(did, provenance))
}
Type => {
if item_sort(item) == Some('t') {
......@@ -853,22 +848,13 @@ pub fn get_trait_name(intr: Rc<IdentInterner>,
item_name(&*intr, doc)
}
pub fn get_trait_item_name_and_kind(intr: Rc<IdentInterner>,
cdata: Cmd,
id: ast::NodeId)
-> (ast::Name, def::TraitItemKind) {
pub fn is_static_method(cdata: Cmd, id: ast::NodeId) -> bool {
let doc = lookup_item(id, cdata.data());
let name = item_name(&*intr, doc);
match item_sort(doc) {
Some('r') | Some('p') => {
let explicit_self = get_explicit_self(doc);
(name, def::TraitItemKind::from_explicit_self_category(explicit_self))
}
Some('t') => (name, def::TypeTraitItemKind),
c => {
panic!("get_trait_item_name_and_kind(): unknown trait item kind \
in metadata: `{:?}`", c)
get_explicit_self(doc) == ty::StaticExplicitSelfCategory
}
_ => false
}
}
......
......@@ -423,13 +423,8 @@ impl tr for def::Def {
fn tr(&self, dcx: &DecodeContext) -> def::Def {
match *self {
def::DefFn(did, is_ctor) => def::DefFn(did.tr(dcx), is_ctor),
def::DefStaticMethod(did, p) => {
def::DefStaticMethod(did.tr(dcx), p.map(|did2| did2.tr(dcx)))
}
def::DefMethod(did0, did1, p) => {
def::DefMethod(did0.tr(dcx),
did1.map(|did1| did1.tr(dcx)),
p.map(|did2| did2.tr(dcx)))
def::DefMethod(did, p) => {
def::DefMethod(did.tr(dcx), p.map(|did2| did2.tr(dcx)))
}
def::DefSelfTy(nid) => { def::DefSelfTy(dcx.tr_id(nid)) }
def::DefMod(did) => { def::DefMod(did.tr(dcx)) }
......
......@@ -452,8 +452,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
v.add_qualif(NON_ZERO_SIZED);
}
}
Some(def::DefFn(..)) |
Some(def::DefStaticMethod(..)) | Some(def::DefMethod(..)) => {
Some(def::DefFn(..)) | Some(def::DefMethod(..)) => {
// Count the function pointer.
v.add_qualif(NON_ZERO_SIZED);
}
......
......@@ -10,10 +10,8 @@
pub use self::Def::*;
pub use self::MethodProvenance::*;
pub use self::TraitItemKind::*;
use middle::subst::ParamSpace;
use middle::ty::{ExplicitSelfCategory, StaticExplicitSelfCategory};
use util::nodemap::NodeMap;
use syntax::ast;
use syntax::ast_util::local_def;
......@@ -23,7 +21,6 @@
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Def {
DefFn(ast::DefId, bool /* is_ctor */),
DefStaticMethod(/* method */ ast::DefId, MethodProvenance),
DefSelfTy(/* trait id */ ast::NodeId),
DefMod(ast::DefId),
DefForeignMod(ast::DefId),
......@@ -51,7 +48,7 @@ pub enum Def {
DefStruct(ast::DefId),
DefRegion(ast::NodeId),
DefLabel(ast::NodeId),
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
DefMethod(ast::DefId /* method */, MethodProvenance),
}
/// The result of resolving the prefix of a path to a type:
......@@ -99,25 +96,6 @@ pub fn map<F>(self, f: F) -> MethodProvenance where
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum TraitItemKind {
NonstaticMethodTraitItemKind,
StaticMethodTraitItemKind,
TypeTraitItemKind,
}
impl TraitItemKind {
pub fn from_explicit_self_category(explicit_self_category:
ExplicitSelfCategory)
-> TraitItemKind {
if explicit_self_category == StaticExplicitSelfCategory {
StaticMethodTraitItemKind
} else {
NonstaticMethodTraitItemKind
}
}
}
impl Def {
pub fn local_node_id(&self) -> ast::NodeId {
let def_id = self.def_id();
......@@ -127,11 +105,10 @@ pub fn local_node_id(&self) -> ast::NodeId {
pub fn def_id(&self) -> ast::DefId {
match *self {
DefFn(id, _) | DefStaticMethod(id, _) | DefMod(id) |
DefForeignMod(id) | DefStatic(id, _) |
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
DefMethod(id, _, _) | DefConst(id) => {
DefMethod(id, _) | DefConst(id) => {
id
}
DefLocal(id) |
......
......@@ -575,7 +575,7 @@ pub fn cat_def(&self,
match def {
def::DefStruct(..) | def::DefVariant(..) | def::DefConst(..) |
def::DefFn(..) | def::DefStaticMethod(..) | def::DefMethod(..) => {
def::DefFn(..) | def::DefMethod(..) => {
Ok(self.cat_rvalue_node(id, span, expr_ty))
}
def::DefMod(_) | def::DefForeignMod(_) | def::DefUse(_) |
......
......@@ -4584,7 +4584,7 @@ pub fn expr_kind(tcx: &ctxt, expr: &ast::Expr) -> ExprKind {
def::DefFn(_, true) => RvalueDpsExpr,
// Fn pointers are just scalar values.
def::DefFn(..) | def::DefStaticMethod(..) | def::DefMethod(..) => RvalueDatumExpr,
def::DefFn(..) | def::DefMethod(..) => RvalueDatumExpr,
// Note: there is actually a good case to be made that
// DefArg's, particularly those of immediate type, ought to
......
......@@ -795,7 +795,6 @@ fn check_path(&mut self, span: Span, path_id: ast::NodeId, last: ast::Ident) {
// be accurate and we can get slightly wonky error messages (but type
// checking is always correct).
match self.tcx.def_map.borrow()[path_id].clone() {
def::DefStaticMethod(..) => ck("static method"),
def::DefFn(..) => ck("function"),
def::DefStatic(..) => ck("static"),
def::DefConst(..) => ck("const"),
......@@ -804,7 +803,6 @@ fn check_path(&mut self, span: Span, path_id: ast::NodeId, last: ast::Ident) {
def::DefTy(_, true) => ck("enum"),
def::DefTrait(..) => ck("trait"),
def::DefStruct(..) => ck("struct"),
def::DefMethod(_, Some(..), _) => ck("trait method"),
def::DefMethod(..) => ck("method"),
def::DefMod(..) => ck("module"),
_ => {}
......
......@@ -40,8 +40,7 @@
use syntax::ast::{ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic, ItemDefaultImpl};
use syntax::ast::{ItemStruct, ItemTrait, ItemTy, ItemUse};
use syntax::ast::{MethodImplItem, Name, NamedField, NodeId};
use syntax::ast::{PathListIdent, PathListMod};
use syntax::ast::{Public, SelfStatic};
use syntax::ast::{PathListIdent, PathListMod, Public};
use syntax::ast::StmtDecl;
use syntax::ast::StructVariantKind;
use syntax::ast::TupleVariantKind;
......@@ -598,22 +597,8 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, parent: &Rc<Module>) ->
&new_parent,
ForbidDuplicateValues,
method.span);
let def = match method.pe_explicit_self()
.node {
SelfStatic => {
// Static methods become
// `DefStaticMethod`s.
DefStaticMethod(local_def(method.id),
FromImpl(local_def(item.id)))
}
_ => {
// Non-static methods become
// `DefMethod`s.
DefMethod(local_def(method.id),
None,
FromImpl(local_def(item.id)))
}
};
let def = DefMethod(local_def(method.id),
FromImpl(local_def(item.id)));
// NB: not IMPORTABLE
let modifiers = if method.pe_vis() == ast::Public {
......@@ -674,7 +659,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, parent: &Rc<Module>) ->
// Add the names of all the items to the trait info.
for trait_item in items {
let (name, kind) = match *trait_item {
let (name, trait_item_id) = match *trait_item {
ast::RequiredMethod(_) |
ast::ProvidedMethod(_) => {
let ty_m = ast_util::trait_item_to_ty_method(trait_item);
......@@ -682,23 +667,8 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, parent: &Rc<Module>) ->
let name = ty_m.ident.name;
// Add it as a name in the trait module.
let (def, static_flag) = match ty_m.explicit_self
.node {
SelfStatic => {
// Static methods become `DefStaticMethod`s.
(DefStaticMethod(
local_def(ty_m.id),
FromTrait(local_def(item.id))),
StaticMethodTraitItemKind)
}
_ => {
// Non-static methods become `DefMethod`s.
(DefMethod(local_def(ty_m.id),
Some(local_def(item.id)),
FromTrait(local_def(item.id))),
NonstaticMethodTraitItemKind)
}
};
let def = DefMethod(local_def(ty_m.id),
FromTrait(local_def(item.id)));
let method_name_bindings =
self.add_child(name,
......@@ -710,7 +680,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, parent: &Rc<Module>) ->
ty_m.span,
PUBLIC);
(name, static_flag)
(name, local_def(ty_m.id))
}
ast::TypeTraitItem(ref associated_type) => {
let def = DefAssociatedTy(local_def(item.id),
......@@ -726,11 +696,12 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, parent: &Rc<Module>) ->
associated_type.ty_param.span,
PUBLIC);
(associated_type.ty_param.ident.name, TypeTraitItemKind)
(associated_type.ty_param.ident.name,
local_def(associated_type.ty_param.id))
}
};
self.trait_item_map.insert((name, def_id), kind);
self.trait_item_map.insert((name, def_id), trait_item_id);
}
name_bindings.define_type(DefTrait(def_id), sp, modifiers);
......@@ -889,7 +860,7 @@ fn handle_external_def(&mut self,
csearch::get_tuple_struct_definition_if_ctor(&self.session.cstore, ctor_id)
.map_or(def, |_| DefStruct(ctor_id)), DUMMY_SP, modifiers);
}
DefFn(..) | DefStaticMethod(..) | DefStatic(..) | DefConst(..) | DefMethod(..) => {
DefFn(..) | DefStatic(..) | DefConst(..) | DefMethod(..) => {
debug!("(building reduced graph for external \
crate) building value (fn/static) {}", final_ident);
// impl methods have already been defined with the correct importability modifier
......@@ -911,21 +882,19 @@ fn handle_external_def(&mut self,
let trait_item_def_ids =
csearch::get_trait_item_def_ids(&self.session.cstore, def_id);
for trait_item_def_id in &trait_item_def_ids {
let (trait_item_name, trait_item_kind) =
csearch::get_trait_item_name_and_kind(
&self.session.cstore,
trait_item_def_id.def_id());
for trait_item_def in &trait_item_def_ids {
let trait_item_name = csearch::get_trait_name(&self.session.cstore,
trait_item_def.def_id());
debug!("(building reduced graph for external crate) ... \
adding trait item '{}'",
token::get_name(trait_item_name));
self.trait_item_map.insert((trait_item_name, def_id), trait_item_kind);
self.trait_item_map.insert((trait_item_name, def_id),
trait_item_def.def_id());
if is_exported {
self.external_exports
.insert(trait_item_def_id.def_id());
self.external_exports.insert(trait_item_def.def_id());
}
}
......
......@@ -900,7 +900,7 @@ struct Resolver<'a, 'tcx:'a> {
graph_root: NameBindings,
trait_item_map: FnvHashMap<(Name, DefId), TraitItemKind>,
trait_item_map: FnvHashMap<(Name, DefId), DefId>,
structs: FnvHashMap<DefId, Vec<Name>>,
......@@ -3128,7 +3128,7 @@ fn resolve_implementation(&mut self,
fn check_trait_item(&self, name: Name, span: Span) {
// If there is a TraitRef in scope for an impl, then the method must be in the trait.
if let Some((did, ref trait_ref)) = self.current_trait_ref {
if self.trait_item_map.get(&(name, did)).is_none() {
if !self.trait_item_map.contains_key(&(name, did)) {
let path_str = self.path_names_to_string(&trait_ref.path, 0);
self.resolve_error(span,
&format!("method `{}` is not a member of trait `{}`",
......@@ -3958,6 +3958,26 @@ fn get_module(this: &mut Resolver, span: Span, name_path: &[ast::Name])
}
}
fn is_static_method(this: &Resolver, did: DefId) -> bool {
if did.krate == ast::LOCAL_CRATE {
let explicit_self = match this.ast_map.get(did.node) {
ast_map::NodeTraitItem(m) => match *m {
ast::RequiredMethod(ref m) => &m.explicit_self,
ast::ProvidedMethod(ref m) => m.pe_explicit_self(),
_ => return false
},
ast_map::NodeImplItem(m) => match *m {
ast::MethodImplItem(ref m) => m.pe_explicit_self(),
_ => return false
},
_ => return false
};
explicit_self.node == ast::SelfStatic
} else {
csearch::is_static_method(&this.session.cstore, did)
}
}
let (path, node_id, allowed) = match self.current_self_type {
Some(ref ty) => match extract_path_and_node_id(ty, Everything) {
Some(x) => x,
......@@ -3986,41 +4006,30 @@ fn get_module(this: &mut Resolver, span: Span, name_path: &[ast::Name])
let name_path = path.segments.iter().map(|seg| seg.identifier.name).collect::<Vec<_>>();
// Look for a method in the current self type's impl module.
match get_module(self, path.span, &name_path[..]) {
Some(module) => match module.children.borrow().get(&name) {
Some(binding) => {
let p_str = self.path_names_to_string(&path, 0);
match binding.def_for_namespace(ValueNS) {
Some(DefStaticMethod(_, provenance)) => {
match provenance {
FromImpl(_) => return StaticMethod(p_str),
FromTrait(_) => unreachable!()
}
}
Some(DefMethod(_, None, _)) if allowed == Everything => return Method,
Some(DefMethod(_, Some(_), _)) => return TraitItem,
_ => ()
if let Some(module) = get_module(self, path.span, &name_path) {
if let Some(binding) = module.children.borrow().get(&name) {
if let Some(DefMethod(did, _)) = binding.def_for_namespace(ValueNS) {
if is_static_method(self, did) {
return StaticMethod(self.path_names_to_string(&path, 0))
}
if self.current_trait_ref.is_some() {
return TraitItem;
} else if allowed == Everything {
return Method;
}
}
None => {}
},
None => {}
}
}
// Look for a method in the current trait.
match self.current_trait_ref {
Some((did, ref trait_ref)) => {
let path_str = self.path_names_to_string(&trait_ref.path, 0);
match self.trait_item_map.get(&(name, did)) {
Some(&StaticMethodTraitItemKind) => {
return TraitMethod(path_str)
}
Some(_) => return TraitItem,
None => {}
if let Some((trait_did, ref trait_ref)) = self.current_trait_ref {
if let Some(&did) = self.trait_item_map.get(&(name, trait_did)) {
if is_static_method(self, did) {
return TraitMethod(self.path_names_to_string(&trait_ref.path, 0));
} else {
return TraitItem;
}
}
None => {}
}
NoSuggestion
......
......@@ -250,7 +250,6 @@ fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
def::DefSelfTy(_) |
def::DefRegion(_) |
def::DefLabel(_) |
def::DefStaticMethod(..) |
def::DefTyParam(..) |
def::DefUse(_) |
def::DefMethod(..) |
......@@ -815,8 +814,7 @@ fn process_path(&mut self,
sub_span,
def_id,
self.cur_scope),
def::DefStaticMethod(declid, provenence) |
def::DefMethod(declid, _, provenence) => {
def::DefMethod(declid, provenence) => {
let sub_span = self.span.sub_span_for_meth_name(span);
let defid = if declid.krate == ast::LOCAL_CRATE {
let ti = ty::impl_or_trait_item(&self.analysis.ty_cx,
......@@ -872,7 +870,14 @@ fn process_path(&mut self,
}
// modules or types in the path prefix
match *def {
def::DefStaticMethod(..) => self.write_sub_path_trait_truncated(path),
def::DefMethod(did, _) => {
let ti = ty::impl_or_trait_item(&self.analysis.ty_cx, did);
if let ty::MethodTraitItem(m) = ti {
if m.explicit_self == ty::StaticExplicitSelfCategory {
self.write_sub_path_trait_truncated(path);
}
}
}
def::DefLocal(_) |
def::DefStatic(_,_) |
def::DefConst(..) |
......
......@@ -165,13 +165,11 @@ fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
let def_id = inline::maybe_instantiate_inline(bcx.ccx(), did);
Callee { bcx: bcx, data: Intrinsic(def_id.node, substs) }
}
def::DefFn(did, _) | def::DefMethod(did, _, def::FromImpl(_)) |
def::DefStaticMethod(did, def::FromImpl(_)) => {
def::DefFn(did, _) | def::DefMethod(did, def::FromImpl(_)) => {
fn_callee(bcx, trans_fn_ref(bcx.ccx(), did, ExprId(ref_expr.id),
bcx.fcx.param_substs).val)
}
def::DefStaticMethod(meth_did, def::FromTrait(trait_did)) |
def::DefMethod(meth_did, _, def::FromTrait(trait_did)) => {
def::DefMethod(meth_did, def::FromTrait(trait_did)) => {
fn_callee(bcx, meth::trans_static_method_callee(bcx.ccx(),
meth_did,
trait_did,
......
......@@ -666,7 +666,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
ast::ExprPath(_) | ast::ExprQPath(_) => {
let def = cx.tcx().def_map.borrow()[e.id];
match def {
def::DefFn(..) | def::DefStaticMethod(..) | def::DefMethod(..) => {
def::DefFn(..) | def::DefMethod(..) => {
expr::trans_def_fn_unadjusted(cx, e, def, param_substs).val
}
def::DefConst(def_id) => {
......
......@@ -875,7 +875,7 @@ fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
let _icx = push_ctxt("trans_def_lvalue");
match def {
def::DefFn(..) | def::DefStaticMethod(..) | def::DefMethod(..) |
def::DefFn(..) | def::DefMethod(..) |
def::DefStruct(_) | def::DefVariant(..) => {
let datum = trans_def_fn_unadjusted(bcx.ccx(), ref_expr, def,
bcx.fcx.param_substs);
......@@ -1275,12 +1275,10 @@ pub fn trans_def_fn_unadjusted<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
match def {
def::DefFn(did, _) |
def::DefStruct(did) | def::DefVariant(_, did, _) |
def::DefStaticMethod(did, def::FromImpl(_)) |
def::DefMethod(did, _, def::FromImpl(_)) => {
def::DefMethod(did, def::FromImpl(_)) => {
callee::trans_fn_ref(ccx, did, ExprId(ref_expr.id), param_substs)
}
def::DefStaticMethod(impl_did, def::FromTrait(trait_did)) |
def::DefMethod(impl_did, _, def::FromTrait(trait_did)) => {
def::DefMethod(impl_did, def::FromTrait(trait_did)) => {
meth::trans_static_method_callee(ccx, impl_did,
trait_did, ref_expr.id,
param_substs)
......
......@@ -4627,7 +4627,7 @@ fn type_scheme_and_predicates_for_def<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
(ty::TypeScheme { generics: ty::Generics::empty(), ty: typ },
ty::GenericPredicates::empty())
}
def::DefFn(id, _) | def::DefStaticMethod(id, _) | def::DefMethod(id, _, _) |
def::DefFn(id, _) | def::DefMethod(id, _) |
def::DefStatic(id, _) | def::DefVariant(_, id, _) |
def::DefStruct(id) | def::DefConst(id) => {
(ty::lookup_item_type(fcx.tcx(), id), ty::lookup_predicates(fcx.tcx(), id))
......@@ -4751,8 +4751,7 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
}
// Case 3. Reference to a method.
def::DefStaticMethod(_, providence) |
def::DefMethod(_, _, providence) => {
def::DefMethod(_, providence) => {
assert!(path.segments.len() >= 2);
match providence {
......
......@@ -17,9 +17,9 @@ impl Bar { pub fn new() {} }
fn main() {
a::Foo::new();
//~^ ERROR: static method `new` is inaccessible
//~^ ERROR: method `new` is inaccessible
//~^^ NOTE: struct `Foo` is private
a::Bar::new();
//~^ ERROR: static method `new` is inaccessible
//~^ ERROR: method `new` is inaccessible
//~^^ NOTE: enum `Bar` is private
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册