Skip to content

Commit 26d4104

Browse files
committed
Implment #[cfg] in where clauses
1 parent 4e5fec2 commit 26d4104

File tree

34 files changed

+1588
-90
lines changed

34 files changed

+1588
-90
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ impl WhereClause {
415415
/// A single predicate in a where-clause.
416416
#[derive(Clone, Encodable, Decodable, Debug)]
417417
pub struct WherePredicate {
418+
pub attrs: AttrVec,
418419
pub kind: WherePredicateKind,
419420
pub id: NodeId,
420421
pub span: Span,

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: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,11 @@ pub trait MutVisitor: Sized {
330330
walk_where_clause(self, where_clause);
331331
}
332332

333-
fn visit_where_predicate(&mut self, where_predicate: &mut WherePredicate) {
334-
walk_where_predicate(self, where_predicate)
333+
fn flat_map_where_predicate(
334+
&mut self,
335+
where_predicate: WherePredicate,
336+
) -> SmallVec<[WherePredicate; 1]> {
337+
walk_flat_map_where_predicate(self, where_predicate)
335338
}
336339

337340
fn visit_where_predicate_kind(&mut self, kind: &mut WherePredicateKind) {
@@ -1066,15 +1069,20 @@ fn walk_ty_alias_where_clauses<T: MutVisitor>(vis: &mut T, tawcs: &mut TyAliasWh
10661069

10671070
fn walk_where_clause<T: MutVisitor>(vis: &mut T, wc: &mut WhereClause) {
10681071
let WhereClause { has_where_token: _, predicates, span } = wc;
1069-
visit_thin_vec(predicates, |predicate| vis.visit_where_predicate(predicate));
1072+
predicates.flat_map_in_place(|predicate| vis.flat_map_where_predicate(predicate));
10701073
vis.visit_span(span);
10711074
}
10721075

1073-
pub fn walk_where_predicate<T: MutVisitor>(vis: &mut T, pred: &mut WherePredicate) {
1074-
let WherePredicate { kind, id, span } = pred;
1076+
pub fn walk_flat_map_where_predicate<T: MutVisitor>(
1077+
vis: &mut T,
1078+
mut pred: WherePredicate,
1079+
) -> SmallVec<[WherePredicate; 1]> {
1080+
let WherePredicate { attrs, kind, id, span } = &mut pred;
10751081
vis.visit_id(id);
1082+
visit_attrs(vis, attrs);
10761083
vis.visit_where_predicate_kind(kind);
10771084
vis.visit_span(span);
1085+
smallvec![pred]
10781086
}
10791087

10801088
pub fn walk_where_predicate_kind<T: MutVisitor>(vis: &mut T, kind: &mut WherePredicateKind) {

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,8 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(
804804
visitor: &mut V,
805805
predicate: &'a WherePredicate,
806806
) -> V::Result {
807-
let WherePredicate { kind, id: _, span: _ } = predicate;
807+
let WherePredicate { attrs, kind, id: _, span: _ } = predicate;
808+
walk_list!(visitor, visit_attribute, attrs);
808809
visitor.visit_where_predicate_kind(kind)
809810
}
810811

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15941594
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
15951595
let hir_id = self.lower_node_id(pred.id);
15961596
let span = self.lower_span(pred.span);
1597+
self.lower_attrs(hir_id, &pred.attrs);
15971598
let kind = self.arena.alloc(match &pred.kind {
15981599
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
15991600
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
@@ -560,6 +560,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
560560
gate_all!(pin_ergonomics, "pinned reference syntax is experimental");
561561
gate_all!(unsafe_fields, "`unsafe` fields are experimental");
562562
gate_all!(unsafe_binders, "unsafe binder types are experimental");
563+
gate_all!(cfg_attribute_in_where, "`#[cfg]` attribute in `where` clause is unstable");
563564

564565
if !visitor.features.never_patterns() {
565566
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,7 +726,8 @@ impl<'a> State<'a> {
726726
}
727727

728728
pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
729-
let ast::WherePredicate { kind, id: _, span: _ } = predicate;
729+
let ast::WherePredicate { attrs, kind, id: _, span: _ } = predicate;
730+
self.print_outer_attributes(attrs);
730731
match kind {
731732
ast::WherePredicateKind::BoundPredicate(where_bound_predicate) => {
732733
self.print_where_bound_predicate(where_bound_predicate);

compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,15 @@ pub(crate) fn expand_deriving_coerce_pointee(
264264
to_ty: &s_ty,
265265
rewritten: false,
266266
};
267-
let mut predicate = ast::WherePredicate {
268-
kind: ast::WherePredicateKind::BoundPredicate(bound.clone()),
269-
span: predicate.span,
270-
id: ast::DUMMY_NODE_ID,
271-
};
272-
substitution.visit_where_predicate(&mut predicate);
267+
let mut kind = ast::WherePredicateKind::BoundPredicate(bound.clone());
268+
substitution.visit_where_predicate_kind(&mut kind);
273269
if substitution.rewritten {
270+
let predicate = ast::WherePredicate {
271+
attrs: predicate.attrs.clone(),
272+
kind,
273+
span: predicate.span,
274+
id: ast::DUMMY_NODE_ID,
275+
};
274276
impl_generics.where_clause.predicates.push(predicate);
275277
}
276278
}
@@ -352,8 +354,8 @@ impl<'a> ast::mut_visit::MutVisitor for TypeSubstitution<'a> {
352354
}
353355
}
354356

355-
fn visit_where_predicate(&mut self, where_predicate: &mut ast::WherePredicate) {
356-
match &mut where_predicate.kind {
357+
fn visit_where_predicate_kind(&mut self, kind: &mut ast::WherePredicateKind) {
358+
match kind {
357359
rustc_ast::WherePredicateKind::BoundPredicate(bound) => {
358360
bound
359361
.bound_generic_params

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ impl<'a> TraitDef<'a> {
690690
// and similarly for where clauses
691691
where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
692692
ast::WherePredicate {
693+
attrs: clause.attrs.clone(),
693694
kind: clause.kind.clone(),
694695
id: ast::DUMMY_NODE_ID,
695696
span: clause.span.with_ctxt(ctxt),
@@ -747,8 +748,12 @@ impl<'a> TraitDef<'a> {
747748
};
748749

749750
let kind = ast::WherePredicateKind::BoundPredicate(predicate);
750-
let predicate =
751-
ast::WherePredicate { kind, id: ast::DUMMY_NODE_ID, span: self.span };
751+
let predicate = ast::WherePredicate {
752+
attrs: ThinVec::new(),
753+
kind,
754+
id: ast::DUMMY_NODE_ID,
755+
span: self.span,
756+
};
752757
where_clause.predicates.push(predicate);
753758
}
754759
}

compiler/rustc_expand/src/base.rs

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

5758
impl Annotatable {
@@ -70,6 +71,7 @@ impl Annotatable {
7071
Annotatable::FieldDef(sf) => sf.span,
7172
Annotatable::Variant(v) => v.span,
7273
Annotatable::Crate(c) => c.spans.inner_span,
74+
Annotatable::WherePredicate(wp) => wp.span,
7375
}
7476
}
7577

@@ -88,6 +90,7 @@ impl Annotatable {
8890
Annotatable::FieldDef(sf) => sf.visit_attrs(f),
8991
Annotatable::Variant(v) => v.visit_attrs(f),
9092
Annotatable::Crate(c) => c.visit_attrs(f),
93+
Annotatable::WherePredicate(wp) => wp.visit_attrs(f),
9194
}
9295
}
9396

@@ -106,6 +109,7 @@ impl Annotatable {
106109
Annotatable::FieldDef(sf) => visitor.visit_field_def(sf),
107110
Annotatable::Variant(v) => visitor.visit_variant(v),
108111
Annotatable::Crate(c) => visitor.visit_crate(c),
112+
Annotatable::WherePredicate(wp) => visitor.visit_where_predicate(wp),
109113
}
110114
}
111115

@@ -126,7 +130,8 @@ impl Annotatable {
126130
| Annotatable::Param(..)
127131
| Annotatable::FieldDef(..)
128132
| Annotatable::Variant(..)
129-
| Annotatable::Crate(..) => panic!("unexpected annotatable"),
133+
| Annotatable::Crate(..)
134+
| Annotatable::WherePredicate(..) => panic!("unexpected annotatable"),
130135
}
131136
}
132137

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

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

453469
macro_rules! make_MacEager {

0 commit comments

Comments
 (0)