@@ -71,50 +71,67 @@ fn extract_error_codes(root_path: &Path, errors: &mut Vec<String>) -> Vec<String
71
71
let path = root_path. join ( Path :: new ( ERROR_CODES_PATH ) ) ;
72
72
let file =
73
73
fs:: read_to_string ( & path) . unwrap_or_else ( |e| panic ! ( "failed to read `{path:?}`: {e}" ) ) ;
74
+ let path = path. display ( ) ;
74
75
75
76
let mut error_codes = Vec :: new ( ) ;
76
77
77
- for line in file. lines ( ) {
78
+ for ( line_index, line) in file. lines ( ) . enumerate ( ) {
79
+ let line_index = line_index + 1 ;
78
80
let line = line. trim ( ) ;
79
81
80
82
if line. starts_with ( 'E' ) {
81
83
let split_line = line. split_once ( ':' ) ;
82
84
83
85
// Extract the error code from the line. Emit a fatal error if it is not in the correct
84
86
// format.
85
- let err_code = if let Some ( err_code) = split_line {
86
- err_code. 0 . to_owned ( )
87
- } else {
87
+ let Some ( split_line) = split_line else {
88
88
errors. push ( format ! (
89
- "Expected a line with the format `Eabcd: abcd, \
89
+ "{path}:{line_index}: Expected a line with the format `Eabcd: abcd, \
90
90
but got \" {}\" without a `:` delimiter",
91
91
line,
92
92
) ) ;
93
93
continue ;
94
94
} ;
95
95
96
+ let err_code = split_line. 0 . to_owned ( ) ;
97
+
96
98
// If this is a duplicate of another error code, emit a fatal error.
97
99
if error_codes. contains ( & err_code) {
98
- errors. push ( format ! ( "Found duplicate error code: `{}`" , err_code) ) ;
100
+ errors. push ( format ! (
101
+ "{path}:{line_index}: Found duplicate error code: `{}`" ,
102
+ err_code
103
+ ) ) ;
99
104
continue ;
100
105
}
101
106
102
107
let mut chars = err_code. chars ( ) ;
103
- chars. next ( ) ;
108
+ assert_eq ! ( chars. next( ) , Some ( 'E' ) ) ;
104
109
let error_num_as_str = chars. as_str ( ) ;
105
110
106
111
// Ensure that the line references the correct markdown file.
107
- let expected_filename = format ! ( " {}," , error_num_as_str) ;
108
- if expected_filename != split_line. unwrap ( ) . 1 {
112
+ let rest = split_line. 1 . split_once ( ',' ) ;
113
+ let Some ( rest) = rest else {
114
+ errors. push ( format ! (
115
+ "{path}:{line_index}: Expected a line with the format `Eabcd: abcd, \
116
+ but got \" {}\" without a `,` delimiter",
117
+ line,
118
+ ) ) ;
119
+ continue ;
120
+ } ;
121
+ if error_num_as_str != rest. 0 . trim ( ) {
109
122
errors. push ( format ! (
110
- "`{}:` should be followed by `{}` but instead found `{}` in \
123
+ "{path}:{line_index}: `{}:` should be followed by `{}, ` but instead found `{}` in \
111
124
`compiler/rustc_error_codes/src/lib.rs`",
112
125
err_code,
113
- expected_filename ,
114
- split_line. unwrap ( ) . 1 ,
126
+ error_num_as_str ,
127
+ split_line. 1 ,
115
128
) ) ;
116
129
continue ;
117
130
}
131
+ if !rest. 1 . trim ( ) . is_empty ( ) && !rest. 1 . trim ( ) . starts_with ( "//" ) {
132
+ errors. push ( format ! ( "{path}:{line_index}: should only have one error per line" ) ) ;
133
+ continue ;
134
+ }
118
135
119
136
error_codes. push ( err_code) ;
120
137
}
@@ -146,7 +163,7 @@ fn check_error_codes_docs(
146
163
return ;
147
164
}
148
165
149
- // Make sure that the file is referenced in `error_codes .rs`
166
+ // Make sure that the file is referenced in `rustc_error_codes/src/lib .rs`
150
167
let filename = path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . split_once ( '.' ) ;
151
168
let err_code = filename. unwrap ( ) . 0 ; // `unwrap` is ok because we know the filename is in the correct format.
152
169
0 commit comments