Skip to content

Commit e512409

Browse files
committed
Add simpler and more explicit syntax to check-cfg
This adds 2 new form and deprecated the other ones: - exhaustive(names, values) - configure(name, \"value1\", \"value2\", ... \"valueN\")
1 parent 4b94c23 commit e512409

File tree

4 files changed

+189
-75
lines changed

4 files changed

+189
-75
lines changed

compiler/rustc_interface/src/interface.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
144144

145145
let expected_error = || {
146146
error!(
147-
"expected `names(name1, name2, ... nameN)` or \
148-
`values(name, \"value1\", \"value2\", ... \"valueN\")`"
147+
"expected `exhaustive(names, values)` or \
148+
`configure(name, \"value1\", \"value2\", ... \"valueN\")`"
149149
)
150150
};
151151

@@ -206,6 +206,64 @@ pub fn parse_check_cfg(specs: Vec<String>) -> CheckCfg {
206206
} else {
207207
expected_error();
208208
}
209+
} else if meta_item.has_name(sym::exhaustive) {
210+
for arg in args {
211+
if arg.is_word() && let Some(ident) = arg.ident() {
212+
if ident.name == sym::names {
213+
check_cfg.exhaustive_names = true;
214+
} else if ident.name == sym::values {
215+
check_cfg.exhaustive_values = true;
216+
} else {
217+
error!(
218+
"expected `exhaustive(names)` or `exhaustive(values)`"
219+
);
220+
}
221+
} else {
222+
error!(
223+
"`exhaustive()` arguments must be simple identifiers"
224+
);
225+
}
226+
}
227+
} else if meta_item.has_name(sym::configure) {
228+
let mut names = Vec::new();
229+
let mut values: FxHashSet<_> = Default::default();
230+
231+
for arg in args {
232+
if arg.is_word() && let Some(ident) = arg.ident() {
233+
if !values.is_empty() {
234+
error!("`configure()` names cannot be after values");
235+
}
236+
names.push(ident);
237+
} else if let Some(LitKind::Str(s, _)) =
238+
arg.lit().map(|lit| &lit.kind)
239+
{
240+
if names.is_empty() {
241+
error!(
242+
"`configure()` first arguments must be simple identifiers"
243+
);
244+
}
245+
values.insert(Some(s.to_string()));
246+
} else {
247+
error!(
248+
"`configure()` arguments must be simple identifiers or string literals"
249+
);
250+
}
251+
}
252+
253+
if values.is_empty() {
254+
values.insert(None);
255+
}
256+
257+
for name in names {
258+
check_cfg
259+
.expecteds
260+
.entry(name.to_string())
261+
.and_modify(|v| match v {
262+
ExpectedValues::Some(v) => v.extend(values.clone()),
263+
ExpectedValues::Any => {}
264+
})
265+
.or_insert_with(|| ExpectedValues::Some(values.clone()));
266+
}
209267
} else {
210268
expected_error();
211269
}

compiler/rustc_interface/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(internal_output_capture)]
44
#![feature(thread_spawn_unchecked)]
55
#![feature(lazy_cell)]
6+
#![feature(let_chains)]
67
#![feature(try_blocks)]
78
#![recursion_limit = "256"]
89
#![allow(rustc::potential_query_instability)]

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ symbols! {
502502
concat,
503503
concat_bytes,
504504
concat_idents,
505+
configure,
505506
conservative_impl_trait,
506507
console,
507508
const_allocate,
@@ -673,6 +674,7 @@ symbols! {
673674
except,
674675
exchange_malloc,
675676
exclusive_range_pattern,
677+
exhaustive,
676678
exhaustive_integer_patterns,
677679
exhaustive_patterns,
678680
existential_type,

0 commit comments

Comments
 (0)