Skip to content

Commit ef09db0

Browse files
committed
Point out the known type when field doesn't satisfy bound
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 ```
1 parent 070fad1 commit ef09db0

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/librustc/traits/error_reporting.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,29 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
487487
} else {
488488
let trait_ref = trait_predicate.to_poly_trait_ref();
489489

490-
let mut err = struct_span_err!(self.tcx.sess, span, E0277,
491-
"the trait bound `{}` is not satisfied",
492-
trait_ref.to_predicate());
493-
err.span_label(span, &format!("the trait `{}` is not implemented \
494-
for `{}`",
495-
trait_ref,
496-
trait_ref.self_ty()));
490+
let (post_message, pre_message) =
491+
if let ObligationCauseCode::BuiltinDerivedObligation(ref data)
492+
= obligation.cause.code {
493+
let parent_trait_ref = self.resolve_type_vars_if_possible(
494+
&data.parent_trait_ref);
495+
(format!(" in `{}`", parent_trait_ref.0.self_ty()),
496+
format!("within `{}`, ", parent_trait_ref.0.self_ty()))
497+
} else {
498+
(String::new(), String::new())
499+
};
500+
let mut err = struct_span_err!(
501+
self.tcx.sess,
502+
span,
503+
E0277,
504+
"the trait bound `{}` is not satisfied{}",
505+
trait_ref.to_predicate(),
506+
post_message);
507+
err.span_label(span,
508+
&format!("{}the trait `{}` is not \
509+
implemented for `{}`",
510+
pre_message,
511+
trait_ref,
512+
trait_ref.self_ty()));
497513

498514
// Try to report a help message
499515

src/test/compile-fail/E0277.rs

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::path::Path;
12+
1113
trait Foo {
1214
fn bar(&self);
1315
}
@@ -16,6 +18,13 @@ fn some_func<T: Foo>(foo: T) {
1618
foo.bar();
1719
}
1820

21+
fn f(p: Path) { }
22+
//~^ ERROR the trait bound `[u8]: std::marker::Sized` is not satisfied in `std::path::Path`
23+
//~| NOTE within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
24+
//~| NOTE `[u8]` does not have a constant size known at compile-time
25+
//~| NOTE required because it appears within the type `std::path::Path`
26+
//~| NOTE all local variables must have a statically known size
27+
1928
fn main() {
2029
some_func(5i32);
2130
//~^ ERROR the trait bound `i32: Foo` is not satisfied

0 commit comments

Comments
 (0)