Skip to content

Commit 71dfa5b

Browse files
committed
auto merge of #18053 : nick29581/rust/ufcs1, r=pcwalton
With the 'receiver' as an argument and static dispatch. Part of UFCS implementation (#16293). r?
2 parents 5201bf1 + fe8e591 commit 71dfa5b

File tree

14 files changed

+231
-114
lines changed

14 files changed

+231
-114
lines changed

src/librustc/metadata/encoder.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,10 @@ fn encode_reexported_static_base_methods(ecx: &EncodeContext,
407407
method_did.def_id());
408408
match impl_item {
409409
ty::MethodTraitItem(ref m) => {
410-
if m.explicit_self ==
411-
ty::StaticExplicitSelfCategory {
412-
encode_reexported_static_method(rbml_w,
413-
exp,
414-
m.def_id,
415-
m.ident);
416-
}
410+
encode_reexported_static_method(rbml_w,
411+
exp,
412+
m.def_id,
413+
m.ident);
417414
}
418415
ty::TypeTraitItem(_) => {}
419416
}
@@ -434,8 +431,7 @@ fn encode_reexported_static_trait_methods(ecx: &EncodeContext,
434431
Some(trait_items) => {
435432
for trait_item in trait_items.iter() {
436433
match *trait_item {
437-
ty::MethodTraitItem(ref m) if m.explicit_self ==
438-
ty::StaticExplicitSelfCategory => {
434+
ty::MethodTraitItem(ref m) => {
439435
encode_reexported_static_method(rbml_w,
440436
exp,
441437
m.def_id,
@@ -1408,18 +1404,16 @@ fn encode_info_for_item(ecx: &EncodeContext,
14081404
encode_family(rbml_w,
14091405
fn_style_static_method_family(
14101406
method_ty.fty.fn_style));
1411-
1412-
let pty = ty::lookup_item_type(tcx,
1413-
method_def_id);
1414-
encode_bounds_and_type(rbml_w, ecx, &pty);
14151407
}
1416-
14171408
_ => {
14181409
encode_family(rbml_w,
14191410
style_fn_family(
14201411
method_ty.fty.fn_style));
14211412
}
14221413
}
1414+
let pty = ty::lookup_item_type(tcx,
1415+
method_def_id);
1416+
encode_bounds_and_type(rbml_w, ecx, &pty);
14231417

14241418
is_nonstatic_method = method_ty.explicit_self !=
14251419
ty::StaticExplicitSelfCategory;

src/librustc/middle/astencode.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ impl tr for def::Def {
453453
},
454454
p)
455455
}
456-
def::DefMethod(did0, did1) => {
457-
def::DefMethod(did0.tr(dcx), did1.map(|did1| did1.tr(dcx)))
456+
def::DefMethod(did0, did1, p) => {
457+
def::DefMethod(did0.tr(dcx), did1.map(|did1| did1.tr(dcx)), p)
458458
}
459459
def::DefSelfTy(nid) => { def::DefSelfTy(dcx.tr_id(nid)) }
460460
def::DefMod(did) => { def::DefMod(did.tr(dcx)) }

src/librustc/middle/def.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub enum Def {
4646
DefTyParamBinder(ast::NodeId), /* struct, impl or trait with ty params */
4747
DefRegion(ast::NodeId),
4848
DefLabel(ast::NodeId),
49-
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */),
49+
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */, MethodProvenance),
5050
}
5151

5252
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
@@ -62,7 +62,7 @@ impl Def {
6262
DefForeignMod(id) | DefStatic(id, _) |
6363
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(id) |
6464
DefTyParam(_, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
65-
DefMethod(id, _) | DefConst(id) => {
65+
DefMethod(id, _, _) | DefConst(id) => {
6666
id
6767
}
6868
DefLocal(id) |

src/librustc/middle/privacy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
811811
def::DefTy(_, true) => ck("enum"),
812812
def::DefTrait(..) => ck("trait"),
813813
def::DefStruct(..) => ck("struct"),
814-
def::DefMethod(_, Some(..)) => ck("trait method"),
814+
def::DefMethod(_, Some(..), _) => ck("trait method"),
815815
def::DefMethod(..) => ck("method"),
816816
def::DefMod(..) => ck("module"),
817817
_ => {}

src/librustc/middle/resolve.rs

+21-57
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ enum FallbackSuggestion {
250250
Method,
251251
TraitItem,
252252
StaticMethod(String),
253-
StaticTraitMethod(String),
253+
TraitMethod(String),
254254
}
255255

256256
enum TypeParameters<'a> {
@@ -1386,17 +1386,17 @@ impl<'a> Resolver<'a> {
13861386
.node {
13871387
SelfStatic => {
13881388
// Static methods become
1389-
// `def_static_method`s.
1390-
DefStaticMethod(
1391-
local_def(method.id),
1392-
FromImpl(local_def(item.id)),
1393-
method.pe_fn_style())
1389+
// `DefStaticMethod`s.
1390+
DefStaticMethod(local_def(method.id),
1391+
FromImpl(local_def(item.id)),
1392+
method.pe_fn_style())
13941393
}
13951394
_ => {
13961395
// Non-static methods become
1397-
// `def_method`s.
1396+
// `DefMethod`s.
13981397
DefMethod(local_def(method.id),
1399-
None)
1398+
None,
1399+
FromImpl(local_def(item.id)))
14001400
}
14011401
};
14021402

@@ -1476,19 +1476,18 @@ impl<'a> Resolver<'a> {
14761476
let (def, static_flag) = match ty_m.explicit_self
14771477
.node {
14781478
SelfStatic => {
1479-
// Static methods become
1480-
// `def_static_method`s.
1479+
// Static methods become `DefStaticMethod`s.
14811480
(DefStaticMethod(
14821481
local_def(ty_m.id),
14831482
FromTrait(local_def(item.id)),
14841483
ty_m.fn_style),
14851484
StaticMethodTraitItemKind)
14861485
}
14871486
_ => {
1488-
// Non-static methods become
1489-
// `def_method`s.
1487+
// Non-static methods become `DefMethod`s.
14901488
(DefMethod(local_def(ty_m.id),
1491-
Some(local_def(item.id))),
1489+
Some(local_def(item.id)),
1490+
FromTrait(local_def(item.id))),
14921491
NonstaticMethodTraitItemKind)
14931492
}
14941493
};
@@ -4607,8 +4606,7 @@ impl<'a> Resolver<'a> {
46074606
// We also need a new scope for the method-
46084607
// specific type parameters.
46094608
this.resolve_method(
4610-
MethodRibKind(id,
4611-
ProvidedMethod(method.id)),
4609+
MethodRibKind(id, ProvidedMethod(method.id)),
46124610
&**method);
46134611
}
46144612
TypeImplItem(ref typedef) => {
@@ -5393,8 +5391,8 @@ impl<'a> Resolver<'a> {
53935391

53945392
let ident = path.segments.last().unwrap().identifier;
53955393
let def = match self.resolve_definition_of_name_in_module(containing_module.clone(),
5396-
ident.name,
5397-
namespace) {
5394+
ident.name,
5395+
namespace) {
53985396
NoNameDefinition => {
53995397
// We failed to resolve the name. Report an error.
54005398
return None;
@@ -5403,26 +5401,6 @@ impl<'a> Resolver<'a> {
54035401
(def, last_private.or(lp))
54045402
}
54055403
};
5406-
match containing_module.kind.get() {
5407-
TraitModuleKind | ImplModuleKind => {
5408-
match containing_module.def_id.get() {
5409-
Some(def_id) => {
5410-
match self.trait_item_map.find(&(ident.name, def_id)) {
5411-
Some(&StaticMethodTraitItemKind) => (),
5412-
Some(&TypeTraitItemKind) => (),
5413-
None => (),
5414-
Some(&NonstaticMethodTraitItemKind) => {
5415-
debug!("containing module was a trait or impl \
5416-
and name was a method -> not resolved");
5417-
return None;
5418-
}
5419-
}
5420-
},
5421-
_ => (),
5422-
}
5423-
},
5424-
_ => (),
5425-
}
54265404
match containing_module.def_id.get() {
54275405
Some(DefId{krate: kid, ..}) => { self.used_crates.insert(kid); },
54285406
_ => {}
@@ -5668,8 +5646,8 @@ impl<'a> Resolver<'a> {
56685646
FromTrait(_) => unreachable!()
56695647
}
56705648
}
5671-
Some(DefMethod(_, None)) if allowed == Everything => return Method,
5672-
Some(DefMethod(_, Some(_))) => return TraitItem,
5649+
Some(DefMethod(_, None, _)) if allowed == Everything => return Method,
5650+
Some(DefMethod(_, Some(_), _)) => return TraitItem,
56735651
_ => ()
56745652
}
56755653
}
@@ -5684,7 +5662,9 @@ impl<'a> Resolver<'a> {
56845662
let path_str = self.path_idents_to_string(&trait_ref.path);
56855663

56865664
match self.trait_item_map.find(&(name, did)) {
5687-
Some(&StaticMethodTraitItemKind) => return StaticTraitMethod(path_str),
5665+
Some(&StaticMethodTraitItemKind) => {
5666+
return TraitMethod(path_str)
5667+
}
56885668
Some(_) => return TraitItem,
56895669
None => {}
56905670
}
@@ -5751,22 +5731,6 @@ impl<'a> Resolver<'a> {
57515731
// Write the result into the def map.
57525732
debug!("(resolving expr) resolved `{}`",
57535733
self.path_idents_to_string(path));
5754-
5755-
// First-class methods are not supported yet; error
5756-
// out here.
5757-
match def {
5758-
(DefMethod(..), _) => {
5759-
self.resolve_error(expr.span,
5760-
"first-class methods \
5761-
are not supported");
5762-
self.session.span_note(expr.span,
5763-
"call the method \
5764-
using the `.` \
5765-
syntax");
5766-
}
5767-
_ => {}
5768-
}
5769-
57705734
self.record_def(expr.id, def);
57715735
}
57725736
None => {
@@ -5826,7 +5790,7 @@ impl<'a> Resolver<'a> {
58265790
Method
58275791
| TraitItem =>
58285792
format!("to call `self.{}`", wrong_name),
5829-
StaticTraitMethod(path_str)
5793+
TraitMethod(path_str)
58305794
| StaticMethod(path_str) =>
58315795
format!("to call `{}::{}`", path_str, wrong_name)
58325796
};

src/librustc/middle/save/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
244244
def::DefStaticMethod(_, _, _) |
245245
def::DefTyParam(..) |
246246
def::DefUse(_) |
247-
def::DefMethod(_, _) |
247+
def::DefMethod(..) |
248248
def::DefPrimTy(_) => {
249249
self.sess.span_bug(span, format!("lookup_def_kind for unexpected item: {:?}",
250250
def).as_slice());

src/librustc/middle/trans/callee.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
137137
};
138138
}
139139

140-
fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, def: def::Def, ref_expr: &ast::Expr)
140+
fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
141+
def: def::Def,
142+
ref_expr: &ast::Expr)
141143
-> Callee<'blk, 'tcx> {
142144
debug!("trans_def(def={}, ref_expr={})", def.repr(bcx.tcx()), ref_expr.repr(bcx.tcx()));
143145
let expr_ty = node_id_type(bcx, ref_expr.id);
@@ -165,14 +167,13 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
165167
let def_id = inline::maybe_instantiate_inline(bcx.ccx(), did);
166168
Callee { bcx: bcx, data: Intrinsic(def_id.node, substs) }
167169
}
168-
def::DefFn(did, _, _) |
170+
def::DefFn(did, _, _) | def::DefMethod(did, _, def::FromImpl(_)) |
169171
def::DefStaticMethod(did, def::FromImpl(_), _) => {
170172
fn_callee(bcx, trans_fn_ref(bcx, did, ExprId(ref_expr.id)))
171173
}
172-
def::DefStaticMethod(impl_did,
173-
def::FromTrait(trait_did),
174-
_) => {
175-
fn_callee(bcx, meth::trans_static_method_callee(bcx, impl_did,
174+
def::DefStaticMethod(meth_did, def::FromTrait(trait_did), _) |
175+
def::DefMethod(meth_did, _, def::FromTrait(trait_did)) => {
176+
fn_callee(bcx, meth::trans_static_method_callee(bcx, meth_did,
176177
trait_did,
177178
ref_expr.id))
178179
}
@@ -205,7 +206,7 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
205206
def::DefTy(..) | def::DefPrimTy(..) | def::DefAssociatedTy(..) |
206207
def::DefUse(..) | def::DefTyParamBinder(..) |
207208
def::DefRegion(..) | def::DefLabel(..) | def::DefTyParam(..) |
208-
def::DefSelfTy(..) | def::DefMethod(..) => {
209+
def::DefSelfTy(..) => {
209210
bcx.tcx().sess.span_bug(
210211
ref_expr.span,
211212
format!("cannot translate def {:?} \

src/librustc/middle/typeck/check/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -5027,7 +5027,7 @@ pub fn polytype_for_def(fcx: &FnCtxt,
50275027
let typ = fcx.local_ty(sp, nid);
50285028
return no_params(typ);
50295029
}
5030-
def::DefFn(id, _, _) | def::DefStaticMethod(id, _, _) |
5030+
def::DefFn(id, _, _) | def::DefStaticMethod(id, _, _) | def::DefMethod(id, _, _) |
50315031
def::DefStatic(id, _) | def::DefVariant(_, id, _) |
50325032
def::DefStruct(id) | def::DefConst(id) => {
50335033
return ty::lookup_item_type(fcx.ccx.tcx, id);
@@ -5057,9 +5057,6 @@ pub fn polytype_for_def(fcx: &FnCtxt,
50575057
def::DefSelfTy(..) => {
50585058
fcx.ccx.tcx.sess.span_bug(sp, "expected value, found self ty");
50595059
}
5060-
def::DefMethod(..) => {
5061-
fcx.ccx.tcx.sess.span_bug(sp, "expected value, found method");
5062-
}
50635060
}
50645061
}
50655062

@@ -5231,8 +5228,7 @@ pub fn instantiate_path(fcx: &FnCtxt,
52315228
}
52325229

52335230
fcx.add_obligations_for_parameters(
5234-
traits::ObligationCause::new(span,
5235-
traits::ItemObligation(def.def_id())),
5231+
traits::ObligationCause::new(span, traits::ItemObligation(def.def_id())),
52365232
&substs,
52375233
&polytype.generics);
52385234

src/librustc/middle/typeck/collect.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ pub fn collect_item_types(ccx: &CrateCtxt) {
7777
}
7878

7979
match ccx.tcx.lang_items.ty_desc() {
80-
Some(id) => { collect_intrinsic_type(ccx, id); } None => {}
80+
Some(id) => { collect_intrinsic_type(ccx, id); }
81+
None => {}
8182
}
8283
match ccx.tcx.lang_items.opaque() {
83-
Some(id) => { collect_intrinsic_type(ccx, id); } None => {}
84+
Some(id) => { collect_intrinsic_type(ccx, id); }
85+
None => {}
8486
}
8587

8688
let mut visitor = CollectTraitDefVisitor{ ccx: ccx };
@@ -306,10 +308,7 @@ fn collect_trait_methods(ccx: &CrateCtxt,
306308
}
307309
});
308310

309-
if ty_method.explicit_self ==
310-
ty::StaticExplicitSelfCategory {
311-
make_static_method_ty(ccx, &*ty_method);
312-
}
311+
make_method_ty(ccx, &*ty_method);
313312

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

367-
fn make_static_method_ty(ccx: &CrateCtxt, m: &ty::Method) {
366+
fn make_method_ty(ccx: &CrateCtxt, m: &ty::Method) {
368367
ccx.tcx.tcache.borrow_mut().insert(
369368
m.def_id,
370369
Polytype {

src/test/compile-fail/call-extern-trait-as-function.rs

-17
This file was deleted.

0 commit comments

Comments
 (0)