Skip to content

Commit f1ace34

Browse files
committed
Make subtyping for projection types stricter. Fixes #21726.
1 parent bedd810 commit f1ace34

File tree

5 files changed

+137
-2
lines changed

5 files changed

+137
-2
lines changed

src/librustc/middle/infer/combine.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,13 @@ pub trait Combine<'tcx> : Sized {
263263
Err(ty::terr_projection_name_mismatched(
264264
expected_found(self, a.item_name, b.item_name)))
265265
} else {
266-
let trait_ref = try!(self.trait_refs(&*a.trait_ref, &*b.trait_ref));
266+
// Note that the trait refs for the projection must be
267+
// *equal*. This is because there is no inherent
268+
// relationship between `<T as Foo>::Bar` and `<U as
269+
// Foo>::Bar` that we can derive based on how `T` relates
270+
// to `U`. Issue #21726 contains further discussion and
271+
// in-depth examples.
272+
let trait_ref = try!(self.equate().trait_refs(&*a.trait_ref, &*b.trait_ref));
267273
Ok(ty::ProjectionTy { trait_ref: Rc::new(trait_ref), item_name: a.item_name })
268274
}
269275
}

src/librustc_typeck/variance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
793793
trait_def.generics.types.as_slice(),
794794
trait_def.generics.regions.as_slice(),
795795
trait_ref.substs,
796-
variance);
796+
self.invariant);
797797
}
798798

799799
ty::ty_trait(ref data) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2015 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+
#![allow(unused_variables)]
12+
13+
trait Trait<'a> {
14+
type Type;
15+
16+
fn method(&'a self) { }
17+
}
18+
19+
fn method1<'a,'b,T>(x: &'a T, y: &'b T)
20+
where T : for<'z> Trait<'z>, 'a : 'b
21+
{
22+
// Note that &'static T <: &'a T.
23+
let a: <T as Trait<'a>>::Type = loop { };
24+
let b: <T as Trait<'b>>::Type = loop { };
25+
let _: <T as Trait<'a>>::Type = a;
26+
}
27+
28+
fn method2<'a,'b,T>(x: &'a T, y: &'b T)
29+
where T : for<'z> Trait<'z>, 'a : 'b
30+
{
31+
// Note that &'static T <: &'a T.
32+
let a: <T as Trait<'a>>::Type = loop { };
33+
let b: <T as Trait<'b>>::Type = loop { };
34+
let _: <T as Trait<'b>>::Type = a; //~ ERROR mismatched types
35+
}
36+
37+
fn method3<'a,'b,T>(x: &'a T, y: &'b T)
38+
where T : for<'z> Trait<'z>, 'a : 'b
39+
{
40+
// Note that &'static T <: &'a T.
41+
let a: <T as Trait<'a>>::Type = loop { };
42+
let b: <T as Trait<'b>>::Type = loop { };
43+
let _: <T as Trait<'a>>::Type = b; //~ ERROR mismatched types
44+
}
45+
46+
fn method4<'a,'b,T>(x: &'a T, y: &'b T)
47+
where T : for<'z> Trait<'z>, 'a : 'b
48+
{
49+
// Note that &'static T <: &'a T.
50+
let a: <T as Trait<'a>>::Type = loop { };
51+
let b: <T as Trait<'b>>::Type = loop { };
52+
let _: <T as Trait<'b>>::Type = b;
53+
}
54+
55+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 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+
// Test that the variance computation considers types/regions that
12+
// appear in projections to be invariant.
13+
14+
trait Trait<'a> {
15+
type Type;
16+
17+
fn method(&'a self) { }
18+
}
19+
20+
#[rustc_variance]
21+
struct Foo<'a, T : Trait<'a>> { //~ ERROR ItemVariances(types=[[+];[];[]], regions=[[-];[];[]])
22+
field: (T, &'a ())
23+
}
24+
25+
#[rustc_variance]
26+
struct Bar<'a, T : Trait<'a>> { //~ ERROR ItemVariances(types=[[o];[];[]], regions=[[o];[];[]])
27+
field: <T as Trait<'a>>::Type
28+
}
29+
30+
fn main() { }

src/test/run-pass/issue-21726.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2015 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+
// Regression test for #21726: an issue arose around the rules for
12+
// subtyping of projection types that resulted in an unconstrained
13+
// region, yielding region inference failures.
14+
15+
fn main() { }
16+
17+
fn foo<'a>(s: &'a str) {
18+
let b: B<()> = B::new(s, ());
19+
b.get_short();
20+
}
21+
22+
trait IntoRef<'a> {
23+
type T: Clone;
24+
fn into_ref(self, &'a str) -> Self::T;
25+
}
26+
27+
impl<'a> IntoRef<'a> for () {
28+
type T = &'a str;
29+
fn into_ref(self, s: &'a str) -> &'a str {
30+
s
31+
}
32+
}
33+
34+
struct B<'a, P: IntoRef<'a>>(P::T);
35+
36+
impl<'a, P: IntoRef<'a>> B<'a, P> {
37+
fn new(s: &'a str, i: P) -> B<'a, P> {
38+
B(i.into_ref(s))
39+
}
40+
41+
fn get_short(&self) -> P::T {
42+
self.0.clone()
43+
}
44+
}

0 commit comments

Comments
 (0)