Skip to content

Commit

Permalink
Substitute type/lifetimeInstatiate method type/early-bound lifetime p…
Browse files Browse the repository at this point in the history
…arameters too when creating xform-self-type.

Fixes rust-lang#18208.
  • Loading branch information
nikomatsakis committed Nov 6, 2014
1 parent ee8904c commit d5e948b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/librustc/middle/typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,32 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
method.fty.sig.inputs[0].repr(self.tcx()),
substs.repr(self.tcx()));

// It is possible for type parameters or early-bound lifetimes
// to appear in the signature of `self`. The substitutions we
// are given do not include type/lifetime parameters for the
// method yet. So create fresh variables here for those too,
// if there are any.
assert_eq!(substs.types.len(subst::FnSpace), 0);
assert_eq!(substs.regions().len(subst::FnSpace), 0);
let mut substs = substs;
let placeholder;
if
!method.generics.types.is_empty_in(subst::FnSpace) ||
!method.generics.regions.is_empty_in(subst::FnSpace)
{
let method_types =
self.infcx().next_ty_vars(
method.generics.types.len(subst::FnSpace));

let method_regions =
self.infcx().region_vars_for_defs(
self.span,
method.generics.regions.get_slice(subst::FnSpace));

placeholder = (*substs).clone().with_method(method_types, method_regions);
substs = &placeholder;
}

let xform_self_ty = method.fty.sig.inputs[0].subst(self.tcx(), substs);
self.infcx().replace_late_bound_regions_with_fresh_var(method.fty.sig.binder_id,
self.span,
Expand Down
34 changes: 34 additions & 0 deletions src/test/run-pass/method-early-bound-lifetimes-on-self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2012 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.

// Check that we successfully handle methods where the `self` type has
// an early-bound lifetime. Issue #18208.

#![allow(dead_code)]

struct Cursor<'a>;

trait CursorNavigator {
fn init_cursor<'a, 'b:'a>(&'a self, cursor: &mut Cursor<'b>) -> bool;
}

struct SimpleNavigator;

impl CursorNavigator for SimpleNavigator {
fn init_cursor<'a, 'b: 'a>(&'a self, _cursor: &mut Cursor<'b>) -> bool {
false
}
}

fn main() {
let mut c = Cursor;
let n = SimpleNavigator;
n.init_cursor(&mut c);
}

0 comments on commit d5e948b

Please sign in to comment.