Skip to content

Commit 2fb2d30

Browse files
authored
Rollup merge of #38150 - estebank:fix-23286, r=nikomatsakis
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 ``` Fix #23286.
2 parents 65e9691 + 5c13041 commit 2fb2d30

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

src/librustc/traits/error_reporting.rs

+38-8
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,28 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
458458
err
459459
}
460460

461+
462+
/// Get the parent trait chain start
463+
fn get_parent_trait_ref(&self, code: &ObligationCauseCode<'tcx>) -> Option<String> {
464+
match code {
465+
&ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
466+
let parent_trait_ref = self.resolve_type_vars_if_possible(
467+
&data.parent_trait_ref);
468+
match self.get_parent_trait_ref(&data.parent_code) {
469+
Some(t) => Some(t),
470+
None => Some(format!("{}", parent_trait_ref.0.self_ty())),
471+
}
472+
}
473+
_ => None,
474+
}
475+
}
476+
461477
pub fn report_selection_error(&self,
462478
obligation: &PredicateObligation<'tcx>,
463479
error: &SelectionError<'tcx>)
464480
{
465481
let span = obligation.cause.span;
482+
466483
let mut err = match *error {
467484
SelectionError::Unimplemented => {
468485
if let ObligationCauseCode::CompareImplMethodObligation {
@@ -487,14 +504,27 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
487504
return;
488505
} else {
489506
let trait_ref = trait_predicate.to_poly_trait_ref();
490-
491-
let mut err = struct_span_err!(self.tcx.sess, span, E0277,
492-
"the trait bound `{}` is not satisfied",
493-
trait_ref.to_predicate());
494-
err.span_label(span, &format!("the trait `{}` is not implemented \
495-
for `{}`",
496-
trait_ref,
497-
trait_ref.self_ty()));
507+
let (post_message, pre_message) = match self.get_parent_trait_ref(
508+
&obligation.cause.code)
509+
{
510+
Some(t) => {
511+
(format!(" in `{}`", t), format!("within `{}`, ", t))
512+
}
513+
None => (String::new(), String::new()),
514+
};
515+
let mut err = struct_span_err!(
516+
self.tcx.sess,
517+
span,
518+
E0277,
519+
"the trait bound `{}` is not satisfied{}",
520+
trait_ref.to_predicate(),
521+
post_message);
522+
err.span_label(span,
523+
&format!("{}the trait `{}` is not \
524+
implemented for `{}`",
525+
pre_message,
526+
trait_ref,
527+
trait_ref.self_ty()));
498528

499529
// Try to report a help message
500530

src/test/compile-fail/E0277-2.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo {
12+
bar: Bar
13+
}
14+
15+
struct Bar {
16+
baz: Baz
17+
}
18+
19+
struct Baz {
20+
x: *const u8
21+
}
22+
23+
fn is_send<T: Send>() { }
24+
25+
fn main() {
26+
is_send::<Foo>();
27+
//~^ ERROR the trait bound `*const u8: std::marker::Send` is not satisfied in `Foo`
28+
//~| NOTE within `Foo`, the trait `std::marker::Send` is not implemented for `*const u8`
29+
//~| NOTE: `*const u8` cannot be sent between threads safely
30+
//~| NOTE: required because it appears within the type `Baz`
31+
//~| NOTE: required because it appears within the type `Bar`
32+
//~| NOTE: required because it appears within the type `Foo`
33+
//~| NOTE: required by `is_send`
34+
}

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)