Skip to content

Commit 2aa303c

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

File tree

58 files changed

+1352
-311
lines changed

Some content is hidden

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

58 files changed

+1352
-311
lines changed

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

+25-5
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,27 @@ 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+
}
430+
431+
impl WherePredicate {
432+
pub fn with_kind(&self, kind: WherePredicateKind) -> WherePredicate {
433+
self.map_kind(|_| kind)
434+
}
435+
pub fn map_kind(
436+
&self,
437+
f: impl FnOnce(&WherePredicateKind) -> WherePredicateKind,
438+
) -> WherePredicate {
439+
WherePredicate { attrs: self.attrs.clone(), kind: f(&self.kind), id: DUMMY_NODE_ID }
440+
}
441+
}
442+
443+
/// Predicate kind in where-clause.
444+
#[derive(Clone, Encodable, Decodable, Debug)]
445+
pub enum WherePredicateKind {
426446
/// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
427447
BoundPredicate(WhereBoundPredicate),
428448
/// A lifetime predicate (e.g., `'a: 'b + 'c`).
@@ -431,12 +451,12 @@ pub enum WherePredicate {
431451
EqPredicate(WhereEqPredicate),
432452
}
433453

434-
impl WherePredicate {
454+
impl WherePredicateKind {
435455
pub fn span(&self) -> Span {
436456
match self {
437-
WherePredicate::BoundPredicate(p) => p.span,
438-
WherePredicate::RegionPredicate(p) => p.span,
439-
WherePredicate::EqPredicate(p) => p.span,
457+
WherePredicateKind::BoundPredicate(p) => p.span,
458+
WherePredicateKind::RegionPredicate(p) => p.span,
459+
WherePredicateKind::EqPredicate(p) => p.span,
440460
}
441461
}
442462
}

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,11 @@ 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 filter_map_where_predicate(
290+
&mut self,
291+
where_predicate: WherePredicate,
292+
) -> Option<WherePredicate> {
293+
walk_filter_map_where_predicate(self, where_predicate)
291294
}
292295

293296
fn visit_vis(&mut self, vis: &mut Visibility) {
@@ -987,32 +990,39 @@ fn walk_ty_alias_where_clauses<T: MutVisitor>(vis: &mut T, tawcs: &mut TyAliasWh
987990

988991
fn walk_where_clause<T: MutVisitor>(vis: &mut T, wc: &mut WhereClause) {
989992
let WhereClause { has_where_token: _, predicates, span } = wc;
990-
visit_thin_vec(predicates, |predicate| vis.visit_where_predicate(predicate));
993+
predicates.flat_map_in_place(|predicate| vis.filter_map_where_predicate(predicate));
991994
vis.visit_span(span);
992995
}
993996

994-
fn walk_where_predicate<T: MutVisitor>(vis: &mut T, pred: &mut WherePredicate) {
995-
match pred {
996-
WherePredicate::BoundPredicate(bp) => {
997+
pub fn walk_filter_map_where_predicate<T: MutVisitor>(
998+
vis: &mut T,
999+
mut pred: WherePredicate,
1000+
) -> Option<WherePredicate> {
1001+
let WherePredicate { ref mut attrs, ref mut kind, ref mut id } = pred;
1002+
vis.visit_id(id);
1003+
visit_attrs(vis, attrs);
1004+
match kind {
1005+
WherePredicateKind::BoundPredicate(bp) => {
9971006
let WhereBoundPredicate { span, bound_generic_params, bounded_ty, bounds } = bp;
9981007
bound_generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
9991008
vis.visit_ty(bounded_ty);
10001009
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::Bound));
10011010
vis.visit_span(span);
10021011
}
1003-
WherePredicate::RegionPredicate(rp) => {
1012+
WherePredicateKind::RegionPredicate(rp) => {
10041013
let WhereRegionPredicate { span, lifetime, bounds } = rp;
10051014
vis.visit_lifetime(lifetime);
10061015
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::Bound));
10071016
vis.visit_span(span);
10081017
}
1009-
WherePredicate::EqPredicate(ep) => {
1018+
WherePredicateKind::EqPredicate(ep) => {
10101019
let WhereEqPredicate { span, lhs_ty, rhs_ty } = ep;
10111020
vis.visit_ty(lhs_ty);
10121021
vis.visit_ty(rhs_ty);
10131022
vis.visit_span(span);
10141023
}
10151024
}
1025+
Some(pred)
10161026
}
10171027

10181028
fn walk_variant_data<T: MutVisitor>(vis: &mut T, vdata: &mut VariantData) {

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,10 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(
786786
visitor: &mut V,
787787
predicate: &'a WherePredicate,
788788
) -> V::Result {
789-
match predicate {
790-
WherePredicate::BoundPredicate(WhereBoundPredicate {
789+
let WherePredicate { attrs, kind, id: _ } = predicate;
790+
walk_list!(visitor, visit_attribute, attrs);
791+
match kind {
792+
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
791793
bounded_ty,
792794
bounds,
793795
bound_generic_params,
@@ -797,11 +799,11 @@ pub fn walk_where_predicate<'a, V: Visitor<'a>>(
797799
try_visit!(visitor.visit_ty(bounded_ty));
798800
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
799801
}
800-
WherePredicate::RegionPredicate(WhereRegionPredicate { lifetime, bounds, span: _ }) => {
802+
WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds, span: _ }) => {
801803
try_visit!(visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound));
802804
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
803805
}
804-
WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span: _ }) => {
806+
WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span: _ }) => {
805807
try_visit!(visitor.visit_ty(lhs_ty));
806808
try_visit!(visitor.visit_ty(rhs_ty));
807809
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,9 @@ 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) => {
384+
self.insert(predicate.span(), predicate.hir_id, Node::WherePredicate(predicate));
385+
match predicate.kind {
386+
WherePredicateKind::BoundPredicate(pred) => {
386387
self.insert(pred.span, pred.hir_id, Node::WhereBoundPredicate(pred));
387388
self.with_parent(pred.hir_id, |this| {
388389
intravisit::walk_where_predicate(this, predicate)

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

+32-25
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,37 +1554,40 @@ 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 {
1557+
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
15581558
hir_id: self.next_id(),
15591559
bounded_ty: self.arena.alloc(bounded_ty),
15601560
bounds,
15611561
span,
15621562
bound_generic_params: &[],
15631563
origin,
1564-
}))
1564+
})
15651565
}
15661566
GenericParamKind::Lifetime => {
15671567
let ident = self.lower_ident(ident);
15681568
let lt_id = self.next_node_id();
15691569
let lifetime = self.new_named_lifetime(id, lt_id, ident);
1570-
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
1570+
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
15711571
lifetime,
15721572
span,
15731573
bounds,
15741574
in_where_clause: false,
1575-
}))
1575+
})
15761576
}
1577-
}
1577+
};
1578+
Some(hir::WherePredicate { hir_id: self.next_id(), kind: self.arena.alloc(kind) })
15781579
}
15791580

