Skip to content

Commit cf01b51

Browse files
committed
Generic type parameters are flexible even for existential types
1 parent 0d25ff8 commit cf01b51

9 files changed

+32
-87
lines changed

src/librustc_typeck/collect.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,16 @@ fn find_existential_constraints<'a, 'tcx>(
13461346
// FIXME(oli-obk): trace the actual span from inference to improve errors
13471347
let span = self.tcx.def_span(def_id);
13481348
if let Some((prev_span, prev_ty)) = self.found {
1349-
if ty != prev_ty {
1349+
let mut ty = ty.walk().fuse();
1350+
let mut prev_ty = prev_ty.walk().fuse();
1351+
let iter_eq = (&mut ty).zip(&mut prev_ty).all(|(t, p)| match (&t.sty, &p.sty) {
1352+
// type parameters are equal to any other type parameter for the purpose of
1353+
// concrete type equality, as it is possible to obtain the same type just
1354+
// by passing matching parameters to a function.
1355+
(ty::Param(_), ty::Param(_)) => true,
1356+
_ => t == p,
1357+
});
1358+
if !iter_eq || ty.next().is_some() || prev_ty.next().is_some() {
13501359
// found different concrete types for the existential type
13511360
let mut err = self.tcx.sess.struct_span_err(
13521361
span,
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// compile-pass
22
#![feature(existential_type)]
33

4+
use std::fmt::Debug;
5+
46
fn main() {}
57

68
// test that unused generic parameters are ok
7-
existential type Two<T, U>: 'static;
9+
existential type Two<T, U>: Debug;
810

9-
fn one<T: 'static>(t: T) -> Two<T, T> {
11+
fn one<T: Debug>(t: T) -> Two<T, T> {
1012
t
1113
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// compile-pass
22
#![feature(existential_type)]
33

4+
use std::fmt::Debug;
5+
46
fn main() {}
57

68
// test that unused generic parameters are ok
7-
existential type Two<T, U>: 'static;
9+
existential type Two<T, U>: Debug;
810

9-
fn one<T: 'static>(t: T) -> Two<T, T> {
11+
fn one<T: Debug>(t: T) -> Two<T, T> {
1012
t
1113
}
1214

13-
fn two<T: 'static, U: 'static>(t: T, _: U) -> Two<T, U> {
15+
fn two<T: Debug, U>(t: T, _: U) -> Two<T, U> {
1416
t
1517
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
// compile-pass
12
#![feature(existential_type)]
23

4+
use std::fmt::Debug;
5+
36
fn main() {}
47

58
// test that unused generic parameters are ok
6-
existential type Two<T, U>: 'static;
9+
existential type Two<T, U>: Debug;
710

8-
fn one<T: 'static>(t: T) -> Two<T, T> {
11+
fn one<T: Debug>(t: T) -> Two<T, T> {
912
t
1013
}
1114

12-
fn two<T: 'static, U: 'static>(t: T, _: U) -> Two<T, U> {
15+
fn two<T: Debug, U>(t: T, _: U) -> Two<T, U> {
1316
t
1417
}
1518

16-
fn three<T: 'static, U: 'static>(_: T, u: U) -> Two<T, U> {
17-
//~^ ERROR defining existential type use differs from previous
19+
fn three<T, U: Debug>(_: T, u: U) -> Two<T, U> {
1820
u
1921
}

src/test/ui/existential_types/generic_duplicate_param_use3.stderr

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
// compile-pass
12
#![feature(existential_type)]
23

4+
use std::fmt::Debug;
5+
36
fn main() {}
47

58
// test that unused generic parameters are ok
6-
existential type Two<T, U>: 'static;
9+
existential type Two<T, U>: Debug;
710

8-
fn one<T: 'static>(t: T) -> Two<T, T> {
11+
fn one<T: Debug>(t: T) -> Two<T, T> {
912
t
1013
}
1114

12-
fn three<T: 'static, U: 'static>(_: T, u: U) -> Two<T, U> {
13-
//~^ ERROR defining existential type use differs from previous
15+
fn three<T, U: Debug>(_: T, u: U) -> Two<T, U> {
1416
u
1517
}

src/test/ui/existential_types/generic_duplicate_param_use4.stderr

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/test/ui/existential_types/generic_duplicate_param_use5.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/test/ui/existential_types/generic_duplicate_param_use5.stderr

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)