Skip to content

Commit 9ba6a6e

Browse files
committed
Substitute type/lifetimeInstatiate method type/early-bound lifetime parameters too when creating xform-self-type.
Fixes #18208.
1 parent 7151297 commit 9ba6a6e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

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

+26
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,32 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
827827
method.fty.sig.inputs[0].repr(self.tcx()),
828828
substs.repr(self.tcx()));
829829

830+
// It is possible for type parameters or early-bound lifetimes
831+
// to appear in the signature of `self`. The substitutions we
832+
// are given do not include type/lifetime parameters for the
833+
// method yet. So create fresh variables here for those too,
834+
// if there are any.
835+
assert_eq!(substs.types.len(subst::FnSpace), 0);
836+
assert_eq!(substs.regions().len(subst::FnSpace), 0);
837+
let mut substs = substs;
838+
let placeholder;
839+
if
840+
!method.generics.types.is_empty_in(subst::FnSpace) ||
841+
!method.generics.regions.is_empty_in(subst::FnSpace)
842+
{
843+
let method_types =
844+
self.infcx().next_ty_vars(
845+
method.generics.types.len(subst::FnSpace));
846+
847+
let method_regions =
848+
self.infcx().region_vars_for_defs(
849+
self.span,
850+
method.generics.regions.get_slice(subst::FnSpace));
851+
852+
placeholder = (*substs).clone().with_method(method_types, method_regions);
853+
substs = &placeholder;
854+
}
855+
830856
let xform_self_ty = method.fty.sig.inputs[0].subst(self.tcx(), substs);
831857
self.infcx().replace_late_bound_regions_with_fresh_var(method.fty.sig.binder_id,
832858
self.span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2012 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+
// Check that we successfully handle methods where the `self` type has
12+
// an early-bound lifetime. Issue #18208.
13+
14+
#![allow(dead_code)]
15+
16+
struct Cursor<'a>;
17+
18+
trait CursorNavigator {
19+
fn init_cursor<'a, 'b:'a>(&'a self, cursor: &mut Cursor<'b>) -> bool;
20+
}
21+
22+
struct SimpleNavigator;
23+
24+
impl CursorNavigator for SimpleNavigator {
25+
fn init_cursor<'a, 'b: 'a>(&'a self, _cursor: &mut Cursor<'b>) -> bool {
26+
false
27+
}
28+
}
29+
30+
fn main() {
31+
let mut c = Cursor;
32+
let n = SimpleNavigator;
33+
n.init_cursor(&mut c);
34+
}

0 commit comments

Comments
 (0)