Skip to content

Commit 23bfa60

Browse files
committed
auto merge of #8659 : msullivan/rust/default-methods, r=alexcrichton
2 parents f858452 + 7b08b2c commit 23bfa60

File tree

8 files changed

+92
-21
lines changed

8 files changed

+92
-21
lines changed

src/librustc/middle/liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ impl Liveness {
16171617

16181618
pub fn should_warn(&self, var: Variable) -> Option<@str> {
16191619
let name = self.ir.variable_name(var);
1620-
if name[0] == ('_' as u8) { None } else { Some(name) }
1620+
if name.len() == 0 || name[0] == ('_' as u8) { None } else { Some(name) }
16211621
}
16221622

16231623
pub fn warn_about_unused_args(&self, decl: &fn_decl, entry_ln: LiveNode) {

src/librustc/middle/trans/meth.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,20 +577,23 @@ fn emit_vtable_methods(bcx: @mut Block,
577577

578578
let trait_method_def_ids = ty::trait_method_def_ids(tcx, trt_id);
579579
do trait_method_def_ids.map |method_def_id| {
580-
let im = ty::method(tcx, *method_def_id);
580+
let ident = ty::method(tcx, *method_def_id).ident;
581+
// The substitutions we have are on the impl, so we grab
582+
// the method type from the impl to substitute into.
583+
let m_id = method_with_name(ccx, impl_id, ident);
584+
let m = ty::method(tcx, m_id);
585+
debug!("(making impl vtable) emitting method %s at subst %s",
586+
m.repr(tcx),
587+
substs.repr(tcx));
581588
let fty = ty::subst_tps(tcx,
582589
substs,
583590
None,
584-
ty::mk_bare_fn(tcx, im.fty.clone()));
585-
if im.generics.has_type_params() || ty::type_has_self(fty) {
591+
ty::mk_bare_fn(tcx, m.fty.clone()));
592+
if m.generics.has_type_params() || ty::type_has_self(fty) {
586593
debug!("(making impl vtable) method has self or type params: %s",
587-
tcx.sess.str_of(im.ident));
594+
tcx.sess.str_of(ident));
588595
C_null(Type::nil().ptr_to())
589596
} else {
590-
debug!("(making impl vtable) adding method to vtable: %s",
591-
tcx.sess.str_of(im.ident));
592-
let m_id = method_with_name(ccx, impl_id, im.ident);
593-
594597
trans_fn_ref_with_vtables(bcx, m_id, 0,
595598
substs, Some(vtables)).llfn
596599
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,9 +938,18 @@ impl<'self> LookupContext<'self> {
938938

939939
// static methods should never have gotten this far:
940940
assert!(candidate.method_ty.explicit_self != sty_static);
941-
let transformed_self_ty =
942-
ty::subst(tcx, &candidate.rcvr_substs,
943-
candidate.method_ty.transformed_self_ty.unwrap());
941+
942+
let transformed_self_ty = match candidate.origin {
943+
method_object(*) => {
944+
// For annoying reasons, we've already handled the
945+
// substitution for object calls.
946+
candidate.method_ty.transformed_self_ty.unwrap()
947+
}
948+
_ => {
949+
ty::subst(tcx, &candidate.rcvr_substs,
950+
candidate.method_ty.transformed_self_ty.unwrap())
951+
}
952+
};
944953

945954
// Determine the values for the type parameters of the method.
946955
// If they were not explicitly supplied, just construct fresh

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3092,7 +3092,6 @@ pub fn ty_param_bounds_and_ty_for_def(fcx: @mut FnCtxt,
30923092
sp: span,
30933093
defn: ast::def)
30943094
-> ty_param_bounds_and_ty {
3095-
30963095
match defn {
30973096
ast::def_arg(nid, _) | ast::def_local(nid, _) | ast::def_self(nid, _) |
30983097
ast::def_binding(nid, _) => {
@@ -3149,7 +3148,8 @@ pub fn instantiate_path(fcx: @mut FnCtxt,
31493148
let ty_param_count = tpt.generics.type_param_defs.len();
31503149
let ty_substs_len = pth.types.len();
31513150

3152-
debug!("ty_param_count=%? ty_substs_len=%?",
3151+
debug!("tpt=%s ty_param_count=%? ty_substs_len=%?",
3152+
tpt.repr(fcx.tcx()),
31533153
ty_param_count,
31543154
ty_substs_len);
31553155

src/librustc/middle/typeck/collect.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,10 @@ pub fn ensure_trait_methods(ccx: &CrateCtxt,
346346
let substd_type_param_defs = m.generics.type_param_defs.subst(tcx, &substs);
347347
new_type_param_defs.push_all(*substd_type_param_defs);
348348

349-
debug!("static method %s type_param_defs=%s substs=%s",
349+
debug!("static method %s type_param_defs=%s ty=%s, substs=%s",
350350
m.def_id.repr(tcx),
351351
new_type_param_defs.repr(tcx),
352+
ty.repr(tcx),
352353
substs.repr(tcx));
353354

354355
tcx.tcache.insert(m.def_id,
@@ -893,8 +894,8 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::item) {
893894
}
894895
ast::item_trait(ref generics, _, ref trait_methods) => {
895896
let _trait_def = trait_def_of_item(ccx, it);
896-
ensure_trait_methods(ccx, it.id);
897897

898+
// Run convert_methods on the provided methods.
898899
let (_, provided_methods) =
899900
split_trait_methods(*trait_methods);
900901
let untransformed_rcvr_ty = ty::mk_self(tcx, local_def(it.id));
@@ -904,6 +905,11 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::item) {
904905
untransformed_rcvr_ty,
905906
&ty_generics, generics,
906907
it.vis);
908+
909+
// We need to do this *after* converting methods, since
910+
// convert_methods produces a tcache entry that is wrong for
911+
// static trait methods. This is somewhat unfortunate.
912+
ensure_trait_methods(ccx, it.id);
907913
}
908914
ast::item_struct(struct_def, ref generics) => {
909915
ensure_no_ty_param_bounds(ccx, it.span, generics, "structure");

src/librustpkg/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ fn rustpkg_local_pkg() {
678678
}
679679
680680
#[test]
681+
#[ignore (reason = "test makes bogus assumptions about build directory layout: issue #8690")]
681682
fn package_script_with_default_build() {
682683
let dir = create_local_package(&PkgId::new("fancy-lib"));
683684
debug!("dir = %s", dir.to_str());

src/libsyntax/ast_map.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,18 @@ impl Visitor<()> for Ctx {
238238
self.map.insert(p.ref_id, node_item(i, item_path));
239239
}
240240
for tm in methods.iter() {
241-
let id = ast_util::trait_method_to_ty_method(tm).id;
241+
let ext = { self.extend(i.ident) };
242242
let d_id = ast_util::local_def(i.id);
243-
self.map.insert(id,
244-
node_trait_method(@(*tm).clone(),
245-
d_id,
246-
item_path));
243+
match *tm {
244+
required(ref m) => {
245+
let entry =
246+
node_trait_method(@(*tm).clone(), d_id, ext);
247+
self.map.insert(m.id, entry);
248+
}
249+
provided(m) => {
250+
self.map_method(d_id, ext, m, true);
251+
}
252+
}
247253
}
248254
}
249255
_ => {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// test for #8664
12+
13+
pub trait Trait2<A> {
14+
fn doit(&self);
15+
}
16+
17+
pub struct Impl<A1, A2, A3> {
18+
/*
19+
* With A2 we get the ICE:
20+
* task <unnamed> failed at 'index out of bounds: the len is 1 but the index is 1', /home/tortue/rust_compiler_newest/src/librustc/middle/subst.rs:58
21+
*/
22+
t: ~Trait2<A2>
23+
}
24+
25+
impl<A1, A2, A3> Impl<A1, A2, A3> {
26+
pub fn step(&self) {
27+
self.t.doit()
28+
}
29+
}
30+
31+
// test for #8601
32+
33+
enum Type<T> { Constant }
34+
35+
trait Trait<K,V> {
36+
fn method(&self,Type<(K,V)>) -> int;
37+
}
38+
39+
impl<V> Trait<u8,V> for () {
40+
fn method(&self, _x: Type<(u8,V)>) -> int { 0 }
41+
}
42+
43+
fn main () {
44+
let a = @() as @Trait<u8, u8>;
45+
assert_eq!(a.method(Constant), 0);
46+
}

0 commit comments

Comments
 (0)