Skip to content

Commit 2da5a4c

Browse files
Port #[cfg] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent a577ea0 commit 2da5a4c

File tree

25 files changed

+434
-93
lines changed

25 files changed

+434
-93
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_attr_data_structures/src/attributes.rs

Lines changed: 28 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,29 @@ 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+
}
166+
143167
/// Represents parsed *built-in* inert attributes.
144168
///
145169
/// ## Overview
@@ -211,6 +235,9 @@ pub enum AttributeKind {
211235
span: Span,
212236
},
213237

238+
/// Represents `#[cfg]`.
239+
Cfg(CfgEntry, Span),
240+
214241
/// Represents `#[cold]`.
215242
Cold(Span),
216243

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl AttributeKind {
1717
AllowInternalUnstable(..) => Yes,
1818
AsPtr(..) => Yes,
1919
BodyStability { .. } => No,
20+
Cfg(..) => Yes,
2021
Confusables { .. } => Yes,
2122
ConstStability { .. } => Yes,
2223
ConstStabilityIndirect => No,

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn cfg_matches(
7474
})
7575
}
7676

77-
fn try_gate_cfg(name: Symbol, span: Span, sess: &Session, features: Option<&Features>) {
77+
pub fn try_gate_cfg(name: Symbol, span: Span, sess: &Session, features: Option<&Features>) {
7878
let gate = find_gated_cfg(|sym| sym == name);
7979
if let (Some(feats), Some(gated_cfg)) = (features, gate) {
8080
gate_cfg(gated_cfg, span, sess, feats);

0 commit comments

Comments
 (0)