Skip to content

Commit f97143d

Browse files
committed
Use a linting node closer the parsing of #[cfg_attr]
1 parent 8317fef commit f97143d

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::convert::identity;
22

33
use rustc_ast::token::Delimiter;
44
use rustc_ast::tokenstream::DelimSpan;
5-
use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, ast, token};
5+
use rustc_ast::{AttrItem, Attribute, LitKind, ast, token};
66
use rustc_errors::{Applicability, PResult, msg};
77
use rustc_feature::{
88
AttrSuggestionStyle, AttributeTemplate, Features, GatedCfg, find_gated_cfg, template,
@@ -324,12 +324,13 @@ pub fn parse_cfg_attr(
324324
cfg_attr: &Attribute,
325325
sess: &Session,
326326
features: Option<&Features>,
327+
lint_node_id: ast::NodeId,
327328
) -> Option<(CfgEntry, Vec<(AttrItem, Span)>)> {
328329
match cfg_attr.get_normal_item().args.unparsed_ref().unwrap() {
329330
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, delim, tokens }) if !tokens.is_empty() => {
330331
check_cfg_attr_bad_delim(&sess.psess, *dspan, *delim);
331332
match parse_in(&sess.psess, tokens.clone(), "`cfg_attr` input", |p| {
332-
parse_cfg_attr_internal(p, sess, features, cfg_attr)
333+
parse_cfg_attr_internal(p, sess, features, lint_node_id, cfg_attr)
333334
}) {
334335
Ok(r) => return Some(r),
335336
Err(e) => {
@@ -390,6 +391,7 @@ fn parse_cfg_attr_internal<'a>(
390391
parser: &mut Parser<'a>,
391392
sess: &'a Session,
392393
features: Option<&Features>,
394+
lint_node_id: ast::NodeId,
393395
attribute: &Attribute,
394396
) -> PResult<'a, (CfgEntry, Vec<(ast::AttrItem, Span)>)> {
395397
// Parse cfg predicate
@@ -410,7 +412,7 @@ fn parse_cfg_attr_internal<'a>(
410412
Some(attribute.get_normal_item().unsafety),
411413
ParsedDescription::Attribute,
412414
pred_span,
413-
CRATE_NODE_ID,
415+
lint_node_id,
414416
Target::Crate,
415417
features,
416418
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },

compiler/rustc_expand/src/config.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,12 @@ impl<'a> StripUnconfigured<'a> {
283283
trace_attr.replace_args(AttrItemKind::Parsed(EarlyParsedAttribute::CfgAttrTrace));
284284
let trace_attr = attr_into_trace(trace_attr, sym::cfg_attr_trace);
285285

286-
let Some((cfg_predicate, expanded_attrs)) =
287-
rustc_attr_parsing::parse_cfg_attr(cfg_attr, &self.sess, self.features)
288-
else {
286+
let Some((cfg_predicate, expanded_attrs)) = rustc_attr_parsing::parse_cfg_attr(
287+
cfg_attr,
288+
&self.sess,
289+
self.features,
290+
self.lint_node_id,
291+
) else {
289292
return vec![trace_attr];
290293
};
291294

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This test check that a module-level `#![allow(unexpected_cfgs)]` works
2+
//
3+
// Related to https://github.com/rust-lang/rust/issues/155118
4+
//
5+
//@ check-pass
6+
//@ no-auto-check-cfg
7+
//@ compile-flags: --check-cfg=cfg()
8+
9+
mod my_mod {
10+
#![allow(unexpected_cfgs)]
11+
12+
#[cfg_attr(asan, sanitize(address = "off"))]
13+
static MY_ITEM: () = ();
14+
}
15+
16+
fn main() {}

0 commit comments

Comments
 (0)