Skip to content

Commit d71d19e

Browse files
committed
Refactor where predicates, and reserve for attributes support
1 parent 328b759 commit d71d19e

File tree

44 files changed

+395
-409
lines changed

Some content is hidden

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

44 files changed

+395
-409
lines changed

compiler/rustc_ast/src/ast.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,15 @@ impl Default for WhereClause {
428428

429429
/// A single predicate in a where-clause.
430430
#[derive(Clone, Encodable, Decodable, Debug)]
431-
pub enum WherePredicate {
431+
pub struct WherePredicate {
432+
pub kind: WherePredicateKind,
433+
pub id: NodeId,
434+
pub span: Span,
435+
}
436+
437+
/// Predicate kind in where-clause.
438+
#[derive(Clone, Encodable, Decodable, Debug)]
439+
pub enum WherePredicateKind {
432440
/// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
433441
BoundPredicate(WhereBoundPredicate),
434442
/// A lifetime predicate (e.g., `'a: 'b + 'c`).
@@ -437,22 +445,11 @@ pub enum WherePredicate {
437445
EqPredicate(WhereEqPredicate),
438446
}
439447

440-
impl WherePredicate {
441-
pub fn span(&self) -> Span {
442-
match self {
443-
WherePredicate::BoundPredicate(p) => p.span,
444-
WherePredicate::RegionPredicate(p) => p.span,
445-
WherePredicate::EqPredicate(p) => p.span,
446-
}
447-
}
448-
}
449-
450448
/// A type bound.
451449
///
452450
/// E.g., `for<'c> Foo: Send + Clone + 'c`.
453451
#[derive(Clone, Encodable, Decodable, Debug)]
454452
pub struct WhereBoundPredicate {
455-
pub span: Span,
456453
/// Any generics from a `for` binding.
457454
pub bound_generic_params: ThinVec<GenericParam>,
458455
/// The type being bounded.
@@ -466,7 +463,6 @@ pub struct WhereBoundPredicate {
466463
/// E.g., `'a: 'b + 'c`.
467464
#[derive(Clone, Encodable, Decodable, Debug)]
468465
pub struct WhereRegionPredicate {
469-
pub span: Span,
470466
pub lifetime: Lifetime,
471467
pub bounds: GenericBounds,
472468
}
@@ -476,7 +472,6 @@ pub struct WhereRegionPredicate {
476472
/// E.g., `T = int`.
477473
#[derive(Clone, Encodable, Decodable, Debug)]
478474
pub struct WhereEqPredicate {
479-
pub span: Span,
480475
pub lhs_ty: P<Ty>,
481476
pub rhs_ty: P<Ty>,
482477
}

compiler/rustc_ast/src/mut_visit.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ pub trait MutVisitor: Sized {
287287
}
288288

289289
fn visit_where_predicate(&mut self, where_predicate: &mut WherePredicate) {
290-
walk_where_predicate(self, where_predicate);
290+
walk_where_predicate(self, where_predicate)
291+
}
292+
293+
fn visit_where_predicate_kind(&mut self, kind: &mut WherePredicateKind) {
294+
walk_where_predicate_kind(self, kind)
291295
}
292296

293297
fn visit_vis(&mut self, vis: &mut Visibility) {
@@ -991,26 +995,30 @@ fn walk_where_clause<T: MutVisitor>(vis: &mut T, wc: &mut WhereClause) {
991995
vis.visit_span(span);
992996
}
993997

994-
fn walk_where_predicate<T: MutVisitor>(vis: &mut T, pred: &mut WherePredicate) {
995-
match pred {
996-
WherePredicate::BoundPredicate(bp) => {
997-
let WhereBoundPredicate { span, bound_generic_params, bounded_ty, bounds } = bp;
998+
pub fn walk_where_predicate<T: MutVisitor>(vis: &mut T, pred: &mut WherePredicate) {
999+
let WherePredicate { kind, id, span } = pred;
1000+
vis.visit_id(id);
1001+
vis.visit_where_predicate_kind(kind);
1002+
vis.visit_span(span);
1003+
}
1004+
1005+
pub fn walk_where_predicate_kind<T: MutVisitor>(vis: &mut T, kind: &mut WherePredicateKind) {
1006+
match kind {
1007+
WherePredicateKind::BoundPredicate(bp) => {
1008+
let WhereBoundPredicate { bound_generic_params, bounded_ty, bounds } = bp;
9981009
bound_generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
9991010
vis.visit_ty(bounded_ty);
10001011
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::Bound));
1001-
vis.visit_span(span);
10021012
}
1003-
WherePredicate::RegionPredicate(rp) => {
1004-
let WhereRegionPredicate { span, lifetime, bounds } = rp;
1013+
WherePredicateKind::RegionPredicate(rp) => {
1014+
let WhereRegionPredicate { lifetime, bounds } = rp;
10051015
vis.visit_lifetime(lifetime);
10061016
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::Bound));
1007-
vis.visit_span(span);
10081017
}
1009-
WherePredicate::EqPredicate(ep) => {
1010-
let WhereEqPredicate { span, lhs_ty, rhs_ty } = ep;
1018+
WherePredicateKind::EqPredicate(ep) => {
1019+
let WhereEqPredicate { lhs_ty, rhs_ty } = ep;
10111020
vis.visit_ty(lhs_ty);
10121021
vis.visit_ty(rhs_ty);
1013-
vis.visit_span(span);
10141022
}
10151023
}
10161024
}

compiler/rustc_ast/src/visit.rs

+15-5
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,22 +789,29 @@ 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 { kind, id: _, span: _ } = predicate;
793+
visitor.visit_where_predicate_kind(kind)
794+
}
795+
796+
pub fn walk_where_predicate_kind<'a, V: Visitor<'a>>(
797+
visitor: &mut V,
798+
kind: &'a WherePredicateKind,
799+
) -> V::Result {
800+
match kind {
801+
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
791802
bounded_ty,
792803
bounds,
793804
bound_generic_params,
794-
span: _,
795805
}) => {
796806
walk_list!(visitor, visit_generic_param, bound_generic_params);
797807
try_visit!(visitor.visit_ty(bounded_ty));
798808
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
799809
}
800-
WherePredicate::RegionPredicate(WhereRegionPredicate { lifetime, bounds, span: _ }) => {
810+
WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds }) => {
801811
try_visit!(visitor.visit_lifetime(lifetime, LifetimeCtxt::Bound));
802812
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
803813
}
804-
WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span: _ }) => {
814+
WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty }) => {
805815
try_visit!(visitor.visit_ty(lhs_ty));
806816
try_visit!(visitor.visit_ty(rhs_ty));
807817
}

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>) {

compiler/rustc_ast_lowering/src/item.rs

+21-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(bound_pred) = &pred.kind else {
14041404
continue;
14051405
};
14061406
let compute_is_param = || {
@@ -1537,9 +1537,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
15371537
}
15381538
});
15391539
let span = self.lower_span(span);
1540-
1541-
match kind {
1542-
GenericParamKind::Const { .. } => None,
1540+
let hir_id = self.next_id();
1541+
let kind = self.arena.alloc(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,36 @@ 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,
1561-
span,
15621560
bound_generic_params: &[],
15631561
origin,
1564-
}))
1562+
})
15651563
}
15661564
GenericParamKind::Lifetime => {
15671565
let ident = self.lower_ident(ident);
15681566
let lt_id = self.next_node_id();
15691567
let lifetime = self.new_named_lifetime(id, lt_id, ident);
1570-
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
1568+
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
15711569
lifetime,
1572-
span,
15731570
bounds,
15741571
in_where_clause: false,
1575-
}))
1572+
})
15761573
}
1577-
}
1574+
});
1575+
Some(hir::WherePredicate { hir_id, span, kind })
15781576
}
15791577

