Skip to content

Commit f26c855

Browse files
committed
Account for type params with bounds
1 parent 02ccf86 commit f26c855

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ fn suggest_restriction(
202202
{
203203
if segment.ident.as_str() == impl_name.as_str() {
204204
// `fn foo(t: impl Trait)`
205-
// ^^^^^^^^^^ get this to suggest
206-
// `T` instead
205+
// ^^^^^^^^^^ get this to suggest `T` instead
207206

208207
// There might be more than one `impl Trait`.
209208
ty_spans.push(input.span);
@@ -236,7 +235,10 @@ fn suggest_restriction(
236235
None => (generics.span, format!("<{}>", type_param)),
237236
// `fn foo<A>(t: impl Trait)`
238237
// ^^^ suggest `<A, T: Trait>` here
239-
Some(param) => (param.span.shrink_to_hi(), format!(", {}", type_param)),
238+
Some(param) => (
239+
param.bounds_span().unwrap_or(param.span).shrink_to_hi(),
240+
format!(", {}", type_param),
241+
),
240242
},
241243
// `fn foo(t: impl Trait)`
242244
// ^ suggest `where <T as Trait>::A: Bound`
@@ -246,8 +248,7 @@ fn suggest_restriction(
246248

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

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

+8
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

+16-1
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)