]> git.lizzy.rs Git - rust.git/commitdiff
Point out the known type when field doesn't satisfy bound
authorEsteban Küber <esteban@kuber.com.ar>
Sun, 4 Dec 2016 01:41:14 +0000 (17:41 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Thu, 8 Dec 2016 18:25:42 +0000 (10:25 -0800)
For file

```rust
use std::path::Path;

fn f(p: Path) { }
```

provide the following error

```nocode
error[E0277]: the trait bound `[u8]: std::marker::Sized` is not satisfied in `std::path::Path`
 --> file.rs:3:6
  |
3 | fn f(p: Path) { }
  |      ^ within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
  |
  = note: `[u8]` does not have a constant size known at compile-time
  = note: required because it appears within the type `std::path::Path`
  = note: all local variables must have a statically known size
```

src/librustc/traits/error_reporting.rs
src/test/compile-fail/E0277.rs

index 2e8e45468ddcb2199f0cb417ee8ba45ff80db168..db375f2be735b08ad0fe2158eec4220035e9b892 100644 (file)
@@ -487,13 +487,29 @@ pub fn report_selection_error(&self,
                             } else {
                                 let trait_ref = trait_predicate.to_poly_trait_ref();
 
-                                let mut err = struct_span_err!(self.tcx.sess, span, E0277,
-                                    "the trait bound `{}` is not satisfied",
-                                    trait_ref.to_predicate());
-                                err.span_label(span, &format!("the trait `{}` is not implemented \
-                                                               for `{}`",
-                                                              trait_ref,
-                                                              trait_ref.self_ty()));
+                                let (post_message, pre_message) =
+                                    if let ObligationCauseCode::BuiltinDerivedObligation(ref data)
+                                        = obligation.cause.code {
+                                    let parent_trait_ref = self.resolve_type_vars_if_possible(
+                                        &data.parent_trait_ref);
+                                    (format!(" in `{}`", parent_trait_ref.0.self_ty()),
+                                     format!("within `{}`, ", parent_trait_ref.0.self_ty()))
+                                } else {
+                                    (String::new(), String::new())
+                                };
+                                let mut err = struct_span_err!(
+                                    self.tcx.sess,
+                                    span,
+                                    E0277,
+                                    "the trait bound `{}` is not satisfied{}",
+                                    trait_ref.to_predicate(),
+                                    post_message);
+                                err.span_label(span,
+                                               &format!("{}the trait `{}` is not \
+                                                         implemented for `{}`",
+                                                        pre_message,
+                                                        trait_ref,
+                                                        trait_ref.self_ty()));
 
                                 // Try to report a help message
 
index e4cb50cd3f25348deaa0d65df285e1ff5d600ef1..e31fea1e45863205e40132d17162e04a9fbe9e85 100644 (file)
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::path::Path;
+
 trait Foo {
     fn bar(&self);
 }
@@ -16,6 +18,13 @@ fn some_func<T: Foo>(foo: T) {
     foo.bar();
 }
 
+fn f(p: Path) { }
+//~^ ERROR the trait bound `[u8]: std::marker::Sized` is not satisfied in `std::path::Path`
+//~| NOTE within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
+//~| NOTE `[u8]` does not have a constant size known at compile-time
+//~| NOTE required because it appears within the type `std::path::Path`
+//~| NOTE all local variables must have a statically known size
+
 fn main() {
     some_func(5i32);
     //~^ ERROR the trait bound `i32: Foo` is not satisfied