]> git.lizzy.rs Git - rust.git/commitdiff
Account for type params with bounds
authorEsteban Küber <esteban@kuber.com.ar>
Sun, 5 Apr 2020 19:32:34 +0000 (12:32 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Sat, 11 Apr 2020 21:34:01 +0000 (14:34 -0700)
src/librustc_trait_selection/traits/error_reporting/suggestions.rs
src/test/ui/suggestions/impl-trait-with-missing-bounds.rs
src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr

index b3209ddc7fef5a0a12c13ae84699843fb37eeb7d..7e210cb7d25ab5d86392d666dd755bb33b3f2e94 100644 (file)
@@ -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<A>(t: impl Trait)`
                 //        ^^^ suggest `<A, T: Trait>` 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 <T as Trait>::A: Bound`
@@ -247,8 +249,7 @@ fn suggest_restriction(
 
         // Suggest `fn foo<T: Trait>(t: T) where <T as Trait>::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,
         );
index bef9ba9f91f7451392f1f607e647dc28de60d643..d831bafa2b51ce321d3821759e796b79f437bc5f 100644 (file)
@@ -24,6 +24,14 @@ fn baz(t: impl std::fmt::Debug, constraints: impl Iterator) {
     }
 }
 
+fn bat<T: std::fmt::Debug>(t: T, constraints: impl Iterator) {
+    for constraint in constraints {
+        qux(t);
+        qux(constraint);
+//~^ ERROR `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug`
+    }
+}
+
 fn qux(_: impl std::fmt::Debug) {}
 
 fn main() {}
index 5f84e6d44ea9de9ea62644f4aef94adfce64ed16..f0f47876ed0f2ec5a98a9d9cc1dc451a3802daf6 100644 (file)
@@ -43,6 +43,21 @@ help: introduce a type parameter with a trait bound instead of using `impl Trait
 LL | fn baz<T: Iterator>(t: impl std::fmt::Debug, constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug  {
    |       ^^^^^^^^^^^^^                                       ^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: aborting due to 3 previous errors
+error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug`
+  --> $DIR/impl-trait-with-missing-bounds.rs:30:13
+   |
+LL |         qux(constraint);
+   |             ^^^^^^^^^^ `<impl Iterator as std::iter::Iterator>::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 `<impl Iterator as std::iter::Iterator>::Item`
+help: introduce a type parameter with a trait bound instead of using `impl Trait`
+   |
+LL | fn bat<T: std::fmt::Debug, T: Iterator>(t: T, constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug  {
+   |                          ^^^^^^^^^^^^^                     ^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 4 previous errors
 
 For more information about this error, try `rustc --explain E0277`.