File tree Expand file tree Collapse file tree 2 files changed +30
-13
lines changed
compiler/rustc_error_codes/src/error_codes Expand file tree Collapse file tree 2 files changed +30
-13
lines changed Original file line number Diff line number Diff line change 11Trait objects must include the ` dyn ` keyword.
22
3- Trait objects are a way to call methods on types that are not known until
4- runtime but conform to some trait.
5-
6- In the following code the trait object should be formed with
7- ` Box<dyn Foo> ` , but ` dyn ` is left off.
3+ Erroneous code example:
84
9- ``` no_run
5+ ``` edition2021,compile_fail,E782
106trait Foo {}
117fn test(arg: Box<Foo>) {}
128```
139
10+ Trait objects are a way to call methods on types that are not known until
11+ runtime but conform to some trait.
12+
13+ Trait objects should be formed with ` Box<dyn Foo> ` , but in the code above
14+ ` dyn ` is left off.
15+
1416This makes it harder to see that ` arg ` is a trait object and not a
1517simply a heap allocated type called ` Foo ` .
1618
19+ To fix this issue, add ` dyn ` before the trait name.
20+
21+ ```
22+ trait Foo {}
23+ fn test(arg: Box<dyn Foo>) {}
24+ ```
25+
1726This used to be allowed before edition 2021, but is now an error.
Original file line number Diff line number Diff line change 11The range pattern ` ... ` is no longer allowed.
22
3+ Erroneous code example:
4+
5+ ``` edition2021,compile_fail,E782
6+ fn main() {
7+ match 2u8 {
8+ 0...9 => println!("Got a number less than 10"),
9+ _ => println!("Got a number 10 or more")
10+ }
11+ }
12+ ```
13+
314Older Rust code using previous editions allowed ` ... ` to stand for exclusive
415ranges which are now signified using ` ..= ` .
516
6- The following code use to compile, but now it now longer does .
17+ To make this code compile replace the ` ... ` with ` ..= ` .
718
8- ``` no_run
19+ ```
920fn main() {
10- let n = 2u8;
11- match n {
12- ...9 => println!("Got a number less than 10"),
21+ match 2u8 {
22+ 0..=9 => println!("Got a number less than 10"),
1323 _ => println!("Got a number 10 or more")
1424 }
1525}
1626```
17-
18- To make this code compile replace the ` ... ` with ` ..= ` .
You can’t perform that action at this time.
0 commit comments