Skip to content

Commit e848eca

Browse files
Handle empty where-clause better
1 parent 7fc9e95 commit e848eca

File tree

23 files changed

+78
-59
lines changed

23 files changed

+78
-59
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
13761376

13771377
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> =
13781378
self.lower_generic_params_mut(&generics.params).collect();
1379-
let has_where_clause = !generics.where_clause.predicates.is_empty();
1379+
let has_where_clause_predicates = !generics.where_clause.predicates.is_empty();
1380+
let has_where_clause_token = generics.where_clause.has_where_token;
13801381
let where_clause_span = self.lower_span(generics.where_clause.span);
13811382
let span = self.lower_span(generics.span);
13821383
let res = f(self);
@@ -1394,7 +1395,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
13941395
let lowered_generics = self.arena.alloc(hir::Generics {
13951396
params: self.arena.alloc_from_iter(params),
13961397
predicates: self.arena.alloc_from_iter(predicates),
1397-
has_where_clause,
1398+
has_where_clause_predicates,
1399+
has_where_clause_token,
13981400
where_clause_span,
13991401
span,
14001402
});

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13321332
generics: self.arena.alloc(hir::Generics {
13331333
params: lifetime_defs,
13341334
predicates: &[],
1335-
has_where_clause: false,
1335+
has_where_clause_predicates: false,
1336+
has_where_clause_token: false,
13361337
where_clause_span: lctx.lower_span(span),
13371338
span: lctx.lower_span(span),
13381339
}),
@@ -1654,7 +1655,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16541655
generics: this.arena.alloc(hir::Generics {
16551656
params: generic_params,
16561657
predicates: &[],
1657-
has_where_clause: false,
1658+
has_where_clause_predicates: false,
1659+
has_where_clause_token: false,
16581660
where_clause_span: this.lower_span(span),
16591661
span: this.lower_span(span),
16601662
}),

compiler/rustc_hir/src/hir.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ pub struct GenericParamCount {
535535
pub struct Generics<'hir> {
536536
pub params: &'hir [GenericParam<'hir>],
537537
pub predicates: &'hir [WherePredicate<'hir>],
538-
pub has_where_clause: bool,
538+
pub has_where_clause_predicates: bool,
539+
pub has_where_clause_token: bool,
539540
pub where_clause_span: Span,
540541
pub span: Span,
541542
}
@@ -545,7 +546,8 @@ impl<'hir> Generics<'hir> {
545546
const NOPE: Generics<'_> = Generics {
546547
params: &[],
547548
predicates: &[],
548-
has_where_clause: false,
549+
has_where_clause_predicates: false,
550+
has_where_clause_token: false,
549551
where_clause_span: DUMMY_SP,
550552
span: DUMMY_SP,
551553
};
@@ -585,17 +587,11 @@ impl<'hir> Generics<'hir> {
585587
if self.predicates.is_empty() { None } else { Some(self.where_clause_span) }
586588
}
587589

