Skip to content

Commit

Permalink
rustc_resolve: remove the distinction between DefStaticMethod and Def…
Browse files Browse the repository at this point in the history
…Method.
  • Loading branch information
eddyb committed Feb 24, 2015
1 parent 0f49254 commit 7a3054f
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 159 deletions.
7 changes: 2 additions & 5 deletions src/librustc/metadata/csearch.rs
Expand Up @@ -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)
Expand Down
22 changes: 4 additions & 18 deletions src/librustc/metadata/decoder.rs
Expand Up @@ -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') {
Expand Down Expand Up @@ -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
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/librustc/middle/astencode.rs
Expand Up @@ -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)) }
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/check_const.rs
Expand Up @@ -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);
}
Expand Down
29 changes: 3 additions & 26 deletions src/librustc/middle/def.rs
Expand Up @@ -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;
Expand All @@ -23,7 +21,6 @@ use std::cell::RefCell;
#[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),
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -99,25 +96,6 @@ impl MethodProvenance {
}
}

#[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();
Expand All @@ -127,11 +105,10 @@ impl Def {

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) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/mem_categorization.rs
Expand Up @@ -575,7 +575,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {

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(_) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/ty.rs
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_privacy/lib.rs
Expand Up @@ -795,7 +795,6 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
// 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"),
Expand All @@ -804,7 +803,6 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
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"),
_ => {}
Expand Down
65 changes: 17 additions & 48 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -40,8 +40,7 @@ use syntax::ast::{Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn};
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;
Expand Down Expand Up @@ -598,22 +597,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
&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 {
Expand Down Expand Up @@ -674,31 +659,16 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {

// 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);

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,
Expand All @@ -710,7 +680,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
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),
Expand All @@ -726,11 +696,12 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
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);
Expand Down Expand Up @@ -889,7 +860,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
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
Expand All @@ -911,21 +882,19 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {

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());
}
}

Expand Down

0 comments on commit 7a3054f

Please sign in to comment.