Skip to content

Commit 05b29f9

Browse files
committed
Inline WhereClause into Generics.
1 parent 71b4e2d commit 05b29f9

File tree

24 files changed

+98
-139
lines changed

24 files changed

+98
-139
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,9 +1338,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
13381338
}
13391339
}
13401340

1341+
let predicates = self.arena.alloc_from_iter(
1342+
generics
1343+
.where_clause
1344+
.predicates
1345+
.iter()
1346+
.map(|predicate| self.lower_where_predicate(predicate)),
1347+
);
1348+
13411349
GenericsCtor {
13421350
params: self.lower_generic_params_mut(&generics.params, itctx).collect(),
1343-
where_clause: self.lower_where_clause(&generics.where_clause),
1351+
predicates,
1352+
where_clause_span: self.lower_span(generics.where_clause.span),
13441353
span: self.lower_span(generics.span),
13451354
}
13461355
}
@@ -1354,15 +1363,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
13541363
generics_ctor.into_generics(self.arena)
13551364
}
13561365

1357-
fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause<'hir> {
1358-
hir::WhereClause {
1359-
predicates: self.arena.alloc_from_iter(
1360-
wc.predicates.iter().map(|predicate| self.lower_where_predicate(predicate)),
1361-
),
1362-
span: self.lower_span(wc.span),
1363-
}
1364-
}
1365-
13661366
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
13671367
match *pred {
13681368
WherePredicate::BoundPredicate(WhereBoundPredicate {
@@ -1414,15 +1414,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
14141414
/// Helper struct for delayed construction of Generics.
14151415
pub(super) struct GenericsCtor<'hir> {
14161416
pub(super) params: SmallVec<[hir::GenericParam<'hir>; 4]>,
1417-
where_clause: hir::WhereClause<'hir>,
1417+
predicates: &'hir [hir::WherePredicate<'hir>],
1418+
where_clause_span: Span,
14181419
span: Span,
14191420
}
14201421

14211422
impl<'hir> GenericsCtor<'hir> {
14221423
pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> &'hir hir::Generics<'hir> {
14231424
arena.alloc(hir::Generics {
14241425
params: arena.alloc_from_iter(self.params),
1425-
where_clause: self.where_clause,
1426+
predicates: self.predicates,
1427+
where_clause_span: self.where_clause_span,
14261428
span: self.span,
14271429
})
14281430
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13851385
let opaque_ty_item = hir::OpaqueTy {
13861386
generics: self.arena.alloc(hir::Generics {
13871387
params: lifetime_defs,
1388-
where_clause: hir::WhereClause { predicates: &[], span: lctx.lower_span(span) },
1388+
predicates: &[],
1389+
where_clause_span: lctx.lower_span(span),
13891390
span: lctx.lower_span(span),
13901391
}),
13911392
bounds: hir_bounds,
@@ -1717,7 +1718,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17171718
let opaque_ty_item = hir::OpaqueTy {
17181719
generics: this.arena.alloc(hir::Generics {
17191720
params: generic_params,
1720-
where_clause: hir::WhereClause { predicates: &[], span: this.lower_span(span) },
1721+
predicates: &[],
1722+
where_clause_span: this.lower_span(span),
17211723
span: this.lower_span(span),
17221724
}),
17231725
bounds: arena_vec![this; future_bound],

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -567,17 +567,15 @@ pub struct GenericParamCount {
567567
#[derive(Debug, HashStable_Generic)]
568568
pub struct Generics<'hir> {
569569
pub params: &'hir [GenericParam<'hir>],
570-
pub where_clause: WhereClause<'hir>,
570+
pub predicates: &'hir [WherePredicate<'hir>],
571+
pub where_clause_span: Span,
571572
pub span: Span,
572573
}
573574

574575
impl<'hir> Generics<'hir> {
575576
pub const fn empty() -> &'hir Generics<'hir> {
576-
const NOPE: Generics<'_> = Generics {
577-
params: &[],
578-
where_clause: WhereClause { predicates: &[], span: DUMMY_SP },
579-
span: DUMMY_SP,
580-
};
577+
const NOPE: Generics<'_> =
578+
Generics { params: &[], predicates: &[], where_clause_span: DUMMY_SP, span: DUMMY_SP };
581579
&NOPE
582580
}
583581

@@ -597,30 +595,20 @@ impl<'hir> Generics<'hir> {
597595
self.params.iter().map(|p| p.span).collect::<Vec<Span>>().into()
598596
}
599597
}
600-
}
601598

602-
/// A where-clause in a definition.
603-
#[derive(Debug, HashStable_Generic)]
604-
pub struct WhereClause<'hir> {
605-
pub predicates: &'hir [WherePredicate<'hir>],
606-
// Only valid if predicates aren't empty.
607-
pub span: Span,
608-
}
609-
610-
impl WhereClause<'_> {
611-
pub fn span(&self) -> Option<Span> {
612-
if self.predicates.is_empty() { None } else { Some(self.span) }
599+
pub fn where_clause_span(&self) -> Option<Span> {
600+
if self.predicates.is_empty() { None } else { Some(self.where_clause_span) }
613601
}
614602

615-
/// The `WhereClause` under normal circumstances points at either the predicates or the empty
603+
/// The `where_span` under normal circumstances points at either the predicates or the empty
616604
/// space where the `where` clause should be. Only of use for diagnostic suggestions.
617605
pub fn span_for_predicates_or_empty_place(&self) -> Span {
618-
self.span
606+
self.where_clause_span
619607
}
620608

621609
/// `Span` where further predicates would be suggested, accounting for trailing commas, like
622610
/// in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
623-
pub fn tail_span_for_suggestion(&self) -> Span {
611+
pub fn tail_span_for_predicate_suggestion(&self) -> Span {
624612
let end = self.span_for_predicates_or_empty_place().shrink_to_hi();
625613
self.predicates.last().map_or(end, |p| p.span()).shrink_to_hi().to(end)
626614
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v Ano
908908

909909
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
910910
walk_list!(visitor, visit_generic_param, generics.params);
911-
walk_list!(visitor, visit_where_predicate, generics.where_clause.predicates);
911+
walk_list!(visitor, visit_where_predicate, generics.predicates);
912912
}
913913

914914
pub fn walk_where_predicate<'v, V: Visitor<'v>>(

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl<'a> State<'a> {
445445
if let Some(bounds) = bounds {
446446
self.print_bounds(":", bounds);
447447
}
448-
self.print_where_clause(&generics.where_clause);
448+
self.print_where_clause(generics);
449449
if let Some(ty) = ty {
450450
self.space();
451451
self.word_space("=");
@@ -465,7 +465,7 @@ impl<'a> State<'a> {
465465
self.print_generic_params(&generics.params);
466466
self.end(); // end the inner ibox
467467

468-
self.print_where_clause(&generics.where_clause);
468+
self.print_where_clause(generics);
469469
self.space();
470470
inner(self);
471471
self.word(";");
@@ -650,7 +650,7 @@ impl<'a> State<'a> {
650650
}
651651

652652
self.print_type(&self_ty);
653-
self.print_where_clause(&generics.where_clause);
653+
self.print_where_clause(generics);
654654

655655
self.space();
656656
self.bopen();
@@ -678,7 +678,7 @@ impl<'a> State<'a> {
678678
}
679679
}
680680
self.print_bounds(":", real_bounds);
681-
self.print_where_clause(&generics.where_clause);
681+
self.print_where_clause(generics);
682682
self.word(" ");
683683
self.bopen();
684684
for trait_item in trait_items {
@@ -703,7 +703,7 @@ impl<'a> State<'a> {
703703
}
704704
self.nbsp();
705705
self.print_bounds("=", real_bounds);
706-
self.print_where_clause(&generics.where_clause);
706+
self.print_where_clause(generics);
707707
self.word(";");
708708
self.end(); // end inner head-block
709709
self.end(); // end outer head-block
@@ -739,7 +739,7 @@ impl<'a> State<'a> {
739739
self.head("enum");
740740
self.print_name(name);
741741
self.print_generic_params(&generics.params);
742-
self.print_where_clause(&generics.where_clause);
742+
self.print_where_clause(generics);
743743
self.space();
744744
self.print_variants(&enum_definition.variants, span)
745745
}
@@ -787,15 +787,15 @@ impl<'a> State<'a> {
787787
});
788788
self.pclose();
789789
}
790-
self.print_where_clause(&generics.where_clause);
790+
self.print_where_clause(generics);
791791
if print_finalizer {
792792
self.word(";");
793793
}
794794
self.end();
795795
self.end() // close the outer-box
796796
}
797797
hir::VariantData::Struct(..) => {
798-
self.print_where_clause(&generics.where_clause);
798+
self.print_where_clause(generics);
799799
self.nbsp();
800800
self.bopen();
801801
self.hardbreak_if_not_bol();
@@ -1995,7 +1995,7 @@ impl<'a> State<'a> {
19951995
self.pclose();
19961996

19971997
self.print_fn_output(decl);
1998-
self.print_where_clause(&generics.where_clause)
1998+
self.print_where_clause(generics)
19991999
}
20002000

20012001
fn print_closure_params(&mut self, decl: &hir::FnDecl<'_>, body_id: hir::BodyId) {
@@ -2133,15 +2133,15 @@ impl<'a> State<'a> {
21332133
self.print_ident(lifetime.name.ident())
21342134
}
21352135

2136-
pub fn print_where_clause(&mut self, where_clause: &hir::WhereClause<'_>) {
2137-
if where_clause.predicates.is_empty() {
2136+
pub fn print_where_clause(&mut self, generics: &hir::Generics<'_>) {
2137+
if generics.predicates.is_empty() {
21382138
return;
21392139
}
21402140

21412141
self.space();
21422142
self.word_space("where");
21432143

2144-
for (i, predicate) in where_clause.predicates.iter().enumerate() {
2144+
for (i, predicate) in generics.predicates.iter().enumerate() {
21452145
if i != 0 {
21462146
self.word_space(",");
21472147
}
@@ -2236,11 +2236,7 @@ impl<'a> State<'a> {
22362236
) {
22372237
self.ibox(INDENT_UNIT);
22382238
self.print_formal_generic_params(generic_params);
2239-
let generics = hir::Generics {
2240-
params: &[],
2241-
where_clause: hir::WhereClause { predicates: &[], span: rustc_span::DUMMY_SP },
2242-
span: rustc_span::DUMMY_SP,
2243-
};
2239+
let generics = hir::Generics::empty();
22442240
self.print_fn(
22452241
decl,
22462242
hir::FnHeader {

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,11 +2543,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
25432543
let pred = format!("{}: {}", bound_kind, sub);
25442544
let suggestion = format!(
25452545
"{} {}",
2546-
if !generics.where_clause.predicates.is_empty() { "," } else { " where" },
2546+
if !generics.predicates.is_empty() { "," } else { " where" },
25472547
pred,
25482548
);
25492549
err.span_suggestion(
2550-
generics.where_clause.tail_span_for_suggestion(),
2550+
generics.tail_span_for_predicate_suggestion(),
25512551
"consider adding a where clause",
25522552
suggestion,
25532553
Applicability::MaybeIncorrect,

compiler/rustc_infer/src/infer/error_reporting/note.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
372372
.hir()
373373
.get_generics(impl_item_def_id)
374374
.unwrap()
375-
.where_clause
376-
.tail_span_for_suggestion();
375+
.where_clause_span
376+
.shrink_to_hi();
377377

378378
let suggestion = format!(
379379
"{} {}",

compiler/rustc_lint/src/builtin.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,20 +1519,19 @@ impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds {
15191519
}
15201520
let mut suggested_changing_assoc_types = false;
15211521
// There must not be a where clause
1522-
if !type_alias_generics.where_clause.predicates.is_empty() {
1522+
if !type_alias_generics.predicates.is_empty() {
15231523
cx.lint(
15241524
TYPE_ALIAS_BOUNDS,
15251525
|lint| {
15261526
let mut err = lint.build("where clauses are not enforced in type aliases");
15271527
let spans: Vec<_> = type_alias_generics
1528-
.where_clause
15291528
.predicates
15301529
.iter()
15311530
.map(|pred| pred.span())
15321531
.collect();
15331532
err.set_span(spans);
15341533
err.span_suggestion(
1535-
type_alias_generics.where_clause.span_for_predicates_or_empty_place(),
1534+
type_alias_generics.span_for_predicates_or_empty_place(),
15361535
"the clause will not be checked when the type alias is used, and should be removed",
15371536
String::new(),
15381537
Applicability::MachineApplicable,
@@ -2245,8 +2244,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22452244

22462245
let mut where_lint_spans = Vec::new();
22472246
let mut dropped_predicate_count = 0;
2248-
let num_predicates = hir_generics.where_clause.predicates.len();
2249-
for (i, where_predicate) in hir_generics.where_clause.predicates.iter().enumerate() {
2247+
let num_predicates = hir_generics.predicates.len();
2248+
for (i, where_predicate) in hir_generics.predicates.iter().enumerate() {
22502249
let (relevant_lifetimes, bounds, span) = match where_predicate {
22512250
hir::WherePredicate::RegionPredicate(predicate) => {
22522251
if let Some(Region::EarlyBound(index, ..)) =
@@ -2303,7 +2302,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
23032302
// If all the bounds on a predicate were inferable and there are
23042303
// further predicates, we want to eat the trailing comma.
23052304
if drop_predicate && i + 1 < num_predicates {
2306-
let next_predicate_span = hir_generics.where_clause.predicates[i + 1].span();
2305+
let next_predicate_span = hir_generics.predicates[i + 1].span();
23072306
where_lint_spans.push(span.to(next_predicate_span.shrink_to_lo()));
23082307
} else {
23092308
where_lint_spans.extend(self.consolidate_outlives_bound_spans(
@@ -2318,8 +2317,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
23182317
// (including the `where`)
23192318
if num_predicates > 0 && dropped_predicate_count == num_predicates {
23202319
let where_span = hir_generics
2321-
.where_clause
2322-
.span()
2320+
.where_clause_span()
23232321
.expect("span of (nonempty) where clause should exist");
23242322
// Extend the where clause back to the closing `>` of the
23252323
// generics, except for tuple struct, which have the `where`

0 commit comments

Comments
 (0)