Skip to content

Commit b334f7c

Browse files
committed
auto merge of #13157 : pnkfelix/rust/fsk-iss13140, r=nikomatsakis
r? @nikomatsakis Fix #13140 Includes two fixes, and a semi-thorough regression test. (There is another set of tests that I linked from #5121, but those are sort of all over the place, while the ones I've included here are more directly focused on the issues at hand.)
2 parents ff733c7 + 0cbb1ce commit b334f7c

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ fn construct_transformed_self_ty_for_object(
263263
let transformed_self_ty = *method_ty.fty.sig.inputs.get(0);
264264
match ty::get(transformed_self_ty).sty {
265265
ty::ty_rptr(r, mt) => { // must be SelfRegion
266+
let r = r.subst(tcx, &substs); // handle Early-Bound lifetime
266267
ty::mk_trait(tcx, trait_def_id, substs,
267268
RegionTraitStore(r), mt.mutbl,
268269
ty::EmptyBuiltinBounds())
@@ -1435,9 +1436,10 @@ impl<'a> LookupContext<'a> {
14351436

14361437
impl Repr for Candidate {
14371438
fn repr(&self, tcx: &ty::ctxt) -> ~str {
1438-
format!("Candidate(rcvr_ty={}, rcvr_substs={}, origin={:?})",
1439+
format!("Candidate(rcvr_ty={}, rcvr_substs={}, method_ty={}, origin={:?})",
14391440
self.rcvr_match_condition.repr(tcx),
14401441
self.rcvr_substs.repr(tcx),
1442+
self.method_ty.repr(tcx),
14411443
self.origin)
14421444
}
14431445
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ fn check_bare_fn(ccx: &CrateCtxt,
329329
id: ast::NodeId,
330330
fty: ty::t,
331331
param_env: ty::ParameterEnvironment) {
332+
// Compute the fty from point of view of inside fn
333+
// (replace any type-scheme with a type)
334+
let fty = fty.subst(ccx.tcx, &param_env.free_substs);
335+
332336
match ty::get(fty).sty {
333337
ty::ty_bare_fn(ref fn_ty) => {
334338
let inh = Inherited::new(ccx.tcx, param_env);
@@ -678,9 +682,7 @@ fn check_method_body(ccx: &CrateCtxt,
678682
method_generics.region_param_defs(),
679683
method.body.id);
680684

681-
// Compute the fty from point of view of inside fn
682685
let fty = ty::node_id_to_type(ccx.tcx, method.id);
683-
let fty = fty.subst(ccx.tcx, &param_env.free_substs);
684686

685687
check_bare_fn(ccx, method.decl, method.body, method.id, fty, param_env);
686688
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright 2014 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+
// Tests that you can use an early-bound lifetime parameter as
12+
// on of the generic parameters in a trait.
13+
14+
trait Trait<'a> {
15+
fn long(&'a self) -> int;
16+
fn short<'b>(&'b self) -> int;
17+
}
18+
19+
fn poly_invoke<'c, T: Trait<'c>>(x: &'c T) -> (int, int) {
20+
let l = x.long();
21+
let s = x.short();
22+
(l,s)
23+
}
24+
25+
fn object_invoke1<'d>(x: &'d Trait<'d>) -> (int, int) {
26+
let l = x.long();
27+
let s = x.short();
28+
(l,s)
29+
}
30+
31+
struct Struct1<'e> {
32+
f: &'e Trait<'e>
33+
}
34+
35+
fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (int,int) {
36+
let l = x.f.long();
37+
let s = x.f.short();
38+
(l,s)
39+
}
40+
41+
struct Struct2<'h, 'i> {
42+
f: &'h Trait<'i>
43+
}
44+
45+
fn object_invoke2<'j, 'k>(x: &'k Trait<'j>) -> int {
46+
x.short()
47+
}
48+
49+
fn field_invoke2<'l, 'm, 'n>(x: &'n Struct2<'l,'m>) -> int {
50+
x.f.short()
51+
}
52+
53+
trait MakerTrait<'o> {
54+
fn mk() -> Self;
55+
}
56+
57+
fn make_val<'p, T:MakerTrait<'p>>() -> T {
58+
MakerTrait::mk()
59+
}
60+
61+
trait RefMakerTrait<'q> {
62+
fn mk(Self) -> &'q Self;
63+
}
64+
65+
fn make_ref<'r, T:RefMakerTrait<'r>>(t:T) -> &'r T {
66+
RefMakerTrait::mk(t)
67+
}
68+
69+
impl<'s> Trait<'s> for (int,int) {
70+
fn long(&'s self) -> int {
71+
let &(x,_) = self;
72+
x
73+
}
74+
fn short<'b>(&'b self) -> int {
75+
let &(_,y) = self;
76+
y
77+
}
78+
}
79+
80+
impl<'t> MakerTrait<'t> for ~Trait<'t> {
81+
fn mk() -> ~Trait<'t> { ~(4,5) as ~Trait }
82+
}
83+
84+
enum List<'l> {
85+
Cons(int, &'l List<'l>),
86+
Null
87+
}
88+
89+
impl<'l> List<'l> {
90+
fn car<'m>(&'m self) -> int {
91+
match self {
92+
&Cons(car, _) => car,
93+
&Null => fail!(),
94+
}
95+
}
96+
fn cdr<'n>(&'n self) -> &'l List<'l> {
97+
match self {
98+
&Cons(_, cdr) => cdr,
99+
&Null => fail!(),
100+
}
101+
}
102+
}
103+
104+
impl<'t> RefMakerTrait<'t> for List<'t> {
105+
fn mk(l:List<'t>) -> &'t List<'t> {
106+
l.cdr()
107+
}
108+
}
109+
110+
pub fn main() {
111+
let t = (2,3);
112+
let o = &t as &Trait;
113+
let s1 = Struct1 { f: o };
114+
let s2 = Struct2 { f: o };
115+
assert_eq!(poly_invoke(&t), (2,3));
116+
assert_eq!(object_invoke1(&t), (2,3));
117+
assert_eq!(field_invoke1(&s1), (2,3));
118+
assert_eq!(object_invoke2(&t), 3);
119+
assert_eq!(field_invoke2(&s2), 3);
120+
121+
let m : ~Trait = make_val();
122+
assert_eq!(object_invoke1(m), (4,5));
123+
assert_eq!(object_invoke2(m), 5);
124+
125+
// The RefMakerTrait above is pretty strange (i.e. it is strange
126+
// to consume a value of type T and return a &T). Easiest thing
127+
// that came to my mind: consume a cell of a linked list and
128+
// return a reference to the list it points to.
129+
let l0 = Null;
130+
let l1 = Cons(1, &l0);
131+
let l2 = Cons(2, &l1);
132+
let rl1 = &l1;
133+
let r = make_ref(l2);
134+
assert_eq!(rl1.car(), r.car());
135+
}

0 commit comments

Comments
 (0)