don't duplicate the code snippet in the "trait unimplemented" error

new error style:
```
path.rs:4:6: 4:7 error: the trait `core::marker::Sized` is not implemented for the type `[u8]` [E0277]
path.rs:4 fn f(p: Path) {}
               ^
path.rs:4:6: 4:7 help: run `rustc --explain E0277` to see a detailed explanation
path.rs:4:6: 4:7 note: `[u8]` does not have a constant size known at compile-time
path.rs:4:6: 4:7 note: required because it appears within the type `std::sys::os_str::Slice`
path.rs:4:6: 4:7 note: required because it appears within the type `std::ffi::os_str::OsStr`
path.rs:4:6: 4:7 note: required because it appears within the type `std::path::Path`
path.rs:4:6: 4:7 note: all local variables must have a statically known size
path.rs:7:5: 7:36 error: the trait `core::marker::Send` is not implemented for the type `alloc::rc::Rc<()>` [E0277]
path.rs:7     foo::<BTreeMap<Rc<()>, Rc<()>>>();
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
path.rs:7:5: 7:36 help: run `rustc --explain E0277` to see a detailed explanation
path.rs:7:5: 7:36 note: `alloc::rc::Rc<()>` cannot be sent between threads safely
path.rs:7:5: 7:36 note: required because it appears within the type `collections::btree::node::Node<alloc::rc::Rc<()>, alloc::rc::Rc<()>>`
path.rs:7:5: 7:36 note: required because it appears within the type `collections::btree::map::BTreeMap<alloc::rc::Rc<()>, alloc::rc::Rc<()>>`
path.rs:7:5: 7:36 note: required by `foo`
error: aborting due to 2 previous errors
```

This improves the #21793/#23286 situation
This commit is contained in:
Ariel Ben-Yehuda 2015-09-13 22:42:21 +03:00
parent cfd76b364c
commit 38f76dbc38
2 changed files with 46 additions and 41 deletions

View File