15801581
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
1581-
match pred {
1582-
WherePredicate::BoundPredicate(WhereBoundPredicate {
1582+
let hir_id = self.lower_node_id(pred.id);
1583+
self.lower_attrs(hir_id, &pred.attrs);
1584+
let kind = match &pred.kind {
1585+
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
15831586
bound_generic_params,
15841587
bounded_ty,
15851588
bounds,
15861589
span,
1587-
}) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1590+
}) => hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
15881591
hir_id: self.next_id(),
15891592
bound_generic_params: self
15901593
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
@@ -1597,26 +1600,30 @@ impl<'hir> LoweringContext<'_, 'hir> {
15971600
span: self.lower_span(*span),
15981601
origin: PredicateOrigin::WhereClause,
15991602
}),
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 {
1603+
WherePredicateKind::RegionPredicate(WhereRegionPredicate {
1604+
lifetime,
1605+
bounds,
1606+
span,
1607+
}) => hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
1608+
span: self.lower_span(*span),
1609+
lifetime: self.lower_lifetime(lifetime),
1610+
bounds: self.lower_param_bounds(
1611+
bounds,
1612+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1613+
),
1614+
in_where_clause: true,
1615+
}),
1616+
WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span }) => {
1617+
hir::WherePredicateKind::EqPredicate(hir::WhereEqPredicate {
16131618
lhs_ty: self
16141619
.lower_ty(lhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
16151620
rhs_ty: self
16161621
.lower_ty(rhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
16171622
span: self.lower_span(*span),
16181623
})
16191624
}
1620-
}
1625+
};
1626+
let kind = self.arena.alloc(kind);
1627+
hir::WherePredicate { hir_id, kind }
16211628
}
16221629
}

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1202,14 +1202,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12021202
validate_generic_param_order(self.dcx(), &generics.params, generics.span);
12031203

