@@ -13,7 +13,8 @@ use rustc_middle::ty;
13
13
use rustc_parse:: maybe_new_parser_from_source_str;
14
14
use rustc_query_impl:: QueryCtxt ;
15
15
use rustc_query_system:: query:: print_query_stack;
16
- use rustc_session:: config:: { self , CheckCfg , ErrorOutputType , Input , OutputFilenames } ;
16
+ use rustc_session:: config:: { self , ErrorOutputType , Input , OutputFilenames } ;
17
+ use rustc_session:: config:: { CheckCfg , ExpectedValues } ;
17
18
use rustc_session:: lint;
18
19
use rustc_session:: parse:: { CrateConfig , ParseSess } ;
19
20
use rustc_session:: Session ;
@@ -121,9 +122,9 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
121
122
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
122
123
pub fn parse_check_cfg ( specs : Vec < String > ) -> CheckCfg {
123
124
rustc_span:: create_default_session_if_not_set_then ( move |_| {
124
- let mut cfg = CheckCfg :: default ( ) ;
125
+ let mut check_cfg = CheckCfg :: default ( ) ;
125
126
126
- ' specs : for s in specs {
127
+ for s in specs {
127
128
let sess = ParseSess :: with_silent_emitter ( Some ( format ! (
128
129
"this error occurred on the command line: `--check-cfg={s}`"
129
130
) ) ) ;
@@ -137,17 +138,24 @@ pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
137
138
concat!( "invalid `--check-cfg` argument: `{}` (" , $reason, ")" ) ,
138
139
s
139
140
) ,
140
- ) ;
141
+ )
141
142
} ;
142
143
}
143
144
145
+ let expected_error = || {
146
+ error ! (
147
+ "expected `names(name1, name2, ... nameN)` or \
148
+ `values(name, \" value1\" , \" value2\" , ... \" valueN\" )`"
149
+ )
150
+ } ;
151
+
144
152
match maybe_new_parser_from_source_str ( & sess, filename, s. to_string ( ) ) {
145
153
Ok ( mut parser) => match parser. parse_meta_item ( ) {
146
154
Ok ( meta_item) if parser. token == token:: Eof => {
147
155
if let Some ( args) = meta_item. meta_item_list ( ) {
148
156
if meta_item. has_name ( sym:: names) {
149
157
let names_valid =
150
- cfg . names_valid . get_or_insert_with ( || FxHashSet :: default ( ) ) ;
158
+ check_cfg . names_valid . get_or_insert_with ( || FxHashSet :: default ( ) ) ;
151
159
for arg in args {
152
160
if arg. is_word ( ) && arg. ident ( ) . is_some ( ) {
153
161
let ident = arg. ident ( ) . expect ( "multi-segment cfg key" ) ;
@@ -156,15 +164,20 @@ pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
156
164
error ! ( "`names()` arguments must be simple identifiers" ) ;
157
165
}
158
166
}
159
- continue ' specs;
160
167
} else if meta_item. has_name ( sym:: values) {
161
168
if let Some ( ( name, values) ) = args. split_first ( ) {
162
169
if name. is_word ( ) && name. ident ( ) . is_some ( ) {
163
170
let ident = name. ident ( ) . expect ( "multi-segment cfg key" ) ;
164
- let ident_values = cfg
171
+ let ident_values = check_cfg
165
172
. values_valid
166
173
. entry ( ident. name . to_string ( ) )
167
- . or_insert_with ( || FxHashSet :: default ( ) ) ;
174
+ . or_insert_with ( || {
175
+ ExpectedValues :: Some ( FxHashSet :: default ( ) )
176
+ } ) ;
177
+
178
+ let ExpectedValues :: Some ( expected_values) = expected_values else {
179
+ bug ! ( "shoudn't be possible" )
180
+ } ;
168
181
169
182
for val in values {
170
183
if let Some ( LitKind :: Str ( s, _) ) =
@@ -177,36 +190,40 @@ pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
177
190
) ;
178
191
}
179
192
}
180
-
181
- continue ' specs;
182
193
} else {
183
194
error ! (
184
195
"`values()` first argument must be a simple identifier"
185
196
) ;
186
197
}
187
198
} else if args. is_empty ( ) {
188
- cfg. well_known_values = true ;
189
- continue ' specs;
199
+ check_cfg. well_known_values = true ;
200
+ } else {
201
+ expected_error ( ) ;
190
202
}
203
+ } else {
204
+ expected_error ( ) ;
191
205
}
206
+ } else {
207
+ expected_error ( ) ;
192
208
}
193
209
}
194
- Ok ( ..) => { }
195
- Err ( err) => err. cancel ( ) ,
210
+ Ok ( ..) => expected_error ( ) ,
211
+ Err ( err) => {
212
+ err. cancel ( ) ;
213
+ expected_error ( ) ;
214
+ }
196
215
} ,
197
- Err ( errs) => drop ( errs) ,
216
+ Err ( errs) => {
217
+ drop ( errs) ;
218
+ expected_error ( ) ;
219
+ }
198
220
}
199
-
200
- error ! (
201
- "expected `names(name1, name2, ... nameN)` or \
202
- `values(name, \" value1\" , \" value2\" , ... \" valueN\" )`"
203
- ) ;
204
221
}
205
222
206
- if let Some ( names_valid) = & mut cfg . names_valid {
207
- names_valid. extend ( cfg . values_valid . keys ( ) . cloned ( ) ) ;
223
+ if let Some ( names_valid) = & mut check_cfg . names_valid {
224
+ names_valid. extend ( check_cfg . values_valid . keys ( ) . cloned ( ) ) ;
208
225
}
209
- cfg
226
+ check_cfg
210
227
} )
211
228
}
212
229
0 commit comments