Skip to content

Commit 082820f

Browse files
Move lints for check-cfg to attribute parsing
1 parent 1658047 commit 082820f

File tree

17 files changed

+95
-112
lines changed

17 files changed

+95
-112
lines changed

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ pub(crate) mod check_cfg;
22

33
use rustc_ast::token::Delimiter;
44
use rustc_ast::tokenstream::DelimSpan;
5-
use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, NodeId, ast, token};
5+
use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, ast, token};
66
use rustc_errors::{Applicability, PResult};
77
use rustc_feature::{AttributeTemplate, Features, template};
88
use rustc_hir::attrs::CfgEntry;
9+
use rustc_hir::lints::AttributeLintKind;
910
use rustc_hir::{AttrPath, RustcVersion};
1011
use rustc_parse::parser::{ForceCollect, Parser};
1112
use rustc_parse::{exp, parse_in};
1213
use rustc_session::Session;
1314
use rustc_session::config::ExpectedValues;
14-
use rustc_session::lint::BuiltinLintDiag;
15-
use rustc_session::lint::builtin::UNEXPECTED_CFGS;
1615
use rustc_session::parse::{ParseSess, feature_err};
1716
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
1817
use thin_vec::ThinVec;
@@ -23,10 +22,7 @@ use crate::session_diagnostics::{
2322
AttributeParseError, AttributeParseErrorReason, CfgAttrBadDelim, MetaBadDelimSugg,
2423
ParsedDescription,
2524
};
26-
use crate::{
27-
AttributeParser, CfgMatchesLintEmitter, fluent_generated, parse_version, session_diagnostics,
28-
try_gate_cfg,
29-
};
25+
use crate::{AttributeParser, fluent_generated, parse_version, session_diagnostics, try_gate_cfg};
3026

3127
pub const CFG_TEMPLATE: AttributeTemplate = template!(
3228
List: &["predicate"],
@@ -194,43 +190,40 @@ fn parse_name_value<S: Stage>(
194190
}
195191
};
196192

193+
match cx.sess.psess.check_config.expecteds.get(&name) {
194+
Some(ExpectedValues::Some(values)) if !values.contains(&value.map(|(v, _)| v)) => {
195+
cx.emit_lint(AttributeLintKind::UnexpectedCfgValue { name, name_span, value }, span);
196+
}
197+
None if cx.sess.psess.check_config.exhaustive_names => {
198+
cx.emit_lint(AttributeLintKind::UnexpectedCfgName { name, name_span, value }, span);
199+
}
200+
_ => { /* not unexpected */ }
201+
}
202+
197203
Ok(CfgEntry::NameValue { name, name_span, value, span })
198204
}
199205