@ -207,7 +207,7 @@ pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
let custom_note = report_on_unimplemented(infcx, &trait_ref.0, let custom_note = report_on_unimplemented(infcx, &trait_ref.0,
obligation.cause.span); obligation.cause.span);
if let Some(s) = custom_note { if let Some(s) = custom_note {
infcx.tcx.sess.span_note(obligation.cause.span, &s); infcx.tcx.sess.fileline_note(obligation.cause.span, &s);
} }
note_obligation_cause(infcx, obligation); note_obligation_cause(infcx, obligation);
} }
@ -305,13 +305,13 @@ pub fn report_object_safety_error<'tcx>(tcx: &ty::ctxt<'tcx>,
for violation in object_safety_violations(tcx, trait_def_id) { for violation in object_safety_violations(tcx, trait_def_id) {
match violation { match violation {
ObjectSafetyViolation::SizedSelf => { ObjectSafetyViolation::SizedSelf => {
tcx.sess.span_note( tcx.sess.fileline_note(
span, span,
"the trait cannot require that `Self : Sized`"); "the trait cannot require that `Self : Sized`");
} }
ObjectSafetyViolation::SupertraitSelf => { ObjectSafetyViolation::SupertraitSelf => {
tcx.sess.span_note( tcx.sess.fileline_note(
span, span,
"the trait cannot use `Self` as a type parameter \ "the trait cannot use `Self` as a type parameter \
in the supertrait listing"); in the supertrait listing");
@ -319,7 +319,7 @@ pub fn report_object_safety_error<'tcx>(tcx: &ty::ctxt<'tcx>,
ObjectSafetyViolation::Method(method, ObjectSafetyViolation::Method(method,
MethodViolationCode::StaticMethod) => { MethodViolationCode::StaticMethod) => {
tcx.sess.span_note( tcx.sess.fileline_note(
span, span,
&format!("method `{}` has no receiver", &format!("method `{}` has no receiver",
method.name)); method.name));
@ -327,7 +327,7 @@ pub fn report_object_safety_error<'tcx>(tcx: &ty::ctxt<'tcx>,
ObjectSafetyViolation::Method(method, ObjectSafetyViolation::Method(method,
MethodViolationCode::ReferencesSelf) => { MethodViolationCode::ReferencesSelf) => {
tcx.sess.span_note( tcx.sess.fileline_note(
span, span,
&format!("method `{}` references the `Self` type \ &format!("method `{}` references the `Self` type \
in its arguments or return type", in its arguments or return type",
@ -336,7 +336,7 @@ pub fn report_object_safety_error<'tcx>(tcx: &ty::ctxt<'tcx>,
ObjectSafetyViolation::Method(method, ObjectSafetyViolation::Method(method,
MethodViolationCode::Generic) => { MethodViolationCode::Generic) => {
tcx.sess.span_note( tcx.sess.fileline_note(
span, span,
&format!("method `{}` has generic type parameters", &format!("method `{}` has generic type parameters",
method.name)); method.name));
@ -458,111 +458,117 @@ fn note_obligation_cause_code<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>,
note_obligation_cause_code(infcx, predicate, cause_span, subcode); note_obligation_cause_code(infcx, predicate, cause_span, subcode);
} }
ObligationCauseCode::SliceOrArrayElem => { ObligationCauseCode::SliceOrArrayElem => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
&format!("slice and array elements must have `Sized` type")); &format!("slice and array elements must have `Sized` type"));
} }
ObligationCauseCode::ProjectionWf(data) => { ObligationCauseCode::ProjectionWf(data) => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
&format!("required so that the projection `{}` is well-formed", &format!("required so that the projection `{}` is well-formed",
data)); data));
} }
ObligationCauseCode::ReferenceOutlivesReferent(ref_ty) => { ObligationCauseCode::ReferenceOutlivesReferent(ref_ty) => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
&format!("required so that reference `{}` does not outlive its referent", &format!("required so that reference `{}` does not outlive its referent",
ref_ty)); ref_ty));
} }
ObligationCauseCode::ItemObligation(item_def_id) => { ObligationCauseCode::ItemObligation(item_def_id) => {
let item_name = tcx.item_path_str(item_def_id); let item_name = tcx.item_path_str(item_def_id);
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
&format!("required by `{}`", item_name)); &format!("required by `{}`", item_name));
} }
ObligationCauseCode::ObjectCastObligation(object_ty) => { ObligationCauseCode::ObjectCastObligation(object_ty) => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
&format!( &format!(
"required for the cast to the object type `{}`", "required for the cast to the object type `{}`",
infcx.ty_to_string(object_ty))); infcx.ty_to_string(object_ty)));
} }
ObligationCauseCode::RepeatVec => { ObligationCauseCode::RepeatVec => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
"the `Copy` trait is required because the \ "the `Copy` trait is required because the \
repeated element will be copied"); repeated element will be copied");
} }
ObligationCauseCode::VariableType(_) => { ObligationCauseCode::VariableType(_) => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
"all local variables must have a statically known size"); "all local variables must have a statically known size");
} }
ObligationCauseCode::ReturnType => { ObligationCauseCode::ReturnType => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
"the return type of a function must have a \ "the return type of a function must have a \
statically known size"); statically known size");
} }
ObligationCauseCode::AssignmentLhsSized => { ObligationCauseCode::AssignmentLhsSized => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
"the left-hand-side of an assignment must have a statically known size"); "the left-hand-side of an assignment must have a statically known size");
} }
ObligationCauseCode::StructInitializerSized => { ObligationCauseCode::StructInitializerSized => {
tcx.sess.span_note( tcx.sess.fileline_note(
cause_span, cause_span,
"structs must have a statically known size to be initialized"); "structs must have a statically known size to be initialized");
} }
ObligationCauseCode::ClosureCapture(var_id, closure_span, builtin_bound) => { ObligationCauseCode::ClosureCapture(var_id, _, builtin_bound) => {
let def_id = tcx.lang_items.from_builtin_kind(builtin_bound).unwrap(); let def_id = tcx.lang_items.from_builtin_kind(builtin_bound).unwrap();
let trait_name = tcx.item_path_str(def_id); let trait_name = tcx.item_path_str(def_id);
let name = tcx.local_var_name_str(var_id); let name = tcx.local_var_name_str(var_id);
span_note!(tcx.sess, closure_span, tcx.sess.fileline_note(
"the closure that captures `{}` requires that all captured variables \ cause_span,
implement the trait `{}`", &format!("the closure that captures `{}` requires that all captured variables \
name, implement the trait `{}`",
trait_name); name,
trait_name));
} }
ObligationCauseCode::FieldSized => { ObligationCauseCode::FieldSized => {
span_note!(tcx.sess, cause_span, tcx.sess.fileline_note(
"only the last field of a struct or enum variant \ cause_span,
may have a dynamically sized type") "only the last field of a struct or enum variant \
may have a dynamically sized type");
} }
ObligationCauseCode::SharedStatic => { ObligationCauseCode::SharedStatic => {
span_note!(tcx.sess, cause_span, tcx.sess.fileline_note(
"shared static variables must have a type that implements `Sync`"); cause_span,
"shared static variables must have a type that implements `Sync`");
} }
ObligationCauseCode::BuiltinDerivedObligation(ref data) => { ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
let parent_trait_ref = infcx.resolve_type_vars_if_possible(&data.parent_trait_ref); let parent_trait_ref = infcx.resolve_type_vars_if_possible(&data.parent_trait_ref);
span_note!(tcx.sess, cause_span, tcx.sess.fileline_note(
"required because it appears within the type `{}`", cause_span,
parent_trait_ref.0.self_ty()); &format!("required because it appears within the type `{}`",
parent_trait_ref.0.self_ty()));
let parent_predicate = parent_trait_ref.to_predicate(); let parent_predicate = parent_trait_ref.to_predicate();
note_obligation_cause_code(infcx, &parent_predicate, cause_span, &*data.parent_code); note_obligation_cause_code(infcx, &parent_predicate, cause_span, &*data.parent_code);
} }
ObligationCauseCode::ImplDerivedObligation(ref data) => { ObligationCauseCode::ImplDerivedObligation(ref data) => {
let parent_trait_ref = infcx.resolve_type_vars_if_possible(&data.parent_trait_ref); let parent_trait_ref = infcx.resolve_type_vars_if_possible(&data.parent_trait_ref);
span_note!(tcx.sess, cause_span, tcx.sess.fileline_note(
"required because of the requirements on the impl of `{}` for `{}`", cause_span,
parent_trait_ref, &format!("required because of the requirements on the impl of `{}` for `{}`",
parent_trait_ref.0.self_ty()); parent_trait_ref,
parent_trait_ref.0.self_ty()));
let parent_predicate = parent_trait_ref.to_predicate(); let parent_predicate = parent_trait_ref.to_predicate();
note_obligation_cause_code(infcx, &parent_predicate, cause_span, &*data.parent_code); note_obligation_cause_code(infcx, &parent_predicate, cause_span, &*data.parent_code);
} }
ObligationCauseCode::CompareImplMethodObligation => { ObligationCauseCode::CompareImplMethodObligation => {
span_note!(tcx.sess, cause_span, tcx.sess.fileline_note(
"the requirement `{}` appears on the impl method \ cause_span,
but not on the corresponding trait method", &format!("the requirement `{}` appears on the impl method \
predicate); but not on the corresponding trait method",
predicate));
} }
} }
} }
pub fn suggest_new_overflow_limit(tcx: &ty::ctxt, span: Span) { fn suggest_new_overflow_limit(tcx: &ty::ctxt, span: Span) {
let current_limit = tcx.sess.recursion_limit.get(); let current_limit = tcx.sess.recursion_limit.get();
let suggested_limit = current_limit * 2; let suggested_limit = current_limit * 2;
tcx.sess.span_note( tcx.sess.fileline_note(
span, span,
&format!( &format!(
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate", "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",

View File

@ -29,7 +29,6 @@ pub use self::error_reporting::report_fulfillment_errors;
pub use self::error_reporting::report_overflow_error; pub use self::error_reporting::report_overflow_error;
pub use self::error_reporting::report_selection_error; pub use self::error_reporting::report_selection_error;
pub use self::error_reporting::report_object_safety_error; pub use self::error_reporting::report_object_safety_error;
pub use self::error_reporting::suggest_new_overflow_limit;
pub use self::coherence::orphan_check; pub use self::coherence::orphan_check;
pub use self::coherence::overlapping_impls; pub use self::coherence::overlapping_impls;
pub use self::coherence::OrphanCheckErr; pub use self::coherence::OrphanCheckErr;