add tests · rust-lang/rust@c8f0f17 (original) (raw)
File tree
6 files changed
lines changed
- tests/ui/traits/trait-upcasting
6 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
1 | +error[E0308]: mismatched types | |
2 | + --> $DIR/higher-ranked-upcasting-ok.rs:17:5 | |
3 | + | | |
4 | +LL | x | |
5 | + | ^ one type is more general than the other | |
6 | + | | |
7 | + = note: expected existential trait ref `for<'a, 'b> Supertrait<'a, 'b>` | |
8 | + found existential trait ref `for<'a> Supertrait<'a, 'a>` | |
9 | + | |
10 | +error[E0308]: mismatched types | |
11 | + --> $DIR/higher-ranked-upcasting-ok.rs:17:5 | |
12 | + | | |
13 | +LL | x | |
14 | + | ^ one type is more general than the other | |
15 | + | | |
16 | + = note: expected existential trait ref `for<'a, 'b> Supertrait<'a, 'b>` | |
17 | + found existential trait ref `for<'a> Supertrait<'a, 'a>` | |
18 | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | |
19 | + | |
20 | +error: aborting due to 2 previous errors | |
21 | + | |
22 | +For more information about this error, try `rustc --explain E0308`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
1 | +error[E0308]: mismatched types | |
2 | + --> $DIR/higher-ranked-upcasting-ok.rs:17:5 | |
3 | + | | |
4 | +LL | fn ok(x: &dyn for<'a, 'b> Subtrait<'a, 'b>) -> &dyn for<'a> Supertrait<'a, 'a> { | |
5 | + | ------------------------------- expected `&dyn for<'a> Supertrait<'a, 'a>` because of return type | |
6 | +LL | x | |
7 | + | ^ expected trait `Supertrait`, found trait `Subtrait` | |
8 | + | | |
9 | + = note: expected reference `&dyn for<'a> Supertrait<'a, 'a>` | |
10 | + found reference `&dyn for<'a, 'b> Subtrait<'a, 'b>` | |
11 | + | |
12 | +error: aborting due to 1 previous error | |
13 | + | |
14 | +For more information about this error, try `rustc --explain E0308`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
1 | +//@ revisions: current next | |
2 | +//@ ignore-compare-mode-next-solver (explicit revisions) | |
3 | +//@[next] compile-flags: -Znext-solver | |
4 | + | |
5 | +// We should be able to instantiate a binder during trait upcasting. | |
6 | +// This test could be `check-pass`, but we should make sure that we | |
7 | +// do so in both trait solvers. | |
8 | +#![feature(trait_upcasting)] | |
9 | +#![crate_type = "rlib"] | |
10 | +trait Supertrait<'a, 'b> {} | |
11 | + | |
12 | +trait Subtrait<'a, 'b>: Supertrait<'a, 'b> {} | |
13 | + | |
14 | +impl<'a> Supertrait<'a, 'a> for () {} | |
15 | +impl<'a> Subtrait<'a, 'a> for () {} | |
16 | +fn ok(x: &dyn for<'a, 'b> Subtrait<'a, 'b>) -> &dyn for<'a> Supertrait<'a, 'a> { | |
17 | + x //~ ERROR mismatched types | |
18 | +//[current]~^ ERROR mismatched types | |
19 | +} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
1 | +error[E0308]: mismatched types | |
2 | + --> $DIR/higher-ranked-upcasting-ub.rs:22:5 | |
3 | + | | |
4 | +LL | x | |
5 | + | ^ one type is more general than the other | |
6 | + | | |
7 | + = note: expected existential trait ref `for<'a> Supertrait<'a, 'a>` | |
8 | + found existential trait ref `for<'a, 'b> Supertrait<'a, 'b>` | |
9 | + | |
10 | +error[E0308]: mismatched types | |
11 | + --> $DIR/higher-ranked-upcasting-ub.rs:22:5 | |
12 | + | | |
13 | +LL | x | |
14 | + | ^ one type is more general than the other | |
15 | + | | |
16 | + = note: expected existential trait ref `for<'a> Supertrait<'a, 'a>` | |
17 | + found existential trait ref `for<'a, 'b> Supertrait<'a, 'b>` | |
18 | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | |
19 | + | |
20 | +error: aborting due to 2 previous errors | |
21 | + | |
22 | +For more information about this error, try `rustc --explain E0308`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
1 | +error[E0308]: mismatched types | |
2 | + --> $DIR/higher-ranked-upcasting-ub.rs:22:5 | |
3 | + | | |
4 | +LL | fn unsound(x: &dyn for<'a> Subtrait<'a, 'a>) -> &dyn for<'a, 'b> Supertrait<'a, 'b> { | |
5 | + | ----------------------------------- expected `&dyn for<'a, 'b> Supertrait<'a, 'b>` because of return type | |
6 | +LL | x | |
7 | + | ^ expected trait `Supertrait`, found trait `Subtrait` | |
8 | + | | |
9 | + = note: expected reference `&dyn for<'a, 'b> Supertrait<'a, 'b>` | |
10 | + found reference `&dyn for<'a> Subtrait<'a, 'a>` | |
11 | + | |
12 | +error: aborting due to 1 previous error | |
13 | + | |
14 | +For more information about this error, try `rustc --explain E0308`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
1 | +//@ revisions: current next | |
2 | +//@ ignore-compare-mode-next-solver (explicit revisions) | |
3 | +//@[next] compile-flags: -Znext-solver | |
4 | + | |
5 | +// We previously wrongly instantiated binders during trait upcasting, | |
6 | +// allowing the super trait to be more generic than the sub trait. | |
7 | +// This was unsound. | |
8 | +#![feature(trait_upcasting)] | |
9 | +trait Supertrait<'a, 'b> { | |
10 | +fn cast(&self, x: &'a str) -> &'b str; | |
11 | +} | |
12 | + | |
13 | +trait Subtrait<'a, 'b>: Supertrait<'a, 'b> {} | |
14 | + | |
15 | +impl<'a> Supertrait<'a, 'a> for () { | |
16 | +fn cast(&self, x: &'a str) -> &'a str { | |
17 | + x | |
18 | +} | |
19 | +} | |
20 | +impl<'a> Subtrait<'a, 'a> for () {} | |
21 | +fn unsound(x: &dyn for<'a> Subtrait<'a, 'a>) -> &dyn for<'a, 'b> Supertrait<'a, 'b> { | |
22 | + x //~ ERROR mismatched types | |
23 | +//[current]~^ ERROR mismatched types | |
24 | +} | |
25 | + | |
26 | +fn transmute<'a, 'b>(x: &'a str) -> &'b str { | |
27 | +unsound(&()).cast(x) | |
28 | +} | |
29 | + | |
30 | +fn main() { | |
31 | +let x; | |
32 | +{ | |
33 | +let mut temp = String::from("hello there"); | |
34 | + x = transmute(temp.as_str()); | |
35 | +} | |
36 | +println!("{x}"); | |
37 | +} |