15801578
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
1581-
match pred {
1582-
WherePredicate::BoundPredicate(WhereBoundPredicate {
1579+
let hir_id = self.lower_node_id(pred.id);
1580+
let span = self.lower_span(pred.span);
1581+
let kind = self.arena.alloc(match &pred.kind {
1582+
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
15831583
bound_generic_params,
15841584
bounded_ty,
15851585
bounds,
1586-
span,
1587-
}) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1588-
hir_id: self.next_id(),
1586+
}) => hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
15891587
bound_generic_params: self
15901588
.lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
15911589
bounded_ty: self
@@ -1594,12 +1592,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
15941592
bounds,
15951593
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
15961594
),
1597-
span: self.lower_span(*span),
15981595
origin: PredicateOrigin::WhereClause,
15991596
}),
1600-
WherePredicate::RegionPredicate(WhereRegionPredicate { lifetime, bounds, span }) => {
1601-
hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
1602-
span: self.lower_span(*span),
1597+
WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds }) => {
1598+
hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
16031599
lifetime: self.lower_lifetime(lifetime),
16041600
bounds: self.lower_param_bounds(
16051601
bounds,
@@ -1608,15 +1604,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
16081604
in_where_clause: true,
16091605
})
16101606
}
1611-
WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span }) => {
1612-
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
1607+
WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty }) => {
1608+
hir::WherePredicateKind::EqPredicate(hir::WhereEqPredicate {
16131609
lhs_ty: self
16141610
.lower_ty(lhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
16151611
rhs_ty: self
16161612
.lower_ty(rhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1617-
span: self.lower_span(*span),
16181613
})
16191614
}
1620-
}
1615+
});
1616+
hir::WherePredicate { hir_id, span, kind }
16211617
}
16221618
}

compiler/rustc_ast_passes/src/ast_validation.rs

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

