Skip to content

Commit 9c47afe

Browse files
Handle empty where-clause better
1 parent 8506b7d commit 9c47afe

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
@@ -1377,7 +1377,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
13771377

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

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13151315
generics: self.arena.alloc(hir::Generics {
13161316
params: lifetime_defs,
13171317
predicates: &[],
1318-
has_where_clause: false,
1318+
has_where_clause_predicates: false,
1319+
has_where_clause_token: false,
13191320
where_clause_span: lctx.lower_span(span),
13201321
span: lctx.lower_span(span),
13211322
}),
@@ -1637,7 +1638,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16371638
generics: this.arena.alloc(hir::Generics {
16381639
params: generic_params,
16391640
predicates: &[],
1640-
has_where_clause: false,
1641+
has_where_clause_predicates: false,
1642+
has_where_clause_token: false,
16411643
where_clause_span: this.lower_span(span),
16421644
span: this.lower_span(span),
16431645
}),

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
@@ -2511,7 +2511,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
25112511
let pred = format!("{}: {}", bound_kind, sub);
25122512
let suggestion = format!(
25132513
"{} {}",
2514-
if !generics.predicates.is_empty() { "," } else { " where" },
2514+
generics.add_where_or_trailing_comma(),
25152515
pred,
25162516
);
25172517
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
@@ -2293,7 +2293,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22932293

22942294
// If all predicates are inferable, drop the entire clause
22952295
// (including the `where`)
2296-
if hir_generics.has_where_clause && dropped_predicate_count == num_predicates {
2296+
if hir_generics.has_where_clause_predicates && dropped_predicate_count == num_predicates
2297+
{
22972298
let where_span = hir_generics
22982299
.where_clause_span()
22992300
.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
@@ -324,7 +324,7 @@ pub trait InferCtxtExt<'tcx> {
324324
fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
325325
(
326326
generics.tail_span_for_predicate_suggestion(),
327-
format!("{} {}", if generics.has_where_clause { "," } else { " where" }, pred,),
327+
format!("{} {}", generics.add_where_or_trailing_comma(), pred),
328328
)
329329
}
330330

@@ -339,15 +339,16 @@ fn suggest_restriction<'tcx>(
339339
fn_sig: Option<&hir::FnSig<'_>>,
340340
projection: Option<&ty::ProjectionTy<'_>>,
341341
trait_pred: ty::PolyTraitPredicate<'tcx>,
342-
super_traits: Option<(&Ident, &hir::GenericBounds<'_>)>,
343-
) {
344342
// When we are dealing with a trait, `super_traits` will be `Some`:
345343
// Given `trait T: A + B + C {}`
346344
// - ^^^^^^^^^ GenericBounds
347345
// |
348346
// &Ident
349-
let span = generics.span_for_predicates_or_empty_place();
350-
if span.from_expansion() || span.desugaring_kind().is_some() {
347+
super_traits: Option<(&Ident, &hir::GenericBounds<'_>)>,
348+
) {
349+
if generics.where_clause_span.from_expansion()
350+
|| generics.where_clause_span.desugaring_kind().is_some()
351+
{
351352
return;
352353
}
353354
// 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
@@ -538,10 +538,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
538538
};
539539
if let Some(hir::Node::Item(hir::Item { kind, .. })) = node {
540540
if let Some(g) = kind.generics() {
541-
let key = match g.predicates {
542-
[.., pred] => (pred.span().shrink_to_hi(), false),
543-
[] => (g.span_for_predicates_or_empty_place(), true),
544-
};
541+
let key = (
542+
g.tail_span_for_predicate_suggestion(),
543+
g.add_where_or_trailing_comma(),
544+
);
545545
type_params
546546
.entry(key)
547547
.or_insert_with(FxHashSet::default)
@@ -805,7 +805,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
805805
.enumerate()
806806
.collect::<Vec<(usize, String)>>();
807807

808-
for ((span, empty_where), obligations) in type_params.into_iter() {
808+
for ((span, add_where_or_comma), obligations) in type_params.into_iter() {
809809
restrict_type_params = true;
810810
// #74886: Sort here so that the output is always the same.
811811
let mut obligations = obligations.into_iter().collect::<Vec<_>>();
@@ -819,7 +819,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
819819
),
820820
format!(
821821
"{} {}",
822-
if empty_where { " where" } else { "," },
822+
add_where_or_comma,
823823
obligations.join(", ")
824824
),
825825
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)