From ef09db0ff301b98f6980f4bd3dad6fb060d18daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 3 Dec 2016 17:41:14 -0800 Subject: [PATCH 1/2] 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 ``` --- src/librustc/traits/error_reporting.rs | 30 ++++++++++++++++++++------ src/test/compile-fail/E0277.rs | 9 ++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index 2e8e45468ddcb..db375f2be735b 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -487,13 +487,29 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { } 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 diff --git a/src/test/compile-fail/E0277.rs b/src/test/compile-fail/E0277.rs index e4cb50cd3f253..e31fea1e45863 100644 --- a/src/test/compile-fail/E0277.rs +++ b/src/test/compile-fail/E0277.rs @@ -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(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 From 5c130411e907b018aef90aabe766d04e989abe9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 12 Dec 2016 14:51:40 -0800 Subject: [PATCH 2/2] review comments --- src/librustc/traits/error_reporting.rs | 34 ++++++++++++++++++-------- src/test/compile-fail/E0277-2.rs | 34 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 src/test/compile-fail/E0277-2.rs diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index db375f2be735b..51f9c1b353e27 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -457,11 +457,28 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { err } + + /// Get the parent trait chain start + fn get_parent_trait_ref(&self, code: &ObligationCauseCode<'tcx>) -> Option { + match code { + &ObligationCauseCode::BuiltinDerivedObligation(ref data) => { + let parent_trait_ref = self.resolve_type_vars_if_possible( + &data.parent_trait_ref); + match self.get_parent_trait_ref(&data.parent_code) { + Some(t) => Some(t), + None => Some(format!("{}", parent_trait_ref.0.self_ty())), + } + } + _ => None, + } + } + pub fn report_selection_error(&self, obligation: &PredicateObligation<'tcx>, error: &SelectionError<'tcx>) { let span = obligation.cause.span; + let mut err = match *error { SelectionError::Unimplemented => { if let ObligationCauseCode::CompareImplMethodObligation { @@ -486,16 +503,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { return; } else { let trait_ref = trait_predicate.to_poly_trait_ref(); - - 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 (post_message, pre_message) = match self.get_parent_trait_ref( + &obligation.cause.code) + { + Some(t) => { + (format!(" in `{}`", t), format!("within `{}`, ", t)) + } + None => (String::new(), String::new()), }; let mut err = struct_span_err!( self.tcx.sess, diff --git a/src/test/compile-fail/E0277-2.rs b/src/test/compile-fail/E0277-2.rs new file mode 100644 index 0000000000000..211c0e6f89031 --- /dev/null +++ b/src/test/compile-fail/E0277-2.rs @@ -0,0 +1,34 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Foo { + bar: Bar +} + +struct Bar { + baz: Baz +} + +struct Baz { + x: *const u8 +} + +fn is_send() { } + +fn main() { + is_send::(); + //~^ ERROR the trait bound `*const u8: std::marker::Send` is not satisfied in `Foo` + //~| NOTE within `Foo`, the trait `std::marker::Send` is not implemented for `*const u8` + //~| NOTE: `*const u8` cannot be sent between threads safely + //~| NOTE: required because it appears within the type `Baz` + //~| NOTE: required because it appears within the type `Bar` + //~| NOTE: required because it appears within the type `Foo` + //~| NOTE: required by `is_send` +}