Skip to content

Commit 5363fcf

Browse files
Define datastructures for #[cfg] attribute, move StrippedCfgItem
1 parent 855e0fe commit 5363fcf

File tree

10 files changed

+53
-31
lines changed

10 files changed

+53
-31
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: 26 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

compiler/rustc_expand/src/base.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_ast::token::MetaVarKind;
1111
use rustc_ast::tokenstream::TokenStream;
1212
use rustc_ast::visit::{AssocCtxt, Visitor};
1313
use rustc_ast::{self as ast, AttrVec, Attribute, HasAttrs, Item, NodeId, PatKind};
14-
use rustc_attr_data_structures::{AttributeKind, Deprecation, Stability, find_attr};
14+
use rustc_attr_data_structures::{AttributeKind, CfgEntry, Deprecation, Stability, find_attr};
1515
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
1616
use rustc_data_structures::sync;
1717
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, PResult};
@@ -1108,7 +1108,13 @@ pub trait ResolverExpand {
11081108
/// HIR proc macros items back to their harness items.
11091109
fn declare_proc_macro(&mut self, id: NodeId);
11101110

1111-
fn append_stripped_cfg_item(&mut self, parent_node: NodeId, ident: Ident, cfg: ast::MetaItem);
1111+
fn append_stripped_cfg_item(
1112+
&mut self,
1113+
parent_node: NodeId,
1114+
ident: Ident,
1115+
cfg: CfgEntry,
1116+
cfg_span: Span,
1117+
);
11121118

11131119
/// Tools registered with `#![register_tool]` and used by tool attributes and lints.
11141120
fn registered_tools(&self) -> &RegisteredTools;

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use def_path_hash_map::DefPathHashMapRef;
77
use encoder::EncodeContext;
88
pub use encoder::{EncodedMetadata, encode_metadata, rendered_const};
99
use rustc_abi::{FieldIdx, ReprOptions, VariantIdx};
10-
use rustc_ast::expand::StrippedCfgItem;
10+
use rustc_attr_data_structures::StrippedCfgItem;
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::svh::Svh;
1313
use rustc_hir::PreciseCapturingArgKind;

compiler/rustc_middle/src/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ macro_rules! arena_types {
110110
[] external_constraints: rustc_middle::traits::solve::ExternalConstraintsData<rustc_middle::ty::TyCtxt<'tcx>>,
111111
[] predefined_opaques_in_body: rustc_middle::traits::solve::PredefinedOpaquesData<rustc_middle::ty::TyCtxt<'tcx>>,
112112
[decode] doc_link_resolutions: rustc_hir::def::DocLinkResMap,
113-
[] stripped_cfg_items: rustc_ast::expand::StrippedCfgItem,
113+
[] stripped_cfg_items: rustc_attr_data_structures::StrippedCfgItem,
114114
[] mod_child: rustc_middle::metadata::ModChild,
115115
[] features: rustc_feature::Features,
116116
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ use std::sync::Arc;
6969

7070
use rustc_abi::Align;
7171
use rustc_arena::TypedArena;
72-
use rustc_ast::expand::StrippedCfgItem;
7372
use rustc_ast::expand::allocator::AllocatorKind;
73+
use rustc_attr_data_structures::StrippedCfgItem;
7474
use rustc_data_structures::fingerprint::Fingerprint;
7575
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
7676
use rustc_data_structures::sorted_map::SortedMap;

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ pub use generic_args::{GenericArgKind, TermKind, *};
2525
pub use generics::*;
2626
pub use intrinsic::IntrinsicDef;
2727
use rustc_abi::{Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, VariantIdx};
28-
use rustc_ast::expand::StrippedCfgItem;
2928
use rustc_ast::node_id::NodeMap;
3029
pub use rustc_ast_ir::{Movability, Mutability, try_visit};
31-
use rustc_attr_data_structures::AttributeKind;
30+
use rustc_attr_data_structures::{AttributeKind, StrippedCfgItem};
3231
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3332
use rustc_data_structures::intern::Interned;
3433
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};

compiler/rustc_middle/src/ty/parameterized.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ trivially_parameterized_over_tcx! {
8383
ty::IntrinsicDef,
8484
rustc_ast::Attribute,
8585
rustc_ast::DelimArgs,
86-
rustc_ast::expand::StrippedCfgItem<rustc_hir::def_id::DefIndex>,
86+
rustc_attr_data_structures::StrippedCfgItem<rustc_hir::def_id::DefIndex>,
8787
rustc_attr_data_structures::ConstStability,
8888
rustc_attr_data_structures::DefaultBodyStability,
8989
rustc_attr_data_structures::Deprecation,

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ use late::{
3636
};
3737
use macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
3838
use rustc_arena::{DroplessArena, TypedArena};
39-
use rustc_ast::expand::StrippedCfgItem;
4039
use rustc_ast::node_id::NodeMap;
4140
use rustc_ast::{
4241
self as ast, AngleBracketedArg, CRATE_NODE_ID, Crate, Expr, ExprKind, GenericArg, GenericArgs,
4342
LitKind, NodeId, Path, attr,
4443
};
44+
use rustc_attr_data_structures::StrippedCfgItem;
4545
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
4646
use rustc_data_structures::intern::Interned;
4747
use rustc_data_structures::steal::Steal;

compiler/rustc_resolve/src/macros.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use std::cell::Cell;
55
use std::mem;
66
use std::sync::Arc;
77

8-
use rustc_ast::expand::StrippedCfgItem;
98
use rustc_ast::{self as ast, Crate, NodeId, attr};
109
use rustc_ast_pretty::pprust;
11-
use rustc_attr_data_structures::StabilityLevel;
10+
use rustc_attr_data_structures::{CfgEntry, StabilityLevel, StrippedCfgItem};
1211
use rustc_errors::{Applicability, DiagCtxtHandle, StashKey};
1312
use rustc_expand::base::{
1413
Annotatable, DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension,
@@ -485,8 +484,18 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
485484
self.proc_macros.push(self.local_def_id(id))
486485
}
487486

488-
fn append_stripped_cfg_item(&mut self, parent_node: NodeId, ident: Ident, cfg: ast::MetaItem) {
489-
self.stripped_cfg_items.push(StrippedCfgItem { parent_module: parent_node, ident, cfg });
487+
fn append_stripped_cfg_item(
488+
&mut self,
489+
parent_node: NodeId,
490+
ident: Ident,
491+
cfg: CfgEntry,
492+
cfg_span: Span,
493+
) {
494+
self.stripped_cfg_items.push(StrippedCfgItem {
495+
parent_module: parent_node,
496+
ident,
497+
cfg: (cfg, cfg_span),
498+
});
490499
}
491500

492501
fn registered_tools(&self) -> &RegisteredTools {

0 commit comments

Comments
 (0)