Skip to content

Commit 102997a

Browse files
Update Tests
1 parent 25acbbd commit 102997a

3 files changed

+824
-1
lines changed

tests/ui/dyn-keyword/dyn-2021-edition-error.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ error[E0782]: trait objects must include the `dyn` keyword
44
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
55
| ^^^^^^^^^
66
|
7-
help: add `dyn` keyword before this trait
7+
help: use a new generic type parameter, constrained by `SomeTrait`
8+
|
9+
LL | fn function<T: SomeTrait>(x: &T, y: Box<SomeTrait>) {
10+
| ++++++++++++++ ~
11+
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
12+
|
13+
LL | fn function(x: &impl SomeTrait, y: Box<SomeTrait>) {
14+
| ++++
15+
help: alternatively, use a trait object to accept any type that implements `SomeTrait`, accessing its methods at runtime using dynamic dispatch
816
|
917
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
1018
| +++
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//@ edition:2021
2+
3+
trait Trait {}
4+
5+
struct IceCream;
6+
7+
impl IceCream {
8+
fn foo(_: &Trait) {}
9+
//~^ ERROR: trait objects must include the `dyn` keyword
10+
11+
fn bar(self, _: &'a Trait) {}
12+
//~^ ERROR: trait objects must include the `dyn` keyword
13+
//~| ERROR: use of undeclared lifetime name
14+
15+
fn alice<'a>(&self, _: &Trait) {}
16+
//~^ ERROR: trait objects must include the `dyn` keyword
17+
18+
fn bob<'a>(_: &'a Trait) {}
19+
//~^ ERROR: trait objects must include the `dyn` keyword
20+
21+
fn cat() -> &Trait {
22+
//~^ ERROR: missing lifetime specifier
23+
//~| ERROR: trait objects must include the `dyn` keyword
24+
&Type
25+
}
26+
27+
fn dog<'a>() -> &Trait {
28+
//~^ ERROR: missing lifetime specifier
29+
//~| ERROR: trait objects must include the `dyn` keyword
30+
&Type
31+
}
32+
33+
fn kitten() -> &'a Trait {
34+
//~^ ERROR: use of undeclared lifetime name
35+
//~| ERROR: trait objects must include the `dyn` keyword
36+
&Type
37+
}
38+
39+
fn puppy<'a>() -> &'a Trait {
40+
//~^ ERROR: trait objects must include the `dyn` keyword
41+
&Type
42+
}
43+
44+
fn parrot() -> &mut Trait {
45+
//~^ ERROR: missing lifetime specifier
46+
//~| ERROR: cannot return a mutable reference to a bare trait
47+
&mut Type
48+
//~^ ERROR: cannot return reference to temporary value
49+
}
50+
}
51+
52+
trait Sing {
53+
fn foo(_: &Trait);
54+
//~^ ERROR: trait objects must include the `dyn` keyword
55+
56+
fn bar(_: &'a Trait);
57+
//~^ ERROR: trait objects must include the `dyn` keyword
58+
//~| ERROR: use of undeclared lifetime name
59+
60+
fn alice<'a>(_: &Trait);
61+
//~^ ERROR: trait objects must include the `dyn` keyword
62+
63+
fn bob<'a>(_: &'a Trait);
64+
//~^ ERROR: trait objects must include the `dyn` keyword
65+
66+
fn cat() -> &Trait;
67+
//~^ ERROR: missing lifetime specifier
68+
//~| ERROR: trait objects must include the `dyn` keyword
69+
70+
fn dog<'a>() -> &Trait {
71+
//~^ ERROR: missing lifetime specifier
72+
//~| ERROR: trait objects must include the `dyn` keyword
73+
&Type
74+
}
75+
76+
fn kitten() -> &'a Trait {
77+
//~^ ERROR: use of undeclared lifetime name
78+
//~| ERROR: trait objects must include the `dyn` keyword
79+
&Type
80+
}
81+
82+
fn puppy<'a>() -> &'a Trait {
83+
//~^ ERROR: trait objects must include the `dyn` keyword
84+
&Type
85+
}
86+
87+
fn parrot() -> &mut Trait {
88+
//~^ ERROR: missing lifetime specifier
89+
//~| ERROR: cannot return a mutable reference to a bare trait
90+
&mut Type
91+
//~^ ERROR: cannot return reference to temporary value
92+
}
93+
}
94+
95+
fn foo(_: &Trait) {}
96+
//~^ ERROR: trait objects must include the `dyn` keyword
97+
98+
fn bar(_: &'a Trait) {}
99+
//~^ ERROR: trait objects must include the `dyn` keyword
100+
//~| ERROR: use of undeclared lifetime name
101+
102+
fn alice<'a>(_: &Trait) {}
103+
//~^ ERROR: trait objects must include the `dyn` keyword
104+
105+
fn bob<'a>(_: &'a Trait) {}
106+
//~^ ERROR: trait objects must include the `dyn` keyword
107+
108+
struct Type;
109+
110+
impl Trait for Type {}
111+
112+
fn cat() -> &Trait {
113+
//~^ ERROR: missing lifetime specifier
114+
//~| ERROR: trait objects must include the `dyn` keyword
115+
&Type
116+
}
117+
118+
fn dog<'a>() -> &Trait {
119+
//~^ ERROR: missing lifetime specifier
120+
//~| ERROR: trait objects must include the `dyn` keyword
121+
&Type
122+
}
123+
124+
fn kitten() -> &'a Trait {
125+
//~^ ERROR: use of undeclared lifetime name
126+
//~| ERROR: trait objects must include the `dyn` keyword
127+
&Type
128+
}
129+
130+
fn puppy<'a>() -> &'a Trait {
131+
//~^ ERROR: trait objects must include the `dyn` keyword
132+
&Type
133+
}
134+
135+
fn parrot() -> &mut Trait {
136+
//~^ ERROR: missing lifetime specifier
137+
//~| ERROR: cannot return a mutable reference to a bare trait
138+
&mut Type
139+
//~^ ERROR: cannot return reference to temporary value
140+
}
141+
142+
fn main() {}

0 commit comments

Comments
 (0)