Skip to content

Commit

Permalink
Allow self as an arg in extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Oct 15, 2014
1 parent db640d5 commit eb598e5
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 96 deletions.
22 changes: 8 additions & 14 deletions src/librustc/metadata/encoder.rs
Expand Up @@ -407,13 +407,10 @@ fn encode_reexported_static_base_methods(ecx: &EncodeContext,
method_did.def_id());
match impl_item {
ty::MethodTraitItem(ref m) => {
if m.explicit_self ==
ty::StaticExplicitSelfCategory {
encode_reexported_static_method(rbml_w,
exp,
m.def_id,
m.ident);
}
encode_reexported_static_method(rbml_w,
exp,
m.def_id,
m.ident);
}
ty::TypeTraitItem(_) => {}
}
Expand All @@ -434,8 +431,7 @@ fn encode_reexported_static_trait_methods(ecx: &EncodeContext,
Some(trait_items) => {
for trait_item in trait_items.iter() {
match *trait_item {
ty::MethodTraitItem(ref m) if m.explicit_self ==
ty::StaticExplicitSelfCategory => {
ty::MethodTraitItem(ref m) => {
encode_reexported_static_method(rbml_w,
exp,
m.def_id,
Expand Down Expand Up @@ -1408,18 +1404,16 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_family(rbml_w,
fn_style_static_method_family(
method_ty.fty.fn_style));

let pty = ty::lookup_item_type(tcx,
method_def_id);
encode_bounds_and_type(rbml_w, ecx, &pty);
}

_ => {
encode_family(rbml_w,
style_fn_family(
method_ty.fty.fn_style));
}
}
let pty = ty::lookup_item_type(tcx,
method_def_id);
encode_bounds_and_type(rbml_w, ecx, &pty);

is_nonstatic_method = method_ty.explicit_self !=
ty::StaticExplicitSelfCategory;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/astencode.rs
Expand Up @@ -453,8 +453,8 @@ impl tr for def::Def {
},
p)
}
def::DefMethod(did0, did1) => {
def::DefMethod(did0.tr(dcx), did1.map(|did1| did1.tr(dcx)))
def::DefMethod(did0, did1, p) => {
def::DefMethod(did0.tr(dcx), did1.map(|did1| did1.tr(dcx)), p)
}
def::DefSelfTy(nid) => { def::DefSelfTy(dcx.tr_id(nid)) }
def::DefMod(did) => { def::DefMod(did.tr(dcx)) }
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/def.rs
Expand Up @@ -46,7 +46,7 @@ pub enum Def {
DefTyParamBinder(ast::NodeId), /* struct, impl or trait with ty params */
DefRegion(ast::NodeId),
DefLabel(ast::NodeId),
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */),
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
}

#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
Expand All @@ -62,7 +62,7 @@ impl Def {
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/privacy.rs
Expand Up @@ -811,7 +811,7 @@ 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(_, Some(..), _) => ck("trait method"),
def::DefMethod(..) => ck("method"),
def::DefMod(..) => ck("module"),
_ => {}
Expand Down
62 changes: 21 additions & 41 deletions src/librustc/middle/resolve.rs
Expand Up @@ -250,7 +250,7 @@ enum FallbackSuggestion {
Method,
TraitItem,
StaticMethod(String),
StaticTraitMethod(String),
TraitMethod(String),
}

enum TypeParameters<'a> {
Expand Down Expand Up @@ -1386,17 +1386,17 @@ impl<'a> Resolver<'a> {
.node {
SelfStatic => {
// Static methods become
// `def_static_method`s.
DefStaticMethod(
local_def(method.id),
FromImpl(local_def(item.id)),
method.pe_fn_style())
// `DefStaticMethod`s.
DefStaticMethod(local_def(method.id),
FromImpl(local_def(item.id)),
method.pe_fn_style())
}
_ => {
// Non-static methods become
// `def_method`s.
// `DefMethod`s.
DefMethod(local_def(method.id),
None)
None,
FromImpl(local_def(item.id)))
}
};

Expand Down Expand Up @@ -1476,19 +1476,18 @@ impl<'a> Resolver<'a> {
let (def, static_flag) = match ty_m.explicit_self
.node {
SelfStatic => {
// Static methods become
// `def_static_method`s.
// Static methods become `DefStaticMethod`s.
(DefStaticMethod(
local_def(ty_m.id),
FromTrait(local_def(item.id)),
ty_m.fn_style),
StaticMethodTraitItemKind)
}
_ => {
// Non-static methods become
// `def_method`s.
// Non-static methods become `DefMethod`s.
(DefMethod(local_def(ty_m.id),
Some(local_def(item.id))),
Some(local_def(item.id)),
FromTrait(local_def(item.id))),
NonstaticMethodTraitItemKind)
}
};
Expand Down Expand Up @@ -4607,8 +4606,7 @@ impl<'a> Resolver<'a> {
// We also need a new scope for the method-
// specific type parameters.
this.resolve_method(
MethodRibKind(id,
ProvidedMethod(method.id)),
MethodRibKind(id, ProvidedMethod(method.id)),
&**method);
}
TypeImplItem(ref typedef) => {
Expand Down Expand Up @@ -5393,8 +5391,8 @@ impl<'a> Resolver<'a> {

let ident = path.segments.last().unwrap().identifier;
let def = match self.resolve_definition_of_name_in_module(containing_module.clone(),
ident.name,
namespace) {
ident.name,
namespace) {
NoNameDefinition => {
// We failed to resolve the name. Report an error.
return None;
Expand All @@ -5403,26 +5401,6 @@ impl<'a> Resolver<'a> {
(def, last_private.or(lp))
}
};
match containing_module.kind.get() {
TraitModuleKind | ImplModuleKind => {
match containing_module.def_id.get() {
Some(def_id) => {
match self.trait_item_map.find(&(ident.name, def_id)) {
Some(&StaticMethodTraitItemKind) => (),
Some(&TypeTraitItemKind) => (),
None => (),
Some(&NonstaticMethodTraitItemKind) => {
debug!("containing module was a trait or impl \
and name was a method -> not resolved");
return None;
}
}
},
_ => (),
}
},
_ => (),
}
match containing_module.def_id.get() {
Some(DefId{krate: kid, ..}) => { self.used_crates.insert(kid); },
_ => {}
Expand Down Expand Up @@ -5668,8 +5646,8 @@ impl<'a> Resolver<'a> {
FromTrait(_) => unreachable!()
}
}
Some(DefMethod(_, None)) if allowed == Everything => return Method,
Some(DefMethod(_, Some(_))) => return TraitItem,
Some(DefMethod(_, None, _)) if allowed == Everything => return Method,
Some(DefMethod(_, Some(_), _)) => return TraitItem,
_ => ()
}
}
Expand All @@ -5684,7 +5662,9 @@ impl<'a> Resolver<'a> {
let path_str = self.path_idents_to_string(&trait_ref.path);

match self.trait_item_map.find(&(name, did)) {
Some(&StaticMethodTraitItemKind) => return StaticTraitMethod(path_str),
Some(&StaticMethodTraitItemKind) => {
return TraitMethod(path_str)
}
Some(_) => return TraitItem,
None => {}
}
Expand Down Expand Up @@ -5810,7 +5790,7 @@ impl<'a> Resolver<'a> {
Method
| TraitItem =>
format!("to call `self.{}`", wrong_name),
StaticTraitMethod(path_str)
TraitMethod(path_str)
| StaticMethod(path_str) =>
format!("to call `{}::{}`", path_str, wrong_name)
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/save/mod.rs
Expand Up @@ -244,7 +244,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
def::DefStaticMethod(_, _, _) |
def::DefTyParam(..) |
def::DefUse(_) |
def::DefMethod(_, _) |
def::DefMethod(..) |
def::DefPrimTy(_) => {
self.sess.span_bug(span, format!("lookup_def_kind for unexpected item: {:?}",
def).as_slice());
Expand Down
13 changes: 7 additions & 6 deletions src/librustc/middle/trans/callee.rs
Expand Up @@ -137,7 +137,9 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
};
}

fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, def: def::Def, ref_expr: &ast::Expr)
fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
def: def::Def,
ref_expr: &ast::Expr)
-> Callee<'blk, 'tcx> {
debug!("trans_def(def={}, ref_expr={})", def.repr(bcx.tcx()), ref_expr.repr(bcx.tcx()));
let expr_ty = node_id_type(bcx, ref_expr.id);
Expand Down Expand Up @@ -165,14 +167,13 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
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::DefFn(did, _, _) | def::DefMethod(did, _, def::FromImpl(_)) |
def::DefStaticMethod(did, def::FromImpl(_), _) => {
fn_callee(bcx, trans_fn_ref(bcx, did, ExprId(ref_expr.id)))
}
def::DefStaticMethod(impl_did,
def::FromTrait(trait_did),
_) => {
fn_callee(bcx, meth::trans_static_method_callee(bcx, impl_did,
def::DefStaticMethod(meth_did, def::FromTrait(trait_did), _) |
def::DefMethod(meth_did, _, def::FromTrait(trait_did)) => {
fn_callee(bcx, meth::trans_static_method_callee(bcx, meth_did,
trait_did,
ref_expr.id))
}
Expand Down
8 changes: 3 additions & 5 deletions src/librustc/middle/typeck/check/mod.rs
Expand Up @@ -5027,10 +5027,9 @@ pub fn polytype_for_def(fcx: &FnCtxt,
let typ = fcx.local_ty(sp, nid);
return no_params(typ);
}
def::DefFn(id, _, _) | def::DefStaticMethod(id, _, _) |
def::DefFn(id, _, _) | def::DefStaticMethod(id, _, _) | def::DefMethod(id, _, _) |
def::DefStatic(id, _) | def::DefVariant(_, id, _) |
def::DefStruct(id) | def::DefConst(id) |
def::DefMethod(id, _) => {
def::DefStruct(id) | def::DefConst(id) => {
return ty::lookup_item_type(fcx.ccx.tcx, id);
}
def::DefTrait(_) |
Expand Down Expand Up @@ -5229,8 +5228,7 @@ pub fn instantiate_path(fcx: &FnCtxt,
}

fcx.add_obligations_for_parameters(
traits::ObligationCause::new(span,
traits::ItemObligation(def.def_id())),
traits::ObligationCause::new(span, traits::ItemObligation(def.def_id())),
&substs,
&polytype.generics);

Expand Down
13 changes: 6 additions & 7 deletions src/librustc/middle/typeck/collect.rs
Expand Up @@ -77,10 +77,12 @@ pub fn collect_item_types(ccx: &CrateCtxt) {
}

match ccx.tcx.lang_items.ty_desc() {
Some(id) => { collect_intrinsic_type(ccx, id); } None => {}
Some(id) => { collect_intrinsic_type(ccx, id); }
None => {}
}
match ccx.tcx.lang_items.opaque() {
Some(id) => { collect_intrinsic_type(ccx, id); } None => {}
Some(id) => { collect_intrinsic_type(ccx, id); }
None => {}
}

let mut visitor = CollectTraitDefVisitor{ ccx: ccx };
Expand Down Expand Up @@ -306,10 +308,7 @@ fn collect_trait_methods(ccx: &CrateCtxt,
}
});

if ty_method.explicit_self ==
ty::StaticExplicitSelfCategory {
make_static_method_ty(ccx, &*ty_method);
}
make_method_ty(ccx, &*ty_method);

tcx.impl_or_trait_items
.borrow_mut()
Expand Down Expand Up @@ -364,7 +363,7 @@ fn collect_trait_methods(ccx: &CrateCtxt,
_ => { /* Ignore things that aren't traits */ }
}

fn make_static_method_ty(ccx: &CrateCtxt, m: &ty::Method) {
fn make_method_ty(ccx: &CrateCtxt, m: &ty::Method) {
ccx.tcx.tcache.borrow_mut().insert(
m.def_id,
Polytype {
Expand Down
17 changes: 0 additions & 17 deletions src/test/compile-fail/call-extern-trait-as-function.rs

This file was deleted.

0 comments on commit eb598e5

Please sign in to comment.