Skip to content

Commit f3192a5

Browse files
committed
Implment #[cfg] in where clauses
1 parent 75eff9a commit f3192a5

File tree

62 files changed

+1563
-401
lines changed

Some content is hidden

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

62 files changed

+1563
-401
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,33 @@ impl Default for WhereClause {
422422

423423
/// A single predicate in a where-clause.
424424
#[derive(Clone, Encodable, Decodable, Debug)]
425-
pub enum WherePredicate {
425+
pub struct WherePredicate {
426+
pub attrs: AttrVec,
427+
pub kind: WherePredicateKind,
428+
pub id: NodeId,
429+
pub span: Span,
430+
}
431+
432+
impl WherePredicate {
433+
pub fn with_kind(&self, kind: WherePredicateKind) -> WherePredicate {
434+
self.map_kind(|_| kind)
435+
}
436+
pub fn map_kind(
437+
&self,
438+
f: impl FnOnce(&WherePredicateKind) -> WherePredicateKind,
439+
) -> WherePredicate {
440+
WherePredicate {
441+
attrs: self.attrs.clone(),
442+
kind: f(&self.kind),
443+
id: DUMMY_NODE_ID,
444+
span: self.span,
445+
}
446+
}
447+
}
448+
449+
/// Predicate kind in where-clause.
450+
#[derive(Clone, Encodable, Decodable, Debug)]
451+
pub enum WherePredicateKind {
426452
/// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
427453
BoundPredicate(WhereBoundPredicate),
428454
/// A lifetime predicate (e.g., `'a: 'b + 'c`).
@@ -431,12 +457,12 @@ pub enum WherePredicate {
431457
EqPredicate(WhereEqPredicate),
432458
}
433459

434-
impl WherePredicate {
460+
impl WherePredicateKind {
435461
pub fn span(&self) -> Span {
436462
match self {
437-
WherePredicate::BoundPredicate(p) => p.span,
438-
WherePredicate::RegionPredicate(p) => p.span,
439-
WherePredicate::EqPredicate(p) => p.span,
463+
WherePredicateKind::BoundPredicate(p) => p.span,
464+
WherePredicateKind::RegionPredicate(p) => p.span,
465+
WherePredicateKind::EqPredicate(p) => p.span,
440466
}
441467
}
442468
}

Diff for: compiler/rustc_ast/src/ast_traits.rs

+13-2
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

Diff for: compiler/rustc_ast/src/mut_visit.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,15 @@ pub trait MutVisitor: Sized {
286286
walk_where_clause(self, where_clause);
287287
}
288288

289-
fn visit_where_predicate(&mut self, where_predicate: &mut WherePredicate) {
290-
walk_where_predicate(self, where_predicate);
289+
fn flat_map_where_predicate(
290+
&mut self,
291+
where_predicate: WherePredicate,
292+
) -> SmallVec<[WherePredicate; 1]> {
293+
walk_flat_map_where_predicate(self, where_predicate)
294+
}
295+
296+
fn visit_where_predicate_kind(&mut self, kind: &mut WherePredicateKind) {
297+
walk_where_predicate_kind(self, kind)
291298
}
292299

293300
fn visit_vis(&mut self, vis: &mut Visibility) {
@@ -987,26 +994,38 @@ fn walk_ty_alias_where_clauses<T: MutVisitor>(vis: &mut T, tawcs: &mut TyAliasWh
987994

988995
fn walk_where_clause<T: MutVisitor>(vis: &mut T, wc: &mut WhereClause) {
989996
let WhereClause { has_where_token: _, predicates, span } = wc;
990-
visit_thin_vec(predicates, |predicate| vis.visit_where_predicate(predicate));
997+
predicates.flat_map_in_place(|predicate| vis.flat_map_where_predicate(predicate));
998+
vis.visit_span(span);
999+
}
1000+
1001+
pub fn walk_flat_map_where_predicate<T: MutVisitor>(
1002+
vis: &mut T,
1003+
mut pred: WherePredicate,
1004+
) -> SmallVec<[WherePredicate; 1]> {
1005+
let WherePredicate { ref mut attrs, ref mut kind, ref mut id, ref mut span } = pred;
1006+
vis.visit_id(id);
1007+
visit_attrs(vis, attrs);
1008+
vis.visit_where_predicate_kind(kind);
9911009
vis.visit_span(span);
1010+
smallvec![pred]
9921011
}
9931012

994-
fn walk_where_predicate<T: MutVisitor>(vis: &mut T, pred: &mut WherePredicate) {
995-
match pred {
996-
WherePredicate::BoundPredicate(bp) => {
1013+
pub fn walk_where_predicate_kind<T: MutVisitor>(vis: &mut T, kind: &mut WherePredicateKind) {
1014+
match kind {
1015+
WherePredicateKind::BoundPredicate(bp) => {
9971016
let WhereBoundPredicate { span, bound_generic_params, bounded_ty, bounds } = bp;
9981017
bound_generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
9991018
vis.visit_ty(bounded_ty);
10001019
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::Bound));
10011020
vis.visit_span(span);
10021021
}
1003-
WherePredicate::RegionPredicate(rp) => {
1022+
WherePredicateKind::RegionPredicate(rp) => {
10041023
let WhereRegionPredicate { span, lifetime, bounds } = rp;
10051024
vis.visit_lifetime(lifetime);
10061025
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::Bound));
10071026
vis.visit_span(span);
10081027
}
1009-
WherePredicate::EqPredicate(ep) => {
1028+
WherePredicateKind::EqPredicate(ep) => {
10101029
let WhereEqPredicate { span, lhs_ty, rhs_ty } = ep;
10111030
vis.visit_ty(lhs_ty);
10121031
vis.visit_ty(rhs_ty);

Diff for: compiler/rustc_ast/src/visit.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ pub trait Visitor<'ast>: Sized {
188188
fn visit_where_predicate(&mut self, p: &'ast WherePredicate) -> Self::Result {
189189
walk_where_predicate(self, p)
190190
}
191+
fn visit_where_predicate_kind(&mut self, k: &'ast WherePredicateKind) -> Self::Result {
192+
walk_where_predicate_kind(self, k)
193+
}
191194
fn visit_fn(&mut self, fk: FnKind<'ast>, _: Span, _: NodeId) -> Self::Result {
192195
walk_fn(self, fk)
193196
}
@@ -786,8 +789,17 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(
786789
visitor: &mut V,
787790
predicate: &'a WherePredicate,
788791
) -> V::Result {
789-
match predicate {
790-
WherePredicate::BoundPredicate(WhereBoundPredicate {
792+
let WherePredicate { attrs, kind, id: _, span: _ } = predicate;
793+
walk_list!(visitor, visit_attribute, attrs);
794+
visitor.visit_where_predicate_kind(kind)
795+
}
796+
797+
pub fn walk_where_predicate_kind<'a, V: Visitor<'a>>(
798+
visitor: &mut V,
799+
kind: &'a WherePredicateKind,
800+
) -> V::Result {
801+
match kind {
802+
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
791803
bounded_ty,
792804
bounds,
793805
bound_generic_params,
@@ -797,11 +809,11 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(
797809
try_visit!(visitor.visit_ty(bounded_ty));
798810
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
799811
}
800-
WherePredicate::RegionPredicate(WhereRegionPredicate { lifetime, bounds, span: _ }) => {
812+
WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds, span: _ }) => {
801813
try_visit!(visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound));
802814
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
803815
}
804-
WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span: _ }) => {
816+
WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span: _ }) => {
805817
try_visit!(visitor.visit_ty(lhs_ty));
806818
try_visit!(visitor.visit_ty(rhs_ty));
807819
}

Diff for: compiler/rustc_ast_lowering/src/index.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
381381
}
382382

383383
fn visit_where_predicate(&mut self, predicate: &'hir WherePredicate<'hir>) {
384-
match predicate {
385-
WherePredicate::BoundPredicate(pred) => {
386-
self.insert(pred.span, pred.hir_id, Node::WhereBoundPredicate(pred));
387-
self.with_parent(pred.hir_id, |this| {
388-
intravisit::walk_where_predicate(this, predicate)
389-
})
390-
}
391-
_ => intravisit::walk_where_predicate(self, predicate),
392-
}
384+
self.insert(predicate.span, predicate.hir_id, Node::WherePredicate(predicate));
385+
self.with_parent(predicate.hir_id, |this| {
386+
intravisit::walk_where_predicate(this, predicate)
387+
});
393388
}
394389

395390
fn visit_array_length(&mut self, len: &'hir ArrayLen<'hir>) {

Diff for: compiler/rustc_ast_lowering/src/item.rs

+33-27
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14001400
// keep track of the Span info. Now, `<dyn HirTyLowerer>::add_implicit_sized_bound`
14011401
// checks both param bounds and where clauses for `?Sized`.
14021402
for pred in &generics.where_clause.predicates {
1403-
let WherePredicate::BoundPredicate(bound_pred) = pred else {
1403+
let WherePredicateKind::BoundPredicate(ref bound_pred) = pred.kind else {
14041404
continue;
14051405
};
14061406
let compute_is_param = || {
@@ -1538,8 +1538,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
15381538
});
15391539
let span = self.lower_span(span);
15401540

1541-
match kind {
1542-
GenericParamKind::Const { .. } => None,
1541+
let kind = match kind {
1542+
GenericParamKind::Const { .. } => return None,
15431543
GenericParamKind::Type { .. } => {
15441544
let def_id = self.local_def_id(id).to_def_id();
15451545
let hir_id = self.next_id();
@@ -1554,38 +1554,39 @@ impl<'hir> LoweringContext<'_, 'hir> {
15541554
let ty_id = self.next_id();
15551555
let bounded_ty =
15561556
self.ty_path(ty_id, param_span, hir::QPath::Resolved(None, ty_path));
1557-
Some(hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1558-
hir_id: self.next_id(),
1557+
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
15591558
bounded_ty: self.arena.alloc(bounded_ty),
15601559
bounds,
15611560
span,
15621561
bound_generic_params: &[],
15631562
origin,
1564-
}))
1563+
})
15651564
}
15661565
GenericParamKind::Lifetime => {
15671566
let ident = self.lower_ident(ident);
15681567
let lt_id = self.next_node_id();
15691568
let lifetime = self.new_named_lifetime(id, lt_id, ident);
1570-
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
1569+
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
15711570
lifetime,
15721571
span,
15731572
bounds,
15741573
in_where_clause: false,
1575-
}))
1574+
})
15761575
}
1577-
}
1576+
};
1577+
Some(hir::WherePredicate { hir_id: self.next_id(), kind: self.arena.alloc(kind), span })
15781578
}
15791579

15801580
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
1581-
match pred {
1582-
WherePredicate::BoundPredicate(WhereBoundPredicate {
1581+
let hir_id = self.lower_node_id(pred.id);
1582+
self.lower_attrs(hir_id, &pred.attrs);
1583+
let kind = match &pred.kind {
1584+
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
15831585
bound_generic_params,
15841586
bounded_ty,
15851587
bounds,
15861588
span,
1587-
}) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1588-
hir_id: self.next_id(),
1589+
}) => hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
15891590
bound_generic_params: self
15901591
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
15911592
bounded_ty: self
@@ -1597,26 +1598,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
15971598
span: self.lower_span(*span),
15981599
origin: PredicateOrigin::WhereClause,
15991600
}),
1600-
WherePredicate::RegionPredicate(WhereRegionPredicate { lifetime, bounds, span }) => {
1601-
hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
1602-
span: self.lower_span(*span),
1603-
lifetime: self.lower_lifetime(lifetime),
1604-
bounds: self.lower_param_bounds(
1605-
bounds,
1606-
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1607-
),
1608-
in_where_clause: true,
1609-
})
1610-
}
1611-
WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span }) => {
1612-
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
1601+
WherePredicateKind::RegionPredicate(WhereRegionPredicate {
1602+
lifetime,
1603+
bounds,
1604+
span,
1605+
}) => hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
1606+
span: self.lower_span(*span),
1607+
lifetime: self.lower_lifetime(lifetime),
1608+
bounds: self.lower_param_bounds(
1609+
bounds,
1610+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1611+
),
1612+
in_where_clause: true,
1613+
}),
1614+
WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span }) => {
1615+
hir::WherePredicateKind::EqPredicate(hir::WhereEqPredicate {
16131616
lhs_ty: self
16141617
.lower_ty(lhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
16151618
rhs_ty: self
16161619
.lower_ty(rhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
16171620
span: self.lower_span(*span),
16181621
})
16191622
}
1620-
}
1623+
};
1624+
let kind = self.arena.alloc(kind);
1625+
let span = self.lower_span(pred.span);
1626+
hir::WherePredicate { hir_id, kind, span }
16211627
}
16221628
}

Diff for: compiler/rustc_ast_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ ast_passes_body_in_extern = incorrect `{$kind}` inside `extern` block
3636
3737
ast_passes_bound_in_context = bounds on `type`s in {$ctx} have no effect
3838
39+
ast_passes_cfg_attribute_in_where_is_unstable =
40+
`#[cfg]` attribute in `where` clause is unstable
41+
3942
ast_passes_const_and_c_variadic = functions cannot be both `const` and C-variadic
4043
.const = `const` because of this
4144
.variadic = C-variadic because of this

0 commit comments

Comments
 (0)