Skip to content

Commit a2b1347

Browse files
committed
Improve error
1 parent 2c1429c commit a2b1347

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rustc_ast_pretty::pprust;
4646
use rustc_data_structures::captures::Captures;
4747
use rustc_data_structures::fx::FxHashSet;
4848
use rustc_data_structures::sync::Lrc;
49-
use rustc_errors::struct_span_err;
49+
use rustc_errors::{struct_span_err, Applicability};
5050
use rustc_hir as hir;
5151
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
5252
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_ID};
@@ -2785,9 +2785,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
27852785
)
27862786
} else {
27872787
let msg = "trait objects must include the `dyn` keyword";
2788-
let label = "`dyn` keyword should be added before this trait";
2788+
let label = "add `dyn` keyword before this trait";
27892789
let mut err = struct_span_err!(self.sess, span, E0782, "{}", msg,);
2790-
err.span_label(span, label);
2790+
err.span_suggestion_verbose(
2791+
span.shrink_to_lo(),
2792+
label,
2793+
String::from("dyn "),
2794+
Applicability::MachineApplicable,
2795+
);
27912796
err.emit();
27922797
}
27932798
}

compiler/rustc_error_codes/src/error_codes/E0783.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ The range pattern `...` is no longer allowed.
22

33
Erroneous code example:
44

5-
```edition2021,compile_fail,E782
5+
```edition2021,compile_fail,E783
66
match 2u8 {
77
0...9 => println!("Got a number less than 10"), // error!
88
_ => println!("Got a number 10 or more"),

src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@ error[E0782]: trait objects must include the `dyn` keyword
22
--> $DIR/dyn-2021-edition-error.rs:6:14
33
|
44
LL | let _x: &SomeTrait = todo!();
5-
| ^^^^^^^^^ `dyn` keyword should be added before this trait
5+
| ^^^^^^^^^
6+
|
7+
help: add `dyn` keyword before this trait
8+
|
9+
LL | let _x: &dyn SomeTrait = todo!();
10+
| ^^^
611

712
error[E0782]: trait objects must include the `dyn` keyword
813
--> $DIR/dyn-2021-edition-error.rs:3:17
914
|
1015
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
11-
| ^^^^^^^^^ `dyn` keyword should be added before this trait
16+
| ^^^^^^^^^
17+
|
18+
help: add `dyn` keyword before this trait
19+
|
20+
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
21+
| ^^^
1222

1323
error[E0782]: trait objects must include the `dyn` keyword
1424
--> $DIR/dyn-2021-edition-error.rs:3:35
1525
|
1626
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
17-
| ^^^^^^^^^ `dyn` keyword should be added before this trait
27+
| ^^^^^^^^^
28+
|
29+
help: add `dyn` keyword before this trait
30+
|
31+
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
32+
| ^^^
1833

1934
error: aborting due to 3 previous errors
2035

0 commit comments

Comments
 (0)