Skip to content

Commit c85fde1

Browse files
committed
Account for type params with bounds
1 parent 794b644 commit c85fde1

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ fn suggest_restriction(
203203
{
204204
if segment.ident.as_str() == impl_name.as_str() {
205205
// `fn foo(t: impl Trait)`
206-
// ^^^^^^^^^^ get this to suggest
207-
// `T` instead
206+
// ^^^^^^^^^^ get this to suggest `T` instead
208207

209208
// There might be more than one `impl Trait`.
210209
ty_spans.push(input.span);
@@ -237,7 +236,10 @@ fn suggest_restriction(
237236
None => (generics.span, format!("<{}>", type_param)),
238237
// `fn foo<A>(t: impl Trait)`
239238
// ^^^ suggest `<A, T: Trait>` here
240-
Some(param) => (param.span.shrink_to_hi(), format!(", {}", type_param)),
239+
Some(param) => (
240+
param.bounds_span().unwrap_or(param.span).shrink_to_hi(),
241+
format!(", {}", type_param),
242+
),
241243
},
242244
// `fn foo(t: impl Trait)`
243245
// ^ suggest `where <T as Trait>::A: Bound`
@@ -247,8 +249,7 @@ fn suggest_restriction(
247249

248250
// Suggest `fn foo<T: Trait>(t: T) where <T as Trait>::A: Bound`.
249251
err.multipart_suggestion(
250-
"introduce a type parameter with a trait bound instead of using \
251-
`impl Trait`",
252+
"introduce a type parameter with a trait bound instead of using `impl Trait`",
252253
sugg,
253254
Applicability::MaybeIncorrect,
254255
);

src/test/ui/suggestions/impl-trait-with-missing-bounds.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ fn baz(t: impl std::fmt::Debug, constraints: impl Iterator) {
2424
}
2525
}
2626

27+
fn bat<T: std::fmt::Debug>(t: T, constraints: impl Iterator) {
28+
for constraint in constraints {
29+
qux(t);
30+
qux(constraint);
31+
//~^ ERROR `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug`
32+
}
33+
}
34+
2735
fn qux(_: impl std::fmt::Debug) {}
2836

2937
fn main() {}

src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ help: introduce a type parameter with a trait bound instead of using `impl Trait
4343
LL | fn baz<T: Iterator>(t: impl std::fmt::Debug, constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug {
4444
| ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4545

46-
error: aborting due to 3 previous errors
46+
error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug`
47+
--> $DIR/impl-trait-with-missing-bounds.rs:30:13
48+
|
49+
LL | qux(constraint);
50+
| ^^^^^^^^^^ `<impl Iterator as std::iter::Iterator>::Item` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
51+
...
52+
LL | fn qux(_: impl std::fmt::Debug) {}
53+
| --- --------------- required by this bound in `qux`
54+
|
55+
= help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item`
56+
help: introduce a type parameter with a trait bound instead of using `impl Trait`
57+
|
58+
LL | fn bat<T: std::fmt::Debug, T: Iterator>(t: T, constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug {
59+
| ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
61+
error: aborting due to 4 previous errors
4762

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

0 commit comments

Comments
 (0)