200-
pub fn eval_config_entry(
201-
sess: &Session,
202-
cfg_entry: &CfgEntry,
203-
id: NodeId,
204-
emit_lints: ShouldEmit,
205-
) -> EvalConfigResult {
206+
pub fn eval_config_entry(sess: &Session, cfg_entry: &CfgEntry) -> EvalConfigResult {
206207
match cfg_entry {
207208
CfgEntry::All(subs, ..) => {
208-
let mut all = None;
209209
for sub in subs {
210-
let res = eval_config_entry(sess, sub, id, emit_lints);
211-
// We cannot short-circuit because `eval_config_entry` emits some lints
210+
let res = eval_config_entry(sess, sub);
212211
if !res.as_bool() {
213-
all.get_or_insert(res);
212+
return res;
214213
}
215214
}
216-
all.unwrap_or_else(|| EvalConfigResult::True)
215+
EvalConfigResult::True
217216
}
218217
CfgEntry::Any(subs, span) => {
219-
let mut any = None;
220218
for sub in subs {
221-
let res = eval_config_entry(sess, sub, id, emit_lints);
222-
// We cannot short-circuit because `eval_config_entry` emits some lints
223-
if res.as_bool() {
224-
any.get_or_insert(res);
219+
if eval_config_entry(sess, sub).as_bool() {
220+
return EvalConfigResult::True;
225221
}
226222
}
227-
any.unwrap_or_else(|| EvalConfigResult::False {
228-
reason: cfg_entry.clone(),
229-
reason_span: *span,
230-
})
223+
EvalConfigResult::False { reason: cfg_entry.clone(), reason_span: *span }
231224
}
232225
CfgEntry::Not(sub, span) => {
233-
if eval_config_entry(sess, sub, id, emit_lints).as_bool() {
226+
if eval_config_entry(sess, sub).as_bool() {
234227
EvalConfigResult::False { reason: cfg_entry.clone(), reason_span: *span }
235228
} else {
236229
EvalConfigResult::True
@@ -243,31 +236,7 @@ pub fn eval_config_entry(
243236
EvalConfigResult::False { reason: cfg_entry.clone(), reason_span: *span }
244237
}
245238
}
246-
CfgEntry::NameValue { name, name_span, value, span } => {
247-
if let ShouldEmit::ErrorsAndLints = emit_lints {
248-
match sess.psess.check_config.expecteds.get(name) {
249-
Some(ExpectedValues::Some(values))
250-
if !values.contains(&value.map(|(v, _)| v)) =>
251-
{
252-
id.emit_span_lint(
253-
sess,
254-
UNEXPECTED_CFGS,
255-
*span,
256-
BuiltinLintDiag::UnexpectedCfgValue((*name, *name_span), *value),
257-
);
258-
}
259-
None if sess.psess.check_config.exhaustive_names => {
260-
id.emit_span_lint(
261-
sess,
262-
UNEXPECTED_CFGS,
263-
*span,
264-
BuiltinLintDiag::UnexpectedCfgName((*name, *name_span), *value),
265-
);
266-
}
267-
_ => { /* not unexpected */ }
268-
}
269-
}
270-
239+
CfgEntry::NameValue { name, name_span: _, value, span } => {
271240
if sess.psess.config.contains(&(*name, value.map(|(v, _)| v))) {
272241
EvalConfigResult::True
273242
} else {

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'sess> AttributeParser<'sess, Early> {
115115
OmitDoc::Skip,
116116
std::convert::identity,
117117
|lint| {
118-
crate::lints::emit_attribute_lint(&lint, sess);
118+
crate::lints::emit_attribute_lint(&lint, sess, sess);
119119
},
120120
)
121121
}
@@ -187,7 +187,7 @@ impl<'sess> AttributeParser<'sess, Early> {
187187
target_span,
188188
target_id: target_node_id,
189189
emit_lint: &mut |lint| {
190-
crate::lints::emit_attribute_lint(&lint, sess);
190+
crate::lints::emit_attribute_lint(&lint, sess, sess);
191191
},
192192
},
193193
attr_span,

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ use std::borrow::Cow;
33
use rustc_errors::{DiagArgValue, LintEmitter};
44
use rustc_hir::Target;
55
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
6+
use rustc_session::Session;
67
use rustc_span::sym;
78

8-
use crate::session_diagnostics;
9+
use crate::{session_diagnostics, unexpected_cfg_name, unexpected_cfg_value};
910

10-
pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<L::Id>, lint_emitter: L) {
11+
//tcx: TyCtxt<'_>
12+
pub fn emit_attribute_lint<L: LintEmitter>(
13+
lint: &AttributeLint<L::Id>,
14+
lint_emitter: L,
15+
sess: &Session,
16+
) {
1117
let AttributeLint { id, span, kind } = lint;
1218

1319
match kind {
@@ -98,5 +104,19 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<L::Id>, lint_emi
98104
},
99105
)
100106
}
107+
&AttributeLintKind::UnexpectedCfgName { name, name_span, value } => lint_emitter
108+
.emit_node_span_lint(
109+
rustc_session::lint::builtin::UNEXPECTED_CFGS,
110+
*id,
111+
*span,
112+
unexpected_cfg_name(sess, None, (name, name_span), value),
113+
),
114+
&AttributeLintKind::UnexpectedCfgValue { name, name_span, value } => lint_emitter
115+
.emit_node_span_lint(
116+
rustc_session::lint::builtin::UNEXPECTED_CFGS,
117+
*id,
118+
*span,
119+
unexpected_cfg_value(sess, None, (name, name_span), value),
120+
),
101121
}
102122
}

compiler/rustc_builtin_macros/src/cfg.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,7 @@ pub(crate) fn expand_cfg(
2626

2727
ExpandResult::Ready(match parse_cfg(cx, sp, tts) {
2828
Ok(cfg) => {
29-
let matches_cfg = attr::eval_config_entry(
30-
cx.sess,
31-
&cfg,
32-
cx.current_expansion.lint_node_id,
33-
ShouldEmit::ErrorsAndLints,
34-
)
35-
.as_bool();
29+
let matches_cfg = attr::eval_config_entry(cx.sess, &cfg).as_bool();
3630

3731
MacEager::expr(cx.expr_bool(sp, matches_cfg))
3832
}

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use find_msvc_tools;
1313
use itertools::Itertools;
1414
use regex::Regex;
1515
use rustc_arena::TypedArena;
16-
use rustc_ast::CRATE_NODE_ID;
17-
use rustc_attr_parsing::{ShouldEmit, eval_config_entry};
16+
use rustc_attr_parsing::eval_config_entry;
1817
use rustc_data_structures::fx::FxIndexSet;
1918
use rustc_data_structures::memmap::Mmap;
2019
use rustc_data_structures::temp_dir::MaybeTempDir;
@@ -3035,9 +3034,7 @@ fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
30353034

30363035
fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
30373036
match lib.cfg {
3038-
Some(ref cfg) => {
3039-
eval_config_entry(sess, cfg, CRATE_NODE_ID, ShouldEmit::ErrorsAndLints).as_bool()
3040-
}
3037+
Some(ref cfg) => eval_config_entry(sess, cfg).as_bool(),
30413038
None => true,
30423039
}
30433040
}

compiler/rustc_expand/src/config.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ pub fn pre_configure_attrs(sess: &Session, attrs: &[Attribute]) -> ast::AttrVec
165165
.iter()
166166
.flat_map(|attr| strip_unconfigured.process_cfg_attr(attr))
167167
.take_while(|attr| {
168-
!is_cfg(attr)
169-
|| strip_unconfigured
170-
.cfg_true(attr, strip_unconfigured.lint_node_id, ShouldEmit::Nothing)
171-
.as_bool()
168+
!is_cfg(attr) || strip_unconfigured.cfg_true(attr, ShouldEmit::Nothing).as_bool()
172169
})
173170
.collect()
174171
}
@@ -318,14 +315,7 @@ impl<'a> StripUnconfigured<'a> {
318315
);
319316
}
320317

321-
if !attr::eval_config_entry(
322-
self.sess,
323-
&cfg_predicate,
324-
ast::CRATE_NODE_ID,
325-
ShouldEmit::ErrorsAndLints,
326-
)
327-
.as_bool()
328-
{
318+
if !attr::eval_config_entry(self.sess, &cfg_predicate).as_bool() {
329319
return vec![trace_attr];
330320
}
331321

@@ -409,18 +399,12 @@ impl<'a> StripUnconfigured<'a> {
409399

410400
/// Determines if a node with the given attributes should be included in this configuration.
411401
fn in_cfg(&self, attrs: &[Attribute]) -> bool {
412-
attrs.iter().all(|attr| {
413-
!is_cfg(attr)
414-
|| self.cfg_true(attr, self.lint_node_id, ShouldEmit::ErrorsAndLints).as_bool()
415-
})
402+
attrs
403+
.iter()
404+
.all(|attr| !is_cfg(attr) || self.cfg_true(attr, ShouldEmit::ErrorsAndLints).as_bool())
416405
}
417406

418-
pub(crate) fn cfg_true(
419-
&self,
420-
attr: &Attribute,
421-
node: NodeId,
422-
emit_errors: ShouldEmit,
423-
) -> EvalConfigResult {
407+
pub(crate) fn cfg_true(&self, attr: &Attribute, emit_errors: ShouldEmit) -> EvalConfigResult {
424408
// Unsafety check needs to be done explicitly here because this attribute will be removed before the normal check
425409
deny_builtin_meta_unsafety(
426410
self.sess.dcx(),
@@ -432,7 +416,7 @@ impl<'a> StripUnconfigured<'a> {
432416
self.sess,
433417
attr,
434418
attr.span,
435-
node,
419+
self.lint_node_id,
436420
self.features,
437421
emit_errors,
438422
parse_cfg,
@@ -442,7 +426,7 @@ impl<'a> StripUnconfigured<'a> {
442426
return EvalConfigResult::True;
443427
};
444428

445-
eval_config_entry(self.sess, &cfg, self.lint_node_id, emit_errors)
429+
eval_config_entry(self.sess, &cfg)
446430
}
447431

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

compiler/rustc_expand/src/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,11 +2216,11 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22162216

22172217
fn expand_cfg_true(
22182218
&mut self,
2219-
node: &mut (impl HasAttrs + HasNodeId),
2219+
node: &mut impl HasAttrs,
22202220
attr: ast::Attribute,
22212221
pos: usize,
22222222
) -> EvalConfigResult {
2223-
let res = self.cfg().cfg_true(&attr, node.node_id(), ShouldEmit::ErrorsAndLints);
2223+
let res = self.cfg().cfg_true(&attr, ShouldEmit::ErrorsAndLints);
22242224
if res.as_bool() {
22252225
// A trace attribute left in AST in place of the original `cfg` attribute.
22262226
// It can later be used by lints or other diagnostics.

compiler/rustc_hir/src/lints.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_data_structures::fingerprint::Fingerprint;
22
use rustc_macros::HashStable_Generic;
3-
use rustc_span::Span;
3+
use rustc_span::{Span, Symbol};
44

55
use crate::{AttrPath, HirId, Target};
66

@@ -62,4 +62,14 @@ pub enum AttributeLintKind {
6262
target: Target,
6363
target_span: Span,
6464
},
65+
UnexpectedCfgName {
66+
name: Symbol,
67+
name_span: Span,
68+
value: Option<(Symbol, Span)>,
69+
},
70+
UnexpectedCfgValue {
71+
name: Symbol,
72+
name_span: Span,
73+
value: Option<(Symbol, Span)>,
74+
},
6575
}

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub fn provide(providers: &mut Providers) {
158158
fn emit_delayed_lint(lint: &DelayedLint, tcx: TyCtxt<'_>) {
159159
match lint {
160160
DelayedLint::AttributeParsing(attribute_lint) => {
161-
rustc_attr_parsing::emit_attribute_lint(attribute_lint, tcx)
161+
rustc_attr_parsing::emit_attribute_lint(attribute_lint, tcx, tcx.sess)
162162
}
163163
}
164164
}

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::ops::ControlFlow;
22
use std::path::{Path, PathBuf};
33

44
use rustc_abi::ExternAbi;
5-
use rustc_ast::CRATE_NODE_ID;
6-
use rustc_attr_parsing::{ShouldEmit, eval_config_entry};
5+
use rustc_attr_parsing::eval_config_entry;
76
use rustc_data_structures::fx::FxHashSet;
87
use rustc_hir::attrs::{AttributeKind, NativeLibKind, PeImportNameType};
98
use rustc_hir::find_attr;
@@ -188,9 +187,7 @@ pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> Vec<NativeLib>
188187

189188
pub(crate) fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
190189
match lib.cfg {
191-
Some(ref cfg) => {
192-
eval_config_entry(sess, cfg, CRATE_NODE_ID, ShouldEmit::ErrorsAndLints).as_bool()
193-
}
190+
Some(ref cfg) => eval_config_entry(sess, cfg).as_bool(),
194191
None => true,
195192
}
196193
}

0 commit comments

Comments
 (0)