588-
/// The `where_span` under normal circumstances points at either the predicates or the empty
589-
/// space where the `where` clause should be. Only of use for diagnostic suggestions.
590-
pub fn span_for_predicates_or_empty_place(&self) -> Span {
591-
self.where_clause_span
592-
}
593-
594590
/// `Span` where further predicates would be suggested, accounting for trailing commas, like
595591
/// in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
596592
pub fn tail_span_for_predicate_suggestion(&self) -> Span {
597-
let end = self.span_for_predicates_or_empty_place().shrink_to_hi();
598-
if self.has_where_clause {
593+
let end = self.where_clause_span.shrink_to_hi();
594+
if self.has_where_clause_predicates {
599595
self.predicates
600596
.iter()
601597
.filter(|p| p.in_where_clause())
@@ -608,6 +604,16 @@ impl<'hir> Generics<'hir> {
608604
}
609605
}
610606

607+
pub fn add_where_or_trailing_comma(&self) -> &'static str {
608+
if self.has_where_clause_predicates {
609+
","
610+
} else if self.has_where_clause_token {
611+
""
612+
} else {
613+
" where"
614+
}
615+
}
616+
611617
pub fn bounds_for_param(
612618
&self,
613619
param_def_id: LocalDefId,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2547,7 +2547,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
25472547
let pred = format!("{}: {}", bound_kind, sub);
25482548
let suggestion = format!(
25492549
"{} {}",
2550-
if !generics.predicates.is_empty() { "," } else { " where" },
2550+
generics.add_where_or_trailing_comma(),
25512551
pred,
25522552
);
25532553
err.span_suggestion(

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
367367
.collect();
368368

369369
if !clauses.is_empty() {
370-
let where_clause_span = self
371-
.tcx
372-
.hir()
373-
.get_generics(impl_item_def_id)
374-
.unwrap()
375-
.where_clause_span
376-
.shrink_to_hi();
370+
let generics = self.tcx.hir().get_generics(impl_item_def_id).unwrap();
371+
let where_clause_span = generics.tail_span_for_predicate_suggestion();
377372

378373
let suggestion = format!(
379374
"{} {}",
380-
if !impl_predicates.is_empty() { "," } else { " where" },
375+
generics.add_where_or_trailing_comma(),
381376
clauses.join(", "),
382377
);
383378
err.span_suggestion(

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2264,7 +2264,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22642264

22652265
// If all predicates are inferable, drop the entire clause
22662266
// (including the `where`)
2267-
if hir_generics.has_where_clause && dropped_predicate_count == num_predicates {
2267+
if hir_generics.has_where_clause_predicates && dropped_predicate_count == num_predicates
2268+
{
22682269
let where_span = hir_generics
22692270
.where_clause_span()
22702271
.expect("span of (nonempty) where clause should exist");

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,14 @@ pub fn suggest_arbitrary_trait_bound(
9292
_ => {}
9393
}
9494
// Suggest a where clause bound for a non-type parameter.
95-
let (action, prefix) = if generics.has_where_clause {
96-
("extending the", ", ")
97-
} else {
98-
("introducing a", " where ")
99-
};
10095
err.span_suggestion_verbose(
10196
generics.tail_span_for_predicate_suggestion(),
10297
&format!(
103-
"consider {} `where` bound, but there might be an alternative better way to express \
98+
"consider {} `where` clause, but there might be an alternative better way to express \
10499
this requirement",
105-
action,
100+
if generics.has_where_clause_token { "extending the" } else { "introducing a" },
106101
),
107-
format!("{}{}: {}", prefix, param_name, constraint),
102+
format!("{} {}: {}", generics.add_where_or_trailing_comma(), param_name, constraint),
108103
Applicability::MaybeIncorrect,
109104
);
110105
true
@@ -257,7 +252,7 @@ pub fn suggest_constraining_type_params<'a>(
257252
continue;
258253
}
259254

260-
if generics.has_where_clause {
255+
if generics.has_where_clause_predicates {
261256
// This part is a bit tricky, because using the `where` clause user can
262257
// provide zero, one or many bounds for the same type parameter, so we
263258
// have following cases to consider:

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub trait InferCtxtExt<'tcx> {
322322
fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
323323
(
324324
generics.tail_span_for_predicate_suggestion(),
325-
format!("{} {}", if generics.has_where_clause { "," } else { " where" }, pred,),
325+
format!("{} {}", generics.add_where_or_trailing_comma(), pred),
326326
)
327327
}
328328

@@ -337,15 +337,16 @@ fn suggest_restriction<'tcx>(
337337
fn_sig: Option<&hir::FnSig<'_>>,
338338
projection: Option<&ty::ProjectionTy<'_>>,
339339
trait_pred: ty::PolyTraitPredicate<'tcx>,
340-
super_traits: Option<(&Ident, &hir::GenericBounds<'_>)>,
341-
) {
342340
// When we are dealing with a trait, `super_traits` will be `Some`:
343341
// Given `trait T: A + B + C {}`
344342
// - ^^^^^^^^^ GenericBounds
345343
// |
346344
// &Ident
347-
let span = generics.span_for_predicates_or_empty_place();
348-
if span.from_expansion() || span.desugaring_kind().is_some() {
345+
super_traits: Option<(&Ident, &hir::GenericBounds<'_>)>,
346+
) {
347+
if generics.where_clause_span.from_expansion()
348+
|| generics.where_clause_span.desugaring_kind().is_some()
349+
{
349350
return;
350351
}
351352
// Given `fn foo(t: impl Trait)` where `Trait` requires assoc type `A`...

compiler/rustc_typeck/src/check/method/suggest.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
542542
};
543543
if let Some(hir::Node::Item(hir::Item { kind, .. })) = node {
544544
if let Some(g) = kind.generics() {
545-
let key = match g.predicates {
546-
[.., pred] => (pred.span().shrink_to_hi(), false),
547-
[] => (g.span_for_predicates_or_empty_place(), true),
548-
};
545+
let key = (
546+
g.tail_span_for_predicate_suggestion(),
547+
g.add_where_or_trailing_comma(),
548+
);
549549
type_params
550550
.entry(key)
551551
.or_insert_with(FxHashSet::default)
@@ -809,7 +809,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
809809
.enumerate()
810810
.collect::<Vec<(usize, String)>>();
811811

812-
for ((span, empty_where), obligations) in type_params.into_iter() {
812+
for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
813813
restrict_type_params = true;
814814
// #74886: Sort here so that the output is always the same.
815815
let mut obligations = obligations.into_iter().collect::<Vec<_>>();
@@ -823,7 +823,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
823823
),
824824
format!(
825825
"{} {}",
826-
if empty_where { " where" } else { "," },
826+
add_where_or_comma,
827827
obligations.join(", ")
828828
),
829829
Applicability::MaybeIncorrect,

compiler/rustc_typeck/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
421421

422422
let suggestion = format!(
423423
"{} {}",
424-
if !gat_item_hir.generics.predicates.is_empty() { "," } else { " where" },
424+
gat_item_hir.generics.add_where_or_trailing_comma(),
425425
unsatisfied_bounds.join(", "),
426426
);
427427
err.span_suggestion(

0 commit comments

Comments
 (0)