Skip to content

Commit ad6f4b7

Browse files
committed
Use explicit instead of implicit control-flow for check-cfg parsing
1 parent cad92b4 commit ad6f4b7

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

compiler/rustc_interface/src/interface.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use rustc_middle::ty;
1313
use rustc_parse::maybe_new_parser_from_source_str;
1414
use rustc_query_impl::QueryCtxt;
1515
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};
1718
use rustc_session::lint;
1819
use rustc_session::parse::{CrateConfig, ParseSess};
1920
use rustc_session::Session;
@@ -121,9 +122,9 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
121122
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
122123
pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
123124
rustc_span::create_default_session_if_not_set_then(move |_| {
124-
let mut cfg = CheckCfg::default();
125+
let mut check_cfg = CheckCfg::default();
125126

126-
'specs: for s in specs {
127+
for s in specs {
127128
let sess = ParseSess::with_silent_emitter(Some(format!(
128129
"this error occurred on the command line: `--check-cfg={s}`"
129130
)));
@@ -137,17 +138,24 @@ pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
137138
concat!("invalid `--check-cfg` argument: `{}` (", $reason, ")"),
138139
s
139140
),
140-
);
141+
)
141142
};
142143
}
143144

145+
let expected_error = || {
146+
error!(
147+
"expected `names(name1, name2, ... nameN)` or \
148+
`values(name, \"value1\", \"value2\", ... \"valueN\")`"
149+
)
150+
};
151+
144152
match maybe_new_parser_from_source_str(&sess, filename, s.to_string()) {
145153
Ok(mut parser) => match parser.parse_meta_item() {
146154
Ok(meta_item) if parser.token == token::Eof => {
147155
if let Some(args) = meta_item.meta_item_list() {
148156
if meta_item.has_name(sym::names) {
149157
let names_valid =
150-
cfg.names_valid.get_or_insert_with(|| FxHashSet::default());
158+
check_cfg.names_valid.get_or_insert_with(|| FxHashSet::default());
151159
for arg in args {
152160
if arg.is_word() && arg.ident().is_some() {
153161
let ident = arg.ident().expect("multi-segment cfg key");
@@ -156,15 +164,20 @@ pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
156164
error!("`names()` arguments must be simple identifiers");
157165
}
158166
}
159-
continue 'specs;
160167
} else if meta_item.has_name(sym::values) {
161168
if let Some((name, values)) = args.split_first() {
162169
if name.is_word() && name.ident().is_some() {
163170
let ident = name.ident().expect("multi-segment cfg key");
164-
let ident_values = cfg
171+
let ident_values = check_cfg
165172
.values_valid
166173
.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+
};
168181

169182
for val in values {
170183
if let Some(LitKind::Str(s, _)) =
@@ -177,36 +190,40 @@ pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
177190
);
178191
}
179192
}
180-
181-
continue 'specs;
182193
} else {
183194
error!(
184195
"`values()` first argument must be a simple identifier"
185196
);
186197
}
187198
} 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();
190202
}
203+
} else {
204+
expected_error();
191205
}
206+
} else {
207+
expected_error();
192208
}
193209
}
194-
Ok(..) => {}
195-
Err(err) => err.cancel(),
210+
Ok(..) => expected_error(),
211+
Err(err) => {
212+
err.cancel();
213+
expected_error();
214+
}
196215
},
197-
Err(errs) => drop(errs),
216+
Err(errs) => {
217+
drop(errs);
218+
expected_error();
219+
}
198220
}
199-
200-
error!(
201-
"expected `names(name1, name2, ... nameN)` or \
202-
`values(name, \"value1\", \"value2\", ... \"valueN\")`"
203-
);
204221
}
205222

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());
208225
}
209-
cfg
226+
check_cfg
210227
})
211228
}
212229

0 commit comments

Comments
 (0)