From c85fde126ed936685b0cdf9d28a3baa96bb0aa3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 5 Apr 2020 12:32:34 -0700 Subject: [PATCH] Account for type params with bounds --- .../traits/error_reporting/suggestions.rs | 11 ++++++----- .../impl-trait-with-missing-bounds.rs | 8 ++++++++ .../impl-trait-with-missing-bounds.stderr | 17 ++++++++++++++++- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index b3209ddc7fe..7e210cb7d25 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -203,8 +203,7 @@ fn suggest_restriction( { if segment.ident.as_str() == impl_name.as_str() { // `fn foo(t: impl Trait)` - // ^^^^^^^^^^ get this to suggest - // `T` instead + // ^^^^^^^^^^ get this to suggest `T` instead // There might be more than one `impl Trait`. ty_spans.push(input.span); @@ -237,7 +236,10 @@ fn suggest_restriction( None => (generics.span, format!("<{}>", type_param)), // `fn foo(t: impl Trait)` // ^^^ suggest `` here - Some(param) => (param.span.shrink_to_hi(), format!(", {}", type_param)), + Some(param) => ( + param.bounds_span().unwrap_or(param.span).shrink_to_hi(), + format!(", {}", type_param), + ), }, // `fn foo(t: impl Trait)` // ^ suggest `where ::A: Bound` @@ -247,8 +249,7 @@ fn suggest_restriction( // Suggest `fn foo(t: T) where ::A: Bound`. err.multipart_suggestion( - "introduce a type parameter with a trait bound instead of using \ - `impl Trait`", + "introduce a type parameter with a trait bound instead of using `impl Trait`", sugg, Applicability::MaybeIncorrect, ); diff --git a/src/test/ui/suggestions/impl-trait-with-missing-bounds.rs b/src/test/ui/suggestions/impl-trait-with-missing-bounds.rs index bef9ba9f91f..d831bafa2b5 100644 --- a/src/test/ui/suggestions/impl-trait-with-missing-bounds.rs +++ b/src/test/ui/suggestions/impl-trait-with-missing-bounds.rs @@ -24,6 +24,14 @@ fn baz(t: impl std::fmt::Debug, constraints: impl Iterator) { } } +fn bat(t: T, constraints: impl Iterator) { + for constraint in constraints { + qux(t); + qux(constraint); +//~^ ERROR `::Item` doesn't implement `std::fmt::Debug` + } +} + fn qux(_: impl std::fmt::Debug) {} fn main() {} diff --git a/src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr b/src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr index 5f84e6d44ea..f0f47876ed0 100644 --- a/src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr +++ b/src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr @@ -43,6 +43,21 @@ help: introduce a type parameter with a trait bound instead of using `impl Trait LL | fn baz(t: impl std::fmt::Debug, constraints: T) where ::Item: std::fmt::Debug { | ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 3 previous errors +error[E0277]: `::Item` doesn't implement `std::fmt::Debug` + --> $DIR/impl-trait-with-missing-bounds.rs:30:13 + | +LL | qux(constraint); + | ^^^^^^^^^^ `::Item` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug` +... +LL | fn qux(_: impl std::fmt::Debug) {} + | --- --------------- required by this bound in `qux` + | + = help: the trait `std::fmt::Debug` is not implemented for `::Item` +help: introduce a type parameter with a trait bound instead of using `impl Trait` + | +LL | fn bat(t: T, constraints: T) where ::Item: std::fmt::Debug { + | ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0277`.