Skip to content

Commit 8a1e8fb

Browse files
Port #[cfg] to the new attribute parsing infrastructure
1 parent 0d11be5 commit 8a1e8fb

File tree

41 files changed

+858
-426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+858
-426
lines changed

compiler/rustc_ast/src/expand/mod.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
//! Definitions shared by macros / syntax extensions and e.g. `rustc_middle`.
22
33
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
4-
use rustc_span::Ident;
5-
use rustc_span::def_id::DefId;
6-
7-
use crate::MetaItem;
84

95
pub mod allocator;
106
pub mod autodiff_attrs;
117
pub mod typetree;
12-
13-
#[derive(Debug, Clone, Encodable, Decodable, HashStable_Generic)]
14-
pub struct StrippedCfgItem<ModId = DefId> {
15-
pub parent_module: ModId,
16-
pub ident: Ident,
17-
pub cfg: MetaItem,
18-
}
19-
20-
impl<ModId> StrippedCfgItem<ModId> {
21-
pub fn map_mod_id<New>(self, f: impl FnOnce(ModId) -> New) -> StrippedCfgItem<New> {
22-
StrippedCfgItem { parent_module: f(self.parent_module), ident: self.ident, cfg: self.cfg }
23-
}
24-
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::sync::Arc;
4242

4343
use rustc_ast::node_id::NodeMap;
4444
use rustc_ast::{self as ast, *};
45-
use rustc_attr_parsing::{AttributeParser, OmitDoc};
45+
use rustc_attr_parsing::{AttributeParser, Late, OmitDoc};
4646
use rustc_data_structures::fingerprint::Fingerprint;
4747
use rustc_data_structures::sorted_map::SortedMap;
4848
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -192,7 +192,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
192192
// interact with `gen`/`async gen` blocks
193193
allow_async_iterator: [sym::gen_future, sym::async_iterator].into(),
194194

195-
attribute_parser: AttributeParser::new(tcx.sess, tcx.features(), registered_tools),
195+
attribute_parser: AttributeParser::new(
196+
tcx.sess,
197+
tcx.features(),
198+
registered_tools,
199+
Late,
200+
),
196201
delayed_lints: Vec::new(),
197202
}
198203
}

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::token::CommentKind;
33
use rustc_ast::{self as ast, AttrStyle};
44
use rustc_macros::{Decodable, Encodable, HashStable_Generic, PrintAttribute};
55
use rustc_span::hygiene::Transparency;
6-
use rustc_span::{Span, Symbol};
6+
use rustc_span::{Ident, Span, Symbol};
77
use thin_vec::ThinVec;
88

99
use crate::{DefaultBodyStability, PartialConstStability, PrintAttribute, RustcVersion, Stability};
@@ -69,6 +69,7 @@ pub enum ReprAttr {
6969
ReprAlign(Align),
7070
}
7171
pub use ReprAttr::*;
72+
use rustc_span::def_id::DefId;
7273

7374
pub enum TransparencyError {
7475
UnknownTransparency(Symbol, Span),
@@ -140,6 +141,30 @@ pub enum UsedBy {
140141
Linker,
141142
}
142143

144+
#[derive(Debug, Clone, Encodable, Decodable, HashStable_Generic)]
145+
pub struct StrippedCfgItem<ModId = DefId> {
146+
pub parent_module: ModId,
147+
pub ident: Ident,
148+
pub cfg: (CfgEntry, Span),
149+
}
150+
151+
impl<ModId> StrippedCfgItem<ModId> {
152+
pub fn map_mod_id<New>(self, f: impl FnOnce(ModId) -> New) -> StrippedCfgItem<New> {
153+
StrippedCfgItem { parent_module: f(self.parent_module), ident: self.ident, cfg: self.cfg }
154+
}
155+
}
156+
157+
#[derive(Encodable, Decodable, Clone, Debug, PartialEq, Eq, Hash)]
158+
#[derive(HashStable_Generic, PrintAttribute)]
159+
pub enum CfgEntry {
160+
All(ThinVec<CfgEntry>, Span),
161+
Any(ThinVec<CfgEntry>, Span),
162+
Not(Box<CfgEntry>, Span),
163+
Bool(bool, Span),
164+
NameValue { name: Symbol, name_span: Span, value: Option<(Symbol, Span)>, span: Span },
165+
Version(Option<RustcVersion>, Span),
166+
}
167+
143168
/// Represents parsed *built-in* inert attributes.
144169
///
145170
/// ## Overview
@@ -211,6 +236,9 @@ pub enum AttributeKind {
211236
span: Span,
212237
},
213238

239+
/// Represents `#[cfg]`.
240+
Cfg(CfgEntry, Span),
241+
214242
/// Represents `#[cold]`.
215243
Cold(Span),
216244

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl AttributeKind {
1818
AllowInternalUnstable(..) => Yes,
1919
AsPtr(..) => Yes,
2020
BodyStability { .. } => No,
21+
Cfg { .. } => Yes,
2122
Cold(..) => No,
2223
Confusables { .. } => Yes,
2324
ConstContinue(..) => No,

0 commit comments

Comments
 (0)