Skip to content

Commit d5e948b

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

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
@@ -816,6 +816,32 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
816816
method.fty.sig.inputs[0].repr(self.tcx()),
817817
substs.repr(self.tcx()));
818818

819+
// It is possible for type parameters or early-bound lifetimes
820+
// to appear in the signature of `self`. The substitutions we
821+
// are given do not include type/lifetime parameters for the
822+
// method yet. So create fresh variables here for those too,
823+
// if there are any.
824+
assert_eq!(substs.types.len(subst::FnSpace), 0);
825+
assert_eq!(substs.regions().len(subst::FnSpace), 0);
826+
let mut substs = substs;
827+
let placeholder;
828+
if
829+
!method.generics.types.is_empty_in(subst::FnSpace) ||
830+
!method.generics.regions.is_empty_in(subst::FnSpace)
831+
{
832+
let method_types =
833+
self.infcx().next_ty_vars(
834+
method.generics.types.len(subst::FnSpace));
835+
836+
let method_regions =
837+
self.infcx().region_vars_for_defs(
838+
self.span,
839+
method.generics.regions.get_slice(subst::FnSpace));
840+
841+
placeholder = (*substs).clone().with_method(method_types, method_regions);
842+
substs = &placeholder;
843+
}
844+
819845
let xform_self_ty = method.fty.sig.inputs[0].subst(self.tcx(), substs);
820846
self.infcx().replace_late_bound_regions_with_fresh_var(method.fty.sig.binder_id,
821847
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)