Skip to content

Commit 30132fa

Browse files
Wip
1 parent 31010ca commit 30132fa

File tree

11 files changed

+95
-12
lines changed

11 files changed

+95
-12
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ impl Attribute {
6262
}
6363
}
6464

65+
pub fn get_mut_normal_item(&mut self) -> &mut AttrItem {
66+
match &mut self.kind {
67+
AttrKind::Normal(normal) => &mut normal.item,
68+
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
69+
}
70+
}
71+
6572
pub fn unwrap_normal_item(self) -> AttrItem {
6673
match self.kind {
6774
AttrKind::Normal(normal) => normal.item,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use rustc_feature::AttributeTemplate;
2+
use rustc_hir::attrs::{AttributeKind, CfgEntry};
3+
use rustc_span::{Symbol, sym};
4+
5+
use crate::{parse_cfg_entry, CFG_TEMPLATE};
6+
use crate::attributes::{CombineAttributeParser, ConvertFn};
7+
use crate::context::{AcceptContext, Stage};
8+
use crate::parser::ArgParser;
9+
use crate::target_checking::{ALL_TARGETS, AllowedTargets};
10+
11+
pub(crate) struct CfgTraceParser;
12+
13+
impl<S: Stage> CombineAttributeParser<S> for CfgTraceParser {
14+
const PATH: &[Symbol] = &[sym::cfg_trace];
15+
type Item = CfgEntry;
16+
const CONVERT: ConvertFn<Self::Item> = AttributeKind::CfgTrace;
17+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
18+
const TEMPLATE: AttributeTemplate = CFG_TEMPLATE;
19+
20+
fn extend(
21+
cx: &mut AcceptContext<'_, '_, S>,
22+
args: &ArgParser,
23+
) -> impl IntoIterator<Item = Self::Item> {
24+
let Some(list) = args.list() else {
25+
cx.expected_list(cx.attr_span, args);
26+
return None;
27+
};
28+
let Some(entry) = list.single() else {
29+
cx.expected_single_argument(list.span);
30+
return None;
31+
};
32+
33+
parse_cfg_entry(cx, entry).ok()
34+
}
35+
}
36+
37+
pub(crate) struct CfgAttrTraceParser;
38+
39+
impl<S: Stage> CombineAttributeParser<S> for CfgAttrTraceParser {
40+
const PATH: &[Symbol] = &[sym::cfg_attr_trace];
41+
type Item = ();
42+
const CONVERT: ConvertFn<Self::Item> = AttributeKind::CfgAttrTrace;
43+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
44+
const TEMPLATE: AttributeTemplate = CFG_TEMPLATE;
45+
46+
fn extend(
47+
_cx: &mut AcceptContext<'_, '_, S>,
48+
_args: &ArgParser,
49+
) -> impl IntoIterator<Item = Self::Item> {
50+
Some(())
51+
}
52+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub(crate) mod allow_unstable;
3333
pub(crate) mod body;
3434
pub(crate) mod cfg;
3535
pub(crate) mod cfg_select;
36+
pub(crate) mod cfg_trace;
3637
pub(crate) mod codegen_attrs;
3738
pub(crate) mod confusables;
3839
pub(crate) mod crate_level;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::attributes::allow_unstable::{
1919
AllowConstFnUnstableParser, AllowInternalUnstableParser, UnstableFeatureBoundParser,
2020
};
2121
use crate::attributes::body::CoroutineParser;
22+
use crate::attributes::cfg_trace::{CfgAttrTraceParser, CfgTraceParser};
2223
use crate::attributes::codegen_attrs::{
2324
ColdParser, CoverageParser, EiiExternItemParser, ExportNameParser, ForceTargetFeatureParser,
2425
NakedParser, NoMangleParser, ObjcClassParser, ObjcSelectorParser, OptimizeParser,
@@ -174,6 +175,8 @@ attribute_parsers!(
174175
// tidy-alphabetical-start
175176
Combine<AllowConstFnUnstableParser>,
176177
Combine<AllowInternalUnstableParser>,
178+
Combine<CfgAttrTraceParser>,
179+
Combine<CfgTraceParser>,
177180
Combine<DebuggerViualizerParser>,
178181
Combine<ForceTargetFeatureParser>,
179182
Combine<LinkParser>,

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl<'sess> AttributeParser<'sess, Early> {
206206
safety,
207207
&mut emit_lint,
208208
target_node_id,
209+
emit_errors,
209210
)
210211
}
211212
let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
@@ -304,13 +305,20 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
304305
ast::AttrKind::Normal(n) => {
305306
attr_paths.push(PathParser(&n.item.path));
306307
let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
308+
let mut should_emit = self.stage.should_emit();
309+
310+
// Don't emit anything for trace attributes
311+
if attr.has_any_name(&[sym::cfg_trace, sym::cfg_attr_trace]) {
312+
should_emit = ShouldEmit::Nothing;
313+
}
307314

308315
self.check_attribute_safety(
309316
&attr_path,
310317
lower_span(n.item.span()),
311318
n.item.unsafety,
312319
&mut emit_lint,
313320
target_id,
321+
should_emit,
314322
);
315323

316324
let parts =
@@ -321,7 +329,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
321329
&n.item.args,
322330
&parts,
323331
&self.sess.psess,
324-
self.stage.should_emit(),
332+
should_emit,
325333
) else {
326334
continue;
327335
};
@@ -374,7 +382,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
374382
};
375383

376384
(accept.accept_fn)(&mut cx, &args);
377-
if !matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
385+
if !matches!(should_emit, ShouldEmit::Nothing) {
378386
Self::check_target(&accept.allowed_targets, target, &mut cx);
379387
}
380388
}

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ impl ArgParser {
120120
}
121121

122122
if args.delim != Delimiter::Parenthesis {
123-
psess.dcx().emit_err(MetaBadDelim {
123+
should_emit.emit_err(psess.dcx().create_err(MetaBadDelim {
124124
span: args.dspan.entire(),
125125
sugg: MetaBadDelimSugg { open: args.dspan.open, close: args.dspan.close },
126-
});
126+
}));
127127
return None;
128128
}
129129

compiler/rustc_attr_parsing/src/safety.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
1717
attr_safety: Safety,
1818
emit_lint: &mut impl FnMut(AttributeLint<S::Id>),
1919
target_id: S::Id,
20+
should_emit: ShouldEmit,
2021
) {
21-
if matches!(self.stage.should_emit(), ShouldEmit::Nothing) {
22+
if matches!(should_emit, ShouldEmit::Nothing) {
2223
return;
2324
}
2425

compiler/rustc_expand/src/config.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use rustc_ast::tokenstream::{
77
AttrTokenStream, AttrTokenTree, LazyAttrTokenStream, Spacing, TokenTree,
88
};
99
use rustc_ast::{
10-
self as ast, AttrKind, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem, MetaItemInner,
11-
NodeId, NormalAttr,
10+
self as ast, AttrArgs, AttrKind, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem,
11+
MetaItemInner, NodeId, NormalAttr,
1212
};
1313
use rustc_attr_parsing as attr;
1414
use rustc_attr_parsing::{
@@ -288,7 +288,10 @@ impl<'a> StripUnconfigured<'a> {
288288
pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
289289
// A trace attribute left in AST in place of the original `cfg_attr` attribute.
290290
// It can later be used by lints or other diagnostics.
291-
let trace_attr = attr_into_trace(cfg_attr.clone(), sym::cfg_attr_trace);
291+
// Remove the arguments, because they won't be used later and may trip up the attribute parser
292+
let mut trace_attr = cfg_attr.clone();
293+
trace_attr.get_mut_normal_item().args = AttrArgs::Empty;
294+
let trace_attr = attr_into_trace(trace_attr, sym::cfg_attr_trace);
292295

293296
let Some((cfg_predicate, expanded_attrs)) =
294297
rustc_attr_parsing::parse_cfg_attr(cfg_attr, &self.sess, self.features)

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,14 @@ pub enum AttributeKind {
647647
span: Span,
648648
},
649649

650+
/// Represents `#[cfg_attr]` trace attributes
651+
/// These are attributes left behind in the HIR after `#[cfg_attr]` attributes are processed
652+
CfgAttrTrace(ThinVec<()>, Span),
653+
654+
/// Represents `#[cfg]` trace attributes
655+
/// These are attributes left behind in the HIR after `#[cfg]` attributes are processed
656+
CfgTrace(ThinVec<CfgEntry>, Span),
657+
650658
/// Represents `#[rustc_coinductive]`.
651659
Coinductive(Span),
652660

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ impl AttributeKind {
2626
AsPtr(..) => Yes,
2727
AutomaticallyDerived(..) => Yes,
2828
BodyStability { .. } => No,
29+
CfgAttrTrace(..) => Yes,
30+
CfgTrace(..) => Yes,
2931
Coinductive(..) => No,
3032
Cold(..) => No,
3133
Confusables { .. } => Yes,

0 commit comments

Comments
 (0)