Skip to content

Commit 73fb19c

Browse files
committed
Auto merge of #26275 - arielb1:unconstrained-projection, r=nikomatsakis
Fixes #26262 Because this rejects code that previously compiled, this is a [breaking-change] r? @nikomatsakis
2 parents a53a098 + 3ca4d92 commit 73fb19c

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/librustc_typeck/constrained_type_params.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,21 @@ pub enum Parameter {
1919
Region(ty::EarlyBoundRegion),
2020
}
2121

22+
/// Returns the list of parameters that are constrained by the type `ty`
23+
/// - i.e. the value of each parameter in the list is uniquely determined
24+
/// by `ty` (see RFC 447).
2225
pub fn parameters_for_type<'tcx>(ty: Ty<'tcx>) -> Vec<Parameter> {
23-
ty.walk()
24-
.flat_map(|ty| parameters_for_type_shallow(ty))
25-
.collect()
26+
let mut result = vec![];
27+
ty::maybe_walk_ty(ty, |t| {
28+
if let ty::TyProjection(..) = t.sty {
29+
false // projections are not injective.
30+
} else {
31+
result.append(&mut parameters_for_type_shallow(t));
32+
// non-projection type constructors are injective.
33+
true
34+
}
35+
});
36+
result
2637
}
2738

2839
pub fn parameters_for_trait_ref<'tcx>(trait_ref: &ty::TraitRef<'tcx>) -> Vec<Parameter> {

src/test/compile-fail/issue-26262.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
// Check that projections don't count as constraining type parameters.
12+
13+
struct S<T>(T);
14+
15+
trait Tr { type Assoc; fn test(); }
16+
17+
impl<T: Tr> S<T::Assoc> {
18+
//~^ ERROR the type parameter `T` is not constrained
19+
fn foo(self, _: T) {
20+
T::test();
21+
}
22+
}
23+
24+
trait Trait1<T> { type Bar; }
25+
trait Trait2<'x> { type Foo; }
26+
27+
impl<'a,T: Trait2<'a>> Trait1<<T as Trait2<'a>>::Foo> for T {
28+
//~^ ERROR the lifetime parameter `'a` is not constrained
29+
type Bar = &'a ();
30+
}
31+
32+
fn main() {}

0 commit comments

Comments
 (0)