Skip to content

Commit 0171057

Browse files
authored
Rollup merge of #120874 - gurry:120838-extra-where-in-suggestion, r=fmease
Take empty `where` bounds into account when suggesting predicates Fixes #120838
2 parents fd287d2 + 0815067 commit 0171057

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

compiler/rustc_middle/src/ty/diagnostics.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,17 @@ pub fn suggest_constraining_type_params<'a>(
358358
// trait Foo<T=()> {... }
359359
// - insert: `where T: Zar`
360360
if matches!(param.kind, hir::GenericParamKind::Type { default: Some(_), .. }) {
361+
// If we are here and the where clause span is of non-zero length
362+
// it means we're dealing with an empty where clause like this:
363+
// fn foo<X>(x: X) where { ... }
364+
// In that case we don't want to add another "where" (Fixes #120838)
365+
let where_prefix = if generics.where_clause_span.is_empty() { " where" } else { "" };
366+
361367
// Suggest a bound, but there is no existing `where` clause *and* the type param has a
362368
// default (`<T=Foo>`), so we suggest adding `where T: Bar`.
363369
suggestions.push((
364370
generics.tail_span_for_predicate_suggestion(),
365-
format!(" where {param_name}: {constraint}"),
371+
format!("{where_prefix} {param_name}: {constraint}"),
366372
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name },
367373
));
368374
continue;

tests/ui/trait-impl-bound-suggestions.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@ trait InsufficientlyConstrainedGeneric<X=()> where X: std::marker::Copy {
1717
}
1818
}
1919

20+
// Regression test for #120838
21+
#[allow(dead_code)]
22+
trait InsufficientlyConstrainedGenericWithEmptyWhere<X=()> where X: std::marker::Copy {
23+
fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct<X> {
24+
//~^ ERROR the trait bound `X: Copy` is not satisfied
25+
ConstrainedStruct { x }
26+
}
27+
}
28+
2029
pub fn main() { }

tests/ui/trait-impl-bound-suggestions.rs

+9
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@ trait InsufficientlyConstrainedGeneric<X=()> {
1717
}
1818
}
1919

20+
// Regression test for #120838
21+
#[allow(dead_code)]
22+
trait InsufficientlyConstrainedGenericWithEmptyWhere<X=()> where {
23+
fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct<X> {
24+
//~^ ERROR the trait bound `X: Copy` is not satisfied
25+
ConstrainedStruct { x }
26+
}
27+
}
28+
2029
pub fn main() { }

tests/ui/trait-impl-bound-suggestions.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ help: consider further restricting type parameter `X`
1414
LL | trait InsufficientlyConstrainedGeneric<X=()> where X: std::marker::Copy {
1515
| ++++++++++++++++++++++++++
1616

17-
error: aborting due to 1 previous error
17+
error[E0277]: the trait bound `X: Copy` is not satisfied
18+
--> $DIR/trait-impl-bound-suggestions.rs:23:52
19+
|
20+
LL | fn return_the_constrained_type(&self, x: X) -> ConstrainedStruct<X> {
21+
| ^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `X`
22+
|
23+
note: required by a bound in `ConstrainedStruct`
24+
--> $DIR/trait-impl-bound-suggestions.rs:8:29
25+
|
26+
LL | struct ConstrainedStruct<X: Copy> {
27+
| ^^^^ required by this bound in `ConstrainedStruct`
28+
help: consider further restricting type parameter `X`
29+
|
30+
LL | trait InsufficientlyConstrainedGenericWithEmptyWhere<X=()> where X: std::marker::Copy {
31+
| ++++++++++++++++++++
32+
33+
error: aborting due to 2 previous errors
1834

1935
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)