Skip to content

Commit 40458f7

Browse files
Fix ice for feature-gated cfg attributes applied to the crate
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent e27f16a commit 40458f7

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn parse_cfg_entry_version<S: Stage>(
9090
list: &MetaItemListParser<'_>,
9191
meta_span: Span,
9292
) -> Option<CfgEntry> {
93-
try_gate_cfg(sym::version, meta_span, cx.sess(), Some(cx.features()));
93+
try_gate_cfg(sym::version, meta_span, cx.sess(), cx.features_option());
9494
let Some(version) = list.single() else {
9595
cx.emit_err(session_diagnostics::ExpectedSingleVersionLiteral { span: list.span });
9696
return None;
@@ -119,7 +119,9 @@ fn parse_cfg_entry_target<S: Stage>(
119119
list: &MetaItemListParser<'_>,
120120
meta_span: Span,
121121
) -> Option<CfgEntry> {
122-
if !cx.features().cfg_target_compact() {
122+
if let Some(features) = cx.features_option()
123+
&& !features.cfg_target_compact()
124+
{
123125
feature_err(
124126
cx.sess(),
125127
sym::cfg_target_compact,
@@ -186,12 +188,13 @@ pub fn eval_config_entry(
186188
cfg_entry: &CfgEntry,
187189
id: NodeId,
188190
features: Option<&Features>,
191+
emit_lints: bool,
189192
) -> EvalConfigResult {
190193
match cfg_entry {
191194
CfgEntry::All(subs, ..) => {
192195
let mut all = None;
193196
for sub in subs {
194-
let res = eval_config_entry(sess, sub, id, features);
197+
let res = eval_config_entry(sess, sub, id, features, emit_lints);
195198
// We cannot short-circuit because `eval_config_entry` emits some lints
196199
if !res.as_bool() {
197200
all.get_or_insert(res);
@@ -202,7 +205,7 @@ pub fn eval_config_entry(
202205
CfgEntry::Any(subs, span) => {
203206
let mut any = None;
204207
for sub in subs {
205-
let res = eval_config_entry(sess, sub, id, features);
208+
let res = eval_config_entry(sess, sub, id, features, emit_lints);
206209
// We cannot short-circuit because `eval_config_entry` emits some lints
207210
if res.as_bool() {
208211
any.get_or_insert(res);
@@ -214,7 +217,7 @@ pub fn eval_config_entry(
214217
})
215218
}
216219
CfgEntry::Not(sub, span) => {
217-
if eval_config_entry(sess, sub, id, features).as_bool() {
220+
if eval_config_entry(sess, sub, id, features, emit_lints).as_bool() {
218221
EvalConfigResult::False { reason: cfg_entry.clone(), reason_span: *span }
219222
} else {
220223
EvalConfigResult::True
@@ -228,24 +231,28 @@ pub fn eval_config_entry(
228231
}
229232
}
230233
CfgEntry::NameValue { name, name_span, value, span } => {
231-
match sess.psess.check_config.expecteds.get(name) {
232-
Some(ExpectedValues::Some(values)) if !values.contains(&value.map(|(v, _)| v)) => {
233-
id.emit_span_lint(
234-
sess,
235-
UNEXPECTED_CFGS,
236-
*span,
237-
BuiltinLintDiag::UnexpectedCfgValue((*name, *name_span), *value),
238-
);
234+
if emit_lints {
235+
match sess.psess.check_config.expecteds.get(name) {
236+
Some(ExpectedValues::Some(values))
237+
if !values.contains(&value.map(|(v, _)| v)) =>
238+
{
239+
id.emit_span_lint(
240+
sess,
241+
UNEXPECTED_CFGS,
242+
*span,
243+
BuiltinLintDiag::UnexpectedCfgValue((*name, *name_span), *value),
244+
);
245+
}
246+
None if sess.psess.check_config.exhaustive_names => {
247+
id.emit_span_lint(
248+
sess,
249+
UNEXPECTED_CFGS,
250+
*span,
251+
BuiltinLintDiag::UnexpectedCfgName((*name, *name_span), *value),
252+
);
253+
}
254+
_ => { /* not unexpected */ }
239255
}
240-
None if sess.psess.check_config.exhaustive_names => {
241-
id.emit_span_lint(
242-
sess,
243-
UNEXPECTED_CFGS,
244-
*span,
245-
BuiltinLintDiag::UnexpectedCfgName((*name, *name_span), *value),
246-
);
247-
}
248-
_ => { /* not unexpected */ }
249256
}
250257

251258
if sess.psess.config.contains(&(*name, value.map(|(v, _)| v))) {

compiler/rustc_expand/src/config.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ pub fn pre_configure_attrs(sess: &Session, attrs: &[Attribute]) -> ast::AttrVec
167167
.flat_map(|attr| strip_unconfigured.process_cfg_attr(attr))
168168
.take_while(|attr| {
169169
!is_cfg(attr)
170-
|| strip_unconfigured.cfg_true(attr, strip_unconfigured.lint_node_id).as_bool()
170+
|| strip_unconfigured
171+
.cfg_true(attr, strip_unconfigured.lint_node_id, false)
172+
.as_bool()
171173
})
172174
.collect()
173175
}
@@ -401,10 +403,17 @@ impl<'a> StripUnconfigured<'a> {
401403

402404
/// Determines if a node with the given attributes should be included in this configuration.
403405
fn in_cfg(&self, attrs: &[Attribute]) -> bool {
404-
attrs.iter().all(|attr| !is_cfg(attr) || self.cfg_true(attr, self.lint_node_id).as_bool())
406+
attrs
407+
.iter()
408+
.all(|attr| !is_cfg(attr) || self.cfg_true(attr, self.lint_node_id, true).as_bool())
405409
}
406410

407-
pub(crate) fn cfg_true(&self, attr: &Attribute, node: NodeId) -> EvalConfigResult {
411+
pub(crate) fn cfg_true(
412+
&self,
413+
attr: &Attribute,
414+
node: NodeId,
415+
emit_errors: bool,
416+
) -> EvalConfigResult {
408417
// We need to run this to do basic validation of the attribute, such as that lits are valid, etc
409418
// FIXME(jdonszelmann) this should not be necessary in the future
410419
match validate_attr::parse_meta(&self.sess.psess, attr) {
@@ -428,15 +437,15 @@ impl<'a> StripUnconfigured<'a> {
428437
attr.span,
429438
node,
430439
self.features,
431-
true,
440+
emit_errors,
432441
parse_cfg_attr,
433442
&CFG_TEMPLATE,
434443
) else {
435444
// Cfg attribute was not parsable, give up
436445
return EvalConfigResult::True;
437446
};
438447

439-
eval_config_entry(self.sess, &cfg, self.lint_node_id, self.features)
448+
eval_config_entry(self.sess, &cfg, self.lint_node_id, self.features, emit_errors)
440449
}
441450

442451
/// If attributes are not allowed on expressions, emit an error for `attr`

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21712171
attr: ast::Attribute,
21722172
pos: usize,
21732173
) -> EvalConfigResult {
2174-
let res = self.cfg().cfg_true(&attr, node.node_id());
2174+
let res = self.cfg().cfg_true(&attr, node.node_id(), true);
21752175
if res.as_bool() {
21762176
// A trace attribute left in AST in place of the original `cfg` attribute.
21772177
// It can later be used by lints or other diagnostics.

0 commit comments

Comments
 (0)