12041204
for predicate in &generics.where_clause.predicates {
1205-
if let WherePredicate::EqPredicate(predicate) = predicate {
1205+
if let WherePredicateKind::EqPredicate(ref predicate) = predicate.kind {
12061206
deny_equality_constraints(self, predicate, generics);
12071207
}
12081208
}
12091209
walk_list!(self, visit_generic_param, &generics.params);
12101210
for predicate in &generics.where_clause.predicates {
1211-
match predicate {
1212-
WherePredicate::BoundPredicate(bound_pred) => {
1211+
match predicate.kind {
1212+
WherePredicateKind::BoundPredicate(ref bound_pred) => {
12131213
// This is slightly complicated. Our representation for poly-trait-refs contains a single
12141214
// binder and thus we only allow a single level of quantification. However,
12151215
// the syntax of Rust permits quantification in two places in where clauses,
@@ -1593,18 +1593,18 @@ fn deny_equality_constraints(
15931593
let mut preds = generics.where_clause.predicates.iter().peekable();
15941594
// Find the predicate that shouldn't have been in the where bound list.
15951595
while let Some(pred) = preds.next() {
1596-
if let WherePredicate::EqPredicate(pred) = pred
1596+
if let WherePredicateKind::EqPredicate(ref pred) = pred.kind
15971597
&& pred.span == predicate.span
15981598
{
15991599
if let Some(next) = preds.peek() {
16001600
// This is the first predicate, remove the trailing comma as well.
1601-
span = span.with_hi(next.span().lo());
1601+
span = span.with_hi(next.kind.span().lo());
16021602
} else if let Some(prev) = prev {
16031603
// Remove the previous comma as well.
16041604
span = span.with_lo(prev.hi());
16051605
}
16061606
}
1607-
prev = Some(pred.span());
1607+
prev = Some(pred.kind.span());
16081608
}
16091609
span
16101610
};
@@ -1621,8 +1621,8 @@ fn deny_equality_constraints(
16211621
if let TyKind::Path(None, full_path) = &predicate.lhs_ty.kind {
16221622
// Given `A: Foo, Foo::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
16231623
for bounds in generics.params.iter().map(|p| &p.bounds).chain(
1624-
generics.where_clause.predicates.iter().filter_map(|pred| match pred {
1625-
WherePredicate::BoundPredicate(p) => Some(&p.bounds),
1624+
generics.where_clause.predicates.iter().filter_map(|pred| match pred.kind {
1625+
WherePredicateKind::BoundPredicate(ref p) => Some(&p.bounds),
16261626
_ => None,
16271627
}),
16281628
) {
@@ -1645,8 +1645,8 @@ fn deny_equality_constraints(
16451645
// Given `A: Foo, A::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
16461646
if let [potential_param, potential_assoc] = &full_path.segments[..] {
16471647
for (ident, bounds) in generics.params.iter().map(|p| (p.ident, &p.bounds)).chain(
1648-
generics.where_clause.predicates.iter().filter_map(|pred| match pred {
1649-
WherePredicate::BoundPredicate(p)
1648+
generics.where_clause.predicates.iter().filter_map(|pred| match pred.kind {
1649+
WherePredicateKind::BoundPredicate(ref p)
16501650
if let ast::TyKind::Path(None, path) = &p.bounded_ty.kind
16511651
&& let [segment] = &path.segments[..] =>
16521652
{

Diff for: compiler/rustc_ast_passes/src/feature_gate.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
344344

345345
fn visit_generics(&mut self, g: &'a ast::Generics) {
346346
for predicate in &g.where_clause.predicates {
347-
match predicate {
348-
ast::WherePredicate::BoundPredicate(bound_pred) => {
347+
visit::walk_list!(self, visit_attribute, &predicate.attrs);
348+
match predicate.kind {
349+
ast::WherePredicateKind::BoundPredicate(ref bound_pred) => {
349350
// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
350351
self.check_late_bound_lifetime_defs(&bound_pred.bound_generic_params);
351352
}

0 commit comments

Comments
 (0)