From 6b5ca8b1aa325e416a5818b40091e96fe00abbde Mon Sep 17 00:00:00 2001
From: "Felix S. Klock II" <pnkfelix@pnkfx.org>
Date: Tue, 25 Mar 2014 18:37:18 +0100
Subject: [PATCH 1/3] Fix #13140: Early/Late Bound related ICEs.

The problem was that we need to apply the substitution, so that the
formal lifetime parameters get replaced with (unifiable)
free-lifetimes that can actually be fed into the constraint solver.

Also, refactor code os that substitution for `check_item` and
`check_method`, moving both down the control flow into `check_bare_fn`.

----

Finally, there was another (similar) spot where we needed to
substitute early-bound lifetimes when invoking an object method of a
trait.
---
 src/librustc/middle/typeck/check/method.rs | 1 +
 src/librustc/middle/typeck/check/mod.rs    | 6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/librustc/middle/typeck/check/method.rs b/src/librustc/middle/typeck/check/method.rs
index 9b623f1e78e..73cecf77602 100644
--- a/src/librustc/middle/typeck/check/method.rs
+++ b/src/librustc/middle/typeck/check/method.rs
@@ -263,6 +263,7 @@ fn construct_transformed_self_ty_for_object(
             let transformed_self_ty = *method_ty.fty.sig.inputs.get(0);
             match ty::get(transformed_self_ty).sty {
                 ty::ty_rptr(r, mt) => { // must be SelfRegion
+                    let r = r.subst(tcx, &substs); // handle Early-Bound lifetime
                     ty::mk_trait(tcx, trait_def_id, substs,
                                  RegionTraitStore(r), mt.mutbl,
                                  ty::EmptyBuiltinBounds())
diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs
index 2410390b5e7..3f1d3e29c54 100644
--- a/src/librustc/middle/typeck/check/mod.rs
+++ b/src/librustc/middle/typeck/check/mod.rs
@@ -329,6 +329,10 @@ fn check_bare_fn(ccx: &CrateCtxt,
                  id: ast::NodeId,
                  fty: ty::t,
                  param_env: ty::ParameterEnvironment) {
+    // Compute the fty from point of view of inside fn
+    // (replace any type-scheme with a type)
+    let fty = fty.subst(ccx.tcx, &param_env.free_substs);
+
     match ty::get(fty).sty {
         ty::ty_bare_fn(ref fn_ty) => {
             let inh = Inherited::new(ccx.tcx, param_env);
@@ -678,9 +682,7 @@ fn check_method_body(ccx: &CrateCtxt,
             method_generics.region_param_defs(),
             method.body.id);
 
-    // Compute the fty from point of view of inside fn
     let fty = ty::node_id_to_type(ccx.tcx, method.id);
-    let fty = fty.subst(ccx.tcx, &param_env.free_substs);
 
     check_bare_fn(ccx, method.decl, method.body, method.id, fty, param_env);
 }

From ab8e02616cb3f2608208feea9627cb981377ac0d Mon Sep 17 00:00:00 2001
From: "Felix S. Klock II" <pnkfelix@pnkfx.org>
Date: Wed, 26 Mar 2014 12:00:52 +0100
Subject: [PATCH 2/3] Fix Repr impl for method::Candidate to include the
 method_ty.

---
 src/librustc/middle/typeck/check/method.rs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/librustc/middle/typeck/check/method.rs b/src/librustc/middle/typeck/check/method.rs
index 73cecf77602..9f1a5506b02 100644
--- a/src/librustc/middle/typeck/check/method.rs
+++ b/src/librustc/middle/typeck/check/method.rs
@@ -1436,9 +1436,10 @@ impl<'a> LookupContext<'a> {
 
 impl Repr for Candidate {
     fn repr(&self, tcx: &ty::ctxt) -> ~str {
-        format!("Candidate(rcvr_ty={}, rcvr_substs={}, origin={:?})",
+        format!("Candidate(rcvr_ty={}, rcvr_substs={}, method_ty={}, origin={:?})",
                 self.rcvr_match_condition.repr(tcx),
                 self.rcvr_substs.repr(tcx),
+                self.method_ty.repr(tcx),
                 self.origin)
     }
 }

From 0cbb1ceaa99dd7065b1237357e828c57ef4135c9 Mon Sep 17 00:00:00 2001
From: "Felix S. Klock II" <pnkfelix@pnkfx.org>
Date: Wed, 26 Mar 2014 10:14:08 +0100
Subject: [PATCH 3/3] Add test using early-bound lifetimes in trait generic
 parameters.

---
 .../regions-early-bound-trait-param.rs        | 135 ++++++++++++++++++
 1 file changed, 135 insertions(+)
 create mode 100644 src/test/run-pass/regions-early-bound-trait-param.rs

diff --git a/src/test/run-pass/regions-early-bound-trait-param.rs b/src/test/run-pass/regions-early-bound-trait-param.rs
new file mode 100644
index 00000000000..f3cfa0b9d33
--- /dev/null
+++ b/src/test/run-pass/regions-early-bound-trait-param.rs
@@ -0,0 +1,135 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Tests that you can use an early-bound lifetime parameter as
+// on of the generic parameters in a trait.
+
+trait Trait<'a> {
+    fn long(&'a self) -> int;
+    fn short<'b>(&'b self) -> int;
+}
+
+fn poly_invoke<'c, T: Trait<'c>>(x: &'c T) -> (int, int) {
+    let l = x.long();
+    let s = x.short();
+    (l,s)
+}
+
+fn object_invoke1<'d>(x: &'d Trait<'d>) -> (int, int) {
+    let l = x.long();
+    let s = x.short();
+    (l,s)
+}
+
+struct Struct1<'e> {
+    f: &'e Trait<'e>
+}
+
+fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (int,int) {
+    let l = x.f.long();
+    let s = x.f.short();
+    (l,s)
+}
+
+struct Struct2<'h, 'i> {
+    f: &'h Trait<'i>
+}
+
+fn object_invoke2<'j, 'k>(x: &'k Trait<'j>) -> int {
+    x.short()
+}
+
+fn field_invoke2<'l, 'm, 'n>(x: &'n Struct2<'l,'m>) -> int {
+    x.f.short()
+}
+
+trait MakerTrait<'o> {
+    fn mk() -> Self;
+}
+
+fn make_val<'p, T:MakerTrait<'p>>() -> T {
+    MakerTrait::mk()
+}
+
+trait RefMakerTrait<'q> {
+    fn mk(Self) -> &'q Self;
+}
+
+fn make_ref<'r, T:RefMakerTrait<'r>>(t:T) -> &'r T {
+    RefMakerTrait::mk(t)
+}
+
+impl<'s> Trait<'s> for (int,int) {
+    fn long(&'s self) -> int {
+        let &(x,_) = self;
+        x
+    }
+    fn short<'b>(&'b self) -> int {
+        let &(_,y) = self;
+        y
+    }
+}
+
+impl<'t> MakerTrait<'t> for ~Trait<'t> {
+    fn mk() -> ~Trait<'t> { ~(4,5) as ~Trait }
+}
+
+enum List<'l> {
+    Cons(int, &'l List<'l>),
+    Null
+}
+
+impl<'l> List<'l> {
+    fn car<'m>(&'m self) -> int {
+        match self {
+            &Cons(car, _) => car,
+            &Null => fail!(),
+        }
+    }
+    fn cdr<'n>(&'n self) -> &'l List<'l> {
+        match self {
+            &Cons(_, cdr) => cdr,
+            &Null => fail!(),
+        }
+    }
+}
+
+impl<'t> RefMakerTrait<'t> for List<'t> {
+    fn mk(l:List<'t>) -> &'t List<'t> {
+        l.cdr()
+    }
+}
+
+pub fn main() {
+    let t = (2,3);
+    let o = &t as &Trait;
+    let s1 = Struct1 { f: o };
+    let s2 = Struct2 { f: o };
+    assert_eq!(poly_invoke(&t), (2,3));
+    assert_eq!(object_invoke1(&t), (2,3));
+    assert_eq!(field_invoke1(&s1), (2,3));
+    assert_eq!(object_invoke2(&t), 3);
+    assert_eq!(field_invoke2(&s2), 3);
+
+    let m : ~Trait = make_val();
+    assert_eq!(object_invoke1(m), (4,5));
+    assert_eq!(object_invoke2(m), 5);
+
+    // The RefMakerTrait above is pretty strange (i.e. it is strange
+    // to consume a value of type T and return a &T).  Easiest thing
+    // that came to my mind: consume a cell of a linked list and
+    // return a reference to the list it points to.
+    let l0 = Null;
+    let l1 = Cons(1, &l0);
+    let l2 = Cons(2, &l1);
+    let rl1 = &l1;
+    let r  = make_ref(l2);
+    assert_eq!(rl1.car(), r.car());
+}