Skip to content

Commit d0cdfab

Browse files
committed
Implment #[cfg] in where clauses
1 parent 78668c5 commit d0cdfab

File tree

32 files changed

+1566
-66
lines changed

32 files changed

+1566
-66
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ impl Default for WhereClause {
429429
/// A single predicate in a where-clause.
430430
#[derive(Clone, Encodable, Decodable, Debug)]
431431
pub struct WherePredicate {
432+
pub attrs: AttrVec,
432433
pub kind: WherePredicateKind,
433434
pub id: NodeId,
434435
pub span: Span,
@@ -444,6 +445,7 @@ impl WherePredicate {
444445
f: impl FnOnce(&WherePredicateKind) -> WherePredicateKind,
445446
) -> WherePredicate {
446447
WherePredicate {
448+
attrs: self.attrs.clone(),
447449
kind: f(&self.kind),
448450
id: DUMMY_NODE_ID,
449451
span: ctxt.map_or(self.span, |ctxt| self.span.with_ctxt(ctxt)),

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::tokenstream::LazyAttrTokenStream;
1111
use crate::{
1212
Arm, AssocItem, AttrItem, AttrKind, AttrVec, Attribute, Block, Crate, Expr, ExprField,
1313
FieldDef, ForeignItem, GenericParam, Item, NodeId, Param, Pat, PatField, Path, Stmt, StmtKind,
14-
Ty, Variant, Visibility,
14+
Ty, Variant, Visibility, WherePredicate,
1515
};
1616

1717
/// A utility trait to reduce boilerplate.
@@ -79,6 +79,7 @@ impl_has_node_id!(
7979
Stmt,
8080
Ty,
8181
Variant,
82+
WherePredicate,
8283
);
8384

8485
impl<T: AstDeref<Target: HasNodeId>> HasNodeId for T {
@@ -127,7 +128,16 @@ macro_rules! impl_has_tokens_none {
127128
}
128129

129130
impl_has_tokens!(AssocItem, AttrItem, Block, Expr, ForeignItem, Item, Pat, Path, Ty, Visibility);
130-
impl_has_tokens_none!(Arm, ExprField, FieldDef, GenericParam, Param, PatField, Variant);
131+
impl_has_tokens_none!(
132+
Arm,
133+
ExprField,
134+
FieldDef,
135+
GenericParam,
136+
Param,
137+
PatField,
138+
Variant,
139+
WherePredicate
140+
);
131141

132142
impl<T: AstDeref<Target: HasTokens>> HasTokens for T {
133143
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
@@ -289,6 +299,7 @@ impl_has_attrs!(
289299
Param,
290300
PatField,
291301
Variant,
302+
WherePredicate,
292303
);
293304
impl_has_attrs_none!(Attribute, AttrItem, Block, Pat, Path, Ty, Visibility);
294305

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,9 @@ pub fn walk_flat_map_where_predicate<T: MutVisitor>(
10021002
vis: &mut T,
10031003
mut pred: WherePredicate,
10041004
) -> SmallVec<[WherePredicate; 1]> {
1005-
let WherePredicate { ref mut kind, ref mut id, ref mut span } = pred;
1005+
let WherePredicate { ref mut attrs, ref mut kind, ref mut id, ref mut span } = pred;
10061006
vis.visit_id(id);
1007+
visit_attrs(vis, attrs);
10071008
vis.visit_where_predicate_kind(kind);
10081009
vis.visit_span(span);
10091010
smallvec![pred]

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,8 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(
789789
visitor: &mut V,
790790
predicate: &'a WherePredicate,
791791
) -> V::Result {
792-
let WherePredicate { kind, id: _, span: _ } = predicate;
792+
let WherePredicate { attrs, kind, id: _, span: _ } = predicate;
793+
walk_list!(visitor, visit_attribute, attrs);
793794
visitor.visit_where_predicate_kind(kind)
794795
}
795796

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15791579

15801580
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
15811581
let hir_id = self.lower_node_id(pred.id);
1582+
self.lower_attrs(hir_id, &pred.attrs);
15821583
let kind = match &pred.kind {
15831584
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
15841585
bound_generic_params,

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
547547
gate_all!(global_registration, "global registration is experimental");
548548
gate_all!(return_type_notation, "return type notation is experimental");
549549
gate_all!(pin_ergonomics, "pinned reference syntax is experimental");
550+
gate_all!(cfg_attribute_in_where, "`#[cfg]` attribute in `where` clause is unstable");
550551

551552
if !visitor.features.never_patterns() {
552553
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,9 @@ impl<'a> State<'a> {
726726
}
727727

728728
pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
729-
let &ast::WherePredicate { ref kind, id, span: _ } = predicate;
729+
let &ast::WherePredicate { ref attrs, ref kind, id, span: _ } = predicate;
730730
self.ann.pre(self, AnnNode::SubItem(id));
731+
self.print_outer_attributes(attrs);
731732
match kind {
732733
ast::WherePredicateKind::BoundPredicate(where_bound_predicate) => {
733734
self.print_where_bound_predicate(where_bound_predicate);

compiler/rustc_builtin_macros/src/cfg_eval.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ fn flat_map_annotatable(
8080
vis.visit_crate(&mut krate);
8181
Some(Annotatable::Crate(krate))
8282
}
83+
Annotatable::WherePredicate(predicate) => {
84+
vis.flat_map_where_predicate(predicate).pop().map(Annotatable::WherePredicate)
85+
}
8386
}
8487
}
8588

@@ -114,6 +117,7 @@ fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
114117
Annotatable::FieldDef(field) => CfgFinder.visit_field_def(field),
115118
Annotatable::Variant(variant) => CfgFinder.visit_variant(variant),
116119
Annotatable::Crate(krate) => CfgFinder.visit_crate(krate),
120+
Annotatable::WherePredicate(predicate) => CfgFinder.visit_where_predicate(predicate),
117121
};
118122
res.is_break()
119123
}

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,12 @@ impl<'a> TraitDef<'a> {
761761
};
762762

763763
let kind = ast::WherePredicateKind::BoundPredicate(predicate);
764-
let predicate =
765-
ast::WherePredicate { kind, id: ast::DUMMY_NODE_ID, span: self.span };
764+
let predicate = ast::WherePredicate {
765+
attrs: ThinVec::new(),
766+
kind,
767+
id: ast::DUMMY_NODE_ID,
768+
span: self.span,
769+
};
766770
where_clause.predicates.push(predicate);
767771
}
768772
}

compiler/rustc_expand/src/base.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub enum Annotatable {
5353
FieldDef(ast::FieldDef),
5454
Variant(ast::Variant),
5555
Crate(ast::Crate),
56+
WherePredicate(ast::WherePredicate),
5657
}
5758

5859
impl Annotatable {
@@ -71,6 +72,7 @@ impl Annotatable {
7172
Annotatable::FieldDef(sf) => sf.span,
7273
Annotatable::Variant(v) => v.span,
7374
Annotatable::Crate(c) => c.spans.inner_span,
75+
Annotatable::WherePredicate(wp) => wp.span,
7476
}
7577
}
7678

@@ -89,6 +91,7 @@ impl Annotatable {
8991
Annotatable::FieldDef(sf) => sf.visit_attrs(f),
9092
Annotatable::Variant(v) => v.visit_attrs(f),
9193
Annotatable::Crate(c) => c.visit_attrs(f),
94+
Annotatable::WherePredicate(wp) => wp.visit_attrs(f),
9295
}
9396
}
9497

@@ -107,6 +110,7 @@ impl Annotatable {
107110
Annotatable::FieldDef(sf) => visitor.visit_field_def(sf),
108111
Annotatable::Variant(v) => visitor.visit_variant(v),
109112
Annotatable::Crate(c) => visitor.visit_crate(c),
113+
Annotatable::WherePredicate(wp) => visitor.visit_where_predicate(wp),
110114
}
111115
}
112116

@@ -127,7 +131,8 @@ impl Annotatable {
127131
| Annotatable::Param(..)
128132
| Annotatable::FieldDef(..)
129133
| Annotatable::Variant(..)
130-
| Annotatable::Crate(..) => panic!("unexpected annotatable"),
134+
| Annotatable::Crate(..)
135+
| Annotatable::WherePredicate(..) => panic!("unexpected annotatable"),
131136
}
132137
}
133138

@@ -228,6 +233,13 @@ impl Annotatable {
228233
_ => panic!("expected krate"),
229234
}
230235
}
236+
237+
pub fn expect_where_predicate(self) -> ast::WherePredicate {
238+
match self {
239+
Annotatable::WherePredicate(wp) => wp,
240+
_ => panic!("expected where predicate"),
241+
}
242+
}
231243
}
232244

233245
/// Result of an expansion that may need to be retried.
@@ -449,6 +461,10 @@ pub trait MacResult {
449461
// Fn-like macros cannot produce a crate.
450462
unreachable!()
451463
}
464+
465+
fn make_where_predicates(self: Box<Self>) -> Option<SmallVec<[ast::WherePredicate; 1]>> {
466+
None
467+
}
452468
}
453469

454470
macro_rules! make_MacEager {

0 commit comments

Comments
 (0)