Skip to content

Commit 9549fbd

Browse files
author
Alexander Regueiro
committed
Added tests.
1 parent 29de923 commit 9549fbd

22 files changed

+792
-9
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16861686
let trait_ref = Binder::bind(TraitRef::identity(self.tcx, exp_found.found));
16871687
let supertraits = crate::traits::util::supertraits(self.tcx, trait_ref);
16881688
if supertraits.into_iter().any(|trait_ref| trait_ref.def_id() == exp_found.expected) {
1689-
diag.note("add `#![feature(trait_upcasting)]` to the crate attributes to enable");
1689+
diag.note(
1690+
"add `#![feature(trait_upcasting)]` to the crate attributes to enable \
1691+
trait upcasting");
16901692
}
16911693
}
16921694

src/test/ui/feature-gates/feature-gate-trait_upcasting.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ fn main() {
1010
let bar: &dyn Bar = &();
1111
let foo: &dyn Foo = bar;
1212
//~^ ERROR mismatched types [E0308]
13-
//~| NOTE expected type `&dyn Foo`
1413
//~| NOTE expected trait `Foo`, found trait `Bar`
15-
//~| NOTE add `#![feature(trait_upcasting)]` to the crate attributes to enable
14+
//~| NOTE expected due to this
15+
//~| NOTE expected reference `&dyn Foo`
16+
//~| NOTE add `#![feature(trait_upcasting)]` to the crate attributes to enable trait upcasting
1617
}

src/test/ui/feature-gates/feature-gate-trait_upcasting.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ error[E0308]: mismatched types
22
--> $DIR/feature-gate-trait_upcasting.rs:11:25
33
|
44
LL | let foo: &dyn Foo = bar;
5-
| ^^^ expected trait `Foo`, found trait `Bar`
5+
| -------- ^^^ expected trait `Foo`, found trait `Bar`
6+
| |
7+
| expected due to this
68
|
7-
= note: add `#![feature(trait_upcasting)]` to the crate attributes to enable
8-
= note: expected type `&dyn Foo`
9-
found type `&dyn Bar`
9+
= note: expected reference `&dyn Foo`
10+
found reference `&dyn Bar`
11+
= note: add `#![feature(trait_upcasting)]` to the crate attributes to enable trait upcasting
1012

1113
error: aborting due to previous error
1214

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// run-pass
2+
3+
#![feature(trait_upcasting)]
4+
5+
trait Foo: PartialEq<i32> + std::fmt::Debug + Send + Sync {
6+
fn a(&self) -> i32 { 10 }
7+
8+
fn z(&self) -> i32 { 11 }
9+
10+
fn y(&self) -> i32 { 12 }
11+
}
12+
13+
trait Bar: Foo {
14+
fn b(&self) -> i32 { 20 }
15+
16+
fn w(&self) -> i32 { 21 }
17+
}
18+
19+
trait Baz: Bar {
20+
fn c(&self) -> i32 { 30 }
21+
}
22+
23+
impl Foo for i32 {
24+
fn a(&self) -> i32 { 100 }
25+
}
26+
27+
impl Bar for i32 {
28+
fn b(&self) -> i32 { 200 }
29+
}
30+
31+
impl Baz for i32 {
32+
fn c(&self) -> i32 { 300 }
33+
}
34+
35+
fn main() {
36+
let baz: &dyn Baz = &1;
37+
let _: &dyn std::fmt::Debug = baz;
38+
let _: &(dyn Send + Sync) = baz;
39+
let _: &dyn Send = baz;
40+
let _: &dyn Sync = baz;
41+
assert_eq!(*baz, 1);
42+
assert_eq!(baz.a(), 100);
43+
assert_eq!(baz.b(), 200);
44+
assert_eq!(baz.c(), 300);
45+
assert_eq!(baz.z(), 11);
46+
assert_eq!(baz.y(), 12);
47+
assert_eq!(baz.w(), 21);
48+
49+
let bar: &dyn Bar = baz;
50+
let _: &dyn std::fmt::Debug = bar;
51+
let _: &(dyn Send + Sync) = bar;
52+
let _: &dyn Send = bar;
53+
let _: &dyn Sync = bar;
54+
assert_eq!(*bar, 1);
55+
assert_eq!(bar.a(), 100);
56+
assert_eq!(bar.b(), 200);
57+
assert_eq!(bar.z(), 11);
58+
assert_eq!(bar.y(), 12);
59+
assert_eq!(bar.w(), 21);
60+
61+
let foo: &dyn Foo = baz;
62+
let _: &dyn std::fmt::Debug = foo;
63+
let _: &(dyn Send + Sync) = foo;
64+
let _: &dyn Send = foo;
65+
let _: &dyn Sync = foo;
66+
assert_eq!(*foo, 1);
67+
assert_eq!(foo.a(), 100);
68+
assert_eq!(foo.z(), 11);
69+
assert_eq!(foo.y(), 12);
70+
71+
let foo: &dyn Foo = bar;
72+
let _: &dyn std::fmt::Debug = foo;
73+
let _: &(dyn Send + Sync) = foo;
74+
let _: &dyn Send = foo;
75+
let _: &dyn Sync = foo;
76+
assert_eq!(*foo, 1);
77+
assert_eq!(foo.a(), 100);
78+
assert_eq!(foo.z(), 11);
79+
assert_eq!(foo.y(), 12);
80+
}

0 commit comments

Comments
 (0)