12031203
for predicate in &generics.where_clause.predicates {
1204-
if let WherePredicate::EqPredicate(predicate) = predicate {
1205-
deny_equality_constraints(self, predicate, generics);
1204+
let span = predicate.span;
1205+
if let WherePredicateKind::EqPredicate(predicate) = &predicate.kind {
1206+
deny_equality_constraints(self, predicate, span, generics);
12061207
}
12071208
}
12081209
walk_list!(self, visit_generic_param, &generics.params);
12091210
for predicate in &generics.where_clause.predicates {
1210-
match predicate {
1211-
WherePredicate::BoundPredicate(bound_pred) => {
1211+
match &predicate.kind {
1212+
WherePredicateKind::BoundPredicate(bound_pred) => {
12121213
// This is slightly complicated. Our representation for poly-trait-refs contains a single
12131214
// binder and thus we only allow a single level of quantification. However,
12141215
// the syntax of Rust permits quantification in two places in where clauses,
@@ -1511,9 +1512,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15111512
fn deny_equality_constraints(
15121513
this: &AstValidator<'_>,
15131514
predicate: &WhereEqPredicate,
1515+
predicate_span: Span,
15141516
generics: &Generics,
15151517
) {
1516-
let mut err = errors::EqualityInWhere { span: predicate.span, assoc: None, assoc2: None };
1518+
let mut err = errors::EqualityInWhere { span: predicate_span, assoc: None, assoc2: None };
15171519

15181520
// Given `<A as Foo>::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
15191521
if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind
@@ -1557,7 +1559,7 @@ fn deny_equality_constraints(
15571559
}
15581560
}
15591561
err.assoc = Some(errors::AssociatedSuggestion {
1560-
span: predicate.span,
1562+
span: predicate_span,
15611563
ident: *ident,
15621564
param: param.ident,
15631565
path: pprust::path_to_string(&assoc_path),
@@ -1587,23 +1589,23 @@ fn deny_equality_constraints(
15871589
// We're removing th eonly where bound left, remove the whole thing.
15881590
generics.where_clause.span
15891591
} else {
1590-
let mut span = predicate.span;
1592+
let mut span = predicate_span;
15911593
let mut prev: Option<Span> = None;
15921594
let mut preds = generics.where_clause.predicates.iter().peekable();
15931595
// Find the predicate that shouldn't have been in the where bound list.
15941596
while let Some(pred) = preds.next() {
1595-
if let WherePredicate::EqPredicate(pred) = pred
1596-
&& pred.span == predicate.span
1597+
if let WherePredicateKind::EqPredicate(_) = pred.kind
1598+
&& pred.span == predicate_span
15971599
{
15981600
if let Some(next) = preds.peek() {
15991601
// This is the first predicate, remove the trailing comma as well.
1600-
span = span.with_hi(next.span().lo());
1602+
span = span.with_hi(next.span.lo());
16011603
} else if let Some(prev) = prev {
16021604
// Remove the previous comma as well.
16031605
span = span.with_lo(prev.hi());
16041606
}
16051607
}
1606-
prev = Some(pred.span());
1608+
prev = Some(pred.span);
16071609
}
16081610
span
16091611
};
@@ -1620,8 +1622,8 @@ fn deny_equality_constraints(
16201622
if let TyKind::Path(None, full_path) = &predicate.lhs_ty.kind {
16211623
// Given `A: Foo, Foo::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
16221624
for bounds in generics.params.iter().map(|p| &p.bounds).chain(
1623-
generics.where_clause.predicates.iter().filter_map(|pred| match pred {
1624-
WherePredicate::BoundPredicate(p) => Some(&p.bounds),
1625+
generics.where_clause.predicates.iter().filter_map(|pred| match &pred.kind {
1626+
WherePredicateKind::BoundPredicate(p) => Some(&p.bounds),
16251627
_ => None,
16261628
}),
16271629
) {
@@ -1644,8 +1646,8 @@ fn deny_equality_constraints(
16441646
// Given `A: Foo, A::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
16451647
if let [potential_param, potential_assoc] = &full_path.segments[..] {
16461648
for (ident, bounds) in generics.params.iter().map(|p| (p.ident, &p.bounds)).chain(
1647-
generics.where_clause.predicates.iter().filter_map(|pred| match pred {
1648-
WherePredicate::BoundPredicate(p)
1649+
generics.where_clause.predicates.iter().filter_map(|pred| match &pred.kind {
1650+
WherePredicateKind::BoundPredicate(p)
16491651
if let ast::TyKind::Path(None, path) = &p.bounded_ty.kind
16501652
&& let [segment] = &path.segments[..] =>
16511653
{

compiler/rustc_ast_passes/src/feature_gate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ 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+
match &predicate.kind {
348+
ast::WherePredicateKind::BoundPredicate(bound_pred) => {
349349
// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
350350
self.check_late_bound_lifetime_defs(&bound_pred.bound_generic_params);
351351
}

0 commit comments

Comments
 (0)