2014-12-06 11:39:25 -05:00
|
|
|
// Copyright 2014 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 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
use super::{
|
|
|
|
FulfillmentError,
|
|
|
|
FulfillmentErrorCode,
|
|
|
|
MismatchedProjectionTypes,
|
2015-03-20 06:48:40 -04:00
|
|
|
Obligation,
|
2014-12-17 14:16:28 -05:00
|
|
|
ObligationCauseCode,
|
|
|
|
OutputTypeParameterMismatch,
|
2015-04-15 11:57:29 +12:00
|
|
|
TraitNotObjectSafe,
|
2014-12-17 14:16:28 -05:00
|
|
|
PredicateObligation,
|
|
|
|
SelectionError,
|
2015-04-15 11:57:29 +12:00
|
|
|
ObjectSafetyViolation,
|
|
|
|
MethodViolationCode,
|
|
|
|
object_safety_violations,
|
2014-12-17 14:16:28 -05:00
|
|
|
};
|
2014-12-06 11:39:25 -05:00
|
|
|
|
2015-01-10 23:51:27 +05:30
|
|
|
use fmt_macros::{Parser, Piece, Position};
|
2015-08-16 06:32:28 -04:00
|
|
|
use middle::def_id::DefId;
|
2014-12-06 11:39:25 -05:00
|
|
|
use middle::infer::InferCtxt;
|
2015-08-07 10:28:51 -04:00
|
|
|
use middle::ty::{self, ToPredicate, HasTypeFlags, ToPolyTraitRef, TraitRef, Ty};
|
2015-09-06 18:32:34 +03:00
|
|
|
use middle::ty::fold::TypeFoldable;
|
2015-09-25 02:40:57 +03:00
|
|
|
use util::nodemap::{FnvHashMap, FnvHashSet};
|
|
|
|
|
2015-11-28 11:41:52 +01:00
|
|
|
use std::cmp;
|
2015-06-18 20:25:05 +03:00
|
|
|
use std::fmt;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr::{AttributeMethods, AttrMetaMethods};
|
2015-12-21 10:00:43 +13:00
|
|
|
use syntax::codemap::Span;
|
|
|
|
use syntax::errors::DiagnosticBuilder;
|
2014-12-06 11:39:25 -05:00
|
|
|
|
2015-09-24 19:58:00 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct TraitErrorKey<'tcx> {
|
|
|
|
span: Span,
|
|
|
|
predicate: ty::Predicate<'tcx>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TraitErrorKey<'tcx> {
|
|
|
|
fn from_error<'a>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
e: &FulfillmentError<'tcx>) -> Self {
|
|
|
|
let predicate =
|
|
|
|
infcx.resolve_type_vars_if_possible(&e.obligation.predicate);
|
|
|
|
TraitErrorKey {
|
|
|
|
span: e.obligation.cause.span,
|
|
|
|
predicate: infcx.tcx.erase_regions(&predicate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 11:39:25 -05:00
|
|
|
pub fn report_fulfillment_errors<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
errors: &Vec<FulfillmentError<'tcx>>) {
|
2015-01-31 12:20:46 -05:00
|
|
|
for error in errors {
|
2014-12-06 11:39:25 -05:00
|
|
|
report_fulfillment_error(infcx, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_fulfillment_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
error: &FulfillmentError<'tcx>) {
|
2015-09-24 19:58:00 +03:00
|
|
|
let error_key = TraitErrorKey::from_error(infcx, error);
|
|
|
|
debug!("report_fulfillment_errors({:?}) - key={:?}",
|
|
|
|
error, error_key);
|
|
|
|
if !infcx.reported_trait_errors.borrow_mut().insert(error_key) {
|
|
|
|
debug!("report_fulfillment_errors: skipping duplicate");
|
|
|
|
return;
|
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
match error.code {
|
|
|
|
FulfillmentErrorCode::CodeSelectionError(ref e) => {
|
|
|
|
report_selection_error(infcx, &error.obligation, e);
|
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
FulfillmentErrorCode::CodeProjectionError(ref e) => {
|
|
|
|
report_projection_error(infcx, &error.obligation, e);
|
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
FulfillmentErrorCode::CodeAmbiguity => {
|
|
|
|
maybe_report_ambiguity(infcx, &error.obligation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
pub fn report_projection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
obligation: &PredicateObligation<'tcx>,
|
|
|
|
error: &MismatchedProjectionTypes<'tcx>)
|
|
|
|
{
|
|
|
|
let predicate =
|
|
|
|
infcx.resolve_type_vars_if_possible(&obligation.predicate);
|
2015-08-07 10:28:51 -04:00
|
|
|
|
2015-06-11 16:21:46 -07:00
|
|
|
// The TyError created by normalize_to_error can end up being unified
|
2015-05-06 22:24:13 +03:00
|
|
|
// into all obligations: for example, if our obligation is something
|
|
|
|
// like `$X = <() as Foo<$X>>::Out` and () does not implement Foo<_>,
|
2015-06-11 16:21:46 -07:00
|
|
|
// then $X will be unified with TyError, but the error still needs to be
|
2015-05-06 22:24:13 +03:00
|
|
|
// reported.
|
2015-05-05 22:09:17 +03:00
|
|
|
if !infcx.tcx.sess.has_errors() || !predicate.references_error() {
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(infcx.tcx.sess, obligation.cause.span, E0271,
|
2015-08-07 10:28:51 -04:00
|
|
|
"type mismatch resolving `{}`: {}",
|
|
|
|
predicate,
|
|
|
|
error.err);
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 23:51:27 +05:30
|
|
|
fn report_on_unimplemented<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
|
2015-01-11 16:41:02 +05:30
|
|
|
trait_ref: &TraitRef<'tcx>,
|
|
|
|
span: Span) -> Option<String> {
|
2015-01-10 23:51:27 +05:30
|
|
|
let def_id = trait_ref.def_id;
|
|
|
|
let mut report = None;
|
2015-06-25 23:42:17 +03:00
|
|
|
for item in infcx.tcx.get_attrs(def_id).iter() {
|
2015-01-11 18:28:06 +05:30
|
|
|
if item.check_name("rustc_on_unimplemented") {
|
2015-08-10 20:40:46 +02:00
|
|
|
let err_sp = item.meta().span.substitute_dummy(span);
|
2015-06-25 23:42:17 +03:00
|
|
|
let def = infcx.tcx.lookup_trait_def(def_id);
|
2015-06-18 20:25:05 +03:00
|
|
|
let trait_str = def.trait_ref.to_string();
|
2015-01-10 23:51:27 +05:30
|
|
|
if let Some(ref istring) = item.value_str() {
|
|
|
|
let mut generic_map = def.generics.types.iter_enumerated()
|
|
|
|
.map(|(param, i, gen)| {
|
|
|
|
(gen.name.as_str().to_string(),
|
|
|
|
trait_ref.substs.types.get(param, i)
|
2015-06-18 20:25:05 +03:00
|
|
|
.to_string())
|
2015-09-25 02:40:57 +03:00
|
|
|
}).collect::<FnvHashMap<String, String>>();
|
2015-01-10 23:51:27 +05:30
|
|
|
generic_map.insert("Self".to_string(),
|
2015-06-18 20:25:05 +03:00
|
|
|
trait_ref.self_ty().to_string());
|
2015-02-04 21:48:12 +01:00
|
|
|
let parser = Parser::new(&istring);
|
2015-01-10 23:51:27 +05:30
|
|
|
let mut errored = false;
|
|
|
|
let err: String = parser.filter_map(|p| {
|
|
|
|
match p {
|
|
|
|
Piece::String(s) => Some(s),
|
|
|
|
Piece::NextArgument(a) => match a.position {
|
|
|
|
Position::ArgumentNamed(s) => match generic_map.get(s) {
|
2015-02-01 21:53:25 -05:00
|
|
|
Some(val) => Some(val),
|
2015-01-10 23:51:27 +05:30
|
|
|
None => {
|
2015-01-18 16:58:25 -08:00
|
|
|
span_err!(infcx.tcx.sess, err_sp, E0272,
|
|
|
|
"the #[rustc_on_unimplemented] \
|
2015-01-11 18:28:06 +05:30
|
|
|
attribute on \
|
2015-01-11 16:41:02 +05:30
|
|
|
trait definition for {} refers to \
|
|
|
|
non-existent type parameter {}",
|
2015-01-18 16:58:25 -08:00
|
|
|
trait_str, s);
|
2015-01-10 23:51:27 +05:30
|
|
|
errored = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
2015-01-18 16:58:25 -08:00
|
|
|
span_err!(infcx.tcx.sess, err_sp, E0273,
|
|
|
|
"the #[rustc_on_unimplemented] \
|
2015-01-11 18:28:06 +05:30
|
|
|
attribute on \
|
2015-01-11 16:41:02 +05:30
|
|
|
trait definition for {} must have named \
|
|
|
|
format arguments, \
|
2015-01-11 18:28:06 +05:30
|
|
|
eg `#[rustc_on_unimplemented = \
|
|
|
|
\"foo {{T}}\"]`",
|
2015-01-18 16:58:25 -08:00
|
|
|
trait_str);
|
2015-01-10 23:51:27 +05:30
|
|
|
errored = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
// Report only if the format string checks out
|
|
|
|
if !errored {
|
|
|
|
report = Some(err);
|
|
|
|
}
|
|
|
|
} else {
|
2015-01-18 16:58:25 -08:00
|
|
|
span_err!(infcx.tcx.sess, err_sp, E0274,
|
|
|
|
"the #[rustc_on_unimplemented] attribute on \
|
2015-01-11 16:41:02 +05:30
|
|
|
trait definition for {} must have a value, \
|
2015-01-11 18:28:06 +05:30
|
|
|
eg `#[rustc_on_unimplemented = \"foo\"]`",
|
2015-01-18 16:58:25 -08:00
|
|
|
trait_str);
|
2015-01-10 23:51:27 +05:30
|
|
|
}
|
2015-01-11 20:03:20 +01:00
|
|
|
break;
|
2015-01-10 23:51:27 +05:30
|
|
|
}
|
2015-01-11 20:03:20 +01:00
|
|
|
}
|
2015-01-10 23:51:27 +05:30
|
|
|
report
|
|
|
|
}
|
|
|
|
|
2015-03-20 06:48:40 -04:00
|
|
|
/// Reports that an overflow has occurred and halts compilation. We
|
|
|
|
/// halt compilation unconditionally because it is important that
|
|
|
|
/// overflows never be masked -- they basically represent computations
|
|
|
|
/// whose result could not be truly determined and thus we can't say
|
|
|
|
/// if the program type checks or not -- and they are unusual
|
|
|
|
/// occurrences in any case.
|
|
|
|
pub fn report_overflow_error<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
obligation: &Obligation<'tcx, T>)
|
|
|
|
-> !
|
2015-08-17 23:50:24 +03:00
|
|
|
where T: fmt::Display + TypeFoldable<'tcx> + HasTypeFlags
|
2015-03-20 06:48:40 -04:00
|
|
|
{
|
|
|
|
let predicate =
|
|
|
|
infcx.resolve_type_vars_if_possible(&obligation.predicate);
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(infcx.tcx.sess, obligation.cause.span, E0275,
|
|
|
|
"overflow evaluating the requirement `{}`",
|
|
|
|
predicate);
|
2015-03-20 06:48:40 -04:00
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
suggest_new_overflow_limit(infcx.tcx, &mut err, obligation.cause.span);
|
2015-03-20 06:48:40 -04:00
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
2015-03-20 06:48:40 -04:00
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
err.emit();
|
2015-03-20 06:48:40 -04:00
|
|
|
infcx.tcx.sess.abort_if_errors();
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
|
2014-12-06 11:39:25 -05:00
|
|
|
pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
obligation: &PredicateObligation<'tcx>,
|
|
|
|
error: &SelectionError<'tcx>)
|
|
|
|
{
|
|
|
|
match *error {
|
2015-01-14 13:43:17 -08:00
|
|
|
SelectionError::Unimplemented => {
|
2015-08-07 10:28:51 -04:00
|
|
|
if let ObligationCauseCode::CompareImplMethodObligation = obligation.cause.code {
|
2015-12-15 04:31:58 -05:00
|
|
|
span_err!(
|
|
|
|
infcx.tcx.sess, obligation.cause.span, E0276,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the requirement `{}` appears on the impl \
|
|
|
|
method but not on the corresponding trait method",
|
2015-09-27 20:26:12 -06:00
|
|
|
obligation.predicate);
|
2015-08-07 10:28:51 -04:00
|
|
|
} else {
|
|
|
|
match obligation.predicate {
|
|
|
|
ty::Predicate::Trait(ref trait_predicate) => {
|
|
|
|
let trait_predicate =
|
|
|
|
infcx.resolve_type_vars_if_possible(trait_predicate);
|
|
|
|
|
|
|
|
if !infcx.tcx.sess.has_errors() || !trait_predicate.references_error() {
|
|
|
|
let trait_ref = trait_predicate.to_poly_trait_ref();
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(
|
2015-12-15 04:31:58 -05:00
|
|
|
infcx.tcx.sess, obligation.cause.span, E0277,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the trait `{}` is not implemented for the type `{}`",
|
|
|
|
trait_ref, trait_ref.self_ty());
|
|
|
|
|
|
|
|
// Check if it has a custom "#[rustc_on_unimplemented]"
|
|
|
|
// error message, report with that message if it does
|
|
|
|
let custom_note = report_on_unimplemented(infcx, &trait_ref.0,
|
|
|
|
obligation.cause.span);
|
2015-08-11 14:32:01 -04:00
|
|
|
if let Some(s) = custom_note {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(obligation.cause.span, &s);
|
2015-11-28 11:41:52 +01:00
|
|
|
} else {
|
|
|
|
let mut impl_candidates = Vec::new();
|
|
|
|
infcx.tcx.lookup_trait_def(trait_ref.def_id())
|
|
|
|
.for_each_relevant_impl(
|
|
|
|
infcx.tcx,
|
|
|
|
trait_ref.self_ty(),
|
|
|
|
|impl_def_id| {
|
|
|
|
match infcx.tcx.impl_trait_ref(impl_def_id) {
|
|
|
|
Some(ref imp) => {
|
|
|
|
impl_candidates.push(format!(" {}", imp));
|
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if impl_candidates.len() > 0 {
|
|
|
|
err.fileline_help(
|
|
|
|
obligation.cause.span,
|
|
|
|
&format!("the following implementations were found:"));
|
|
|
|
|
|
|
|
let end = cmp::min(4, impl_candidates.len());
|
|
|
|
for candidate in &impl_candidates[0..end] {
|
|
|
|
err.fileline_help(obligation.cause.span,
|
|
|
|
candidate);
|
|
|
|
}
|
|
|
|
if impl_candidates.len() > 4 {
|
|
|
|
err.fileline_help(obligation.cause.span,
|
|
|
|
&format!("and {} others",
|
|
|
|
impl_candidates.len()-4));
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 13:43:17 -08:00
|
|
|
}
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2015-01-14 13:43:17 -08:00
|
|
|
}
|
2015-11-28 11:41:52 +01:00
|
|
|
},
|
2015-08-07 10:28:51 -04:00
|
|
|
ty::Predicate::Equate(ref predicate) => {
|
|
|
|
let predicate = infcx.resolve_type_vars_if_possible(predicate);
|
|
|
|
let err = infcx.equality_predicate(obligation.cause.span,
|
|
|
|
&predicate).err().unwrap();
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(
|
2015-12-15 04:31:58 -05:00
|
|
|
infcx.tcx.sess, obligation.cause.span, E0278,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the requirement `{}` is not satisfied (`{}`)",
|
|
|
|
predicate,
|
|
|
|
err);
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
2015-01-14 13:43:17 -08:00
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
ty::Predicate::RegionOutlives(ref predicate) => {
|
|
|
|
let predicate = infcx.resolve_type_vars_if_possible(predicate);
|
|
|
|
let err = infcx.region_outlives_predicate(obligation.cause.span,
|
|
|
|
&predicate).err().unwrap();
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(
|
2015-12-15 04:31:58 -05:00
|
|
|
infcx.tcx.sess, obligation.cause.span, E0279,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the requirement `{}` is not satisfied (`{}`)",
|
|
|
|
predicate,
|
|
|
|
err);
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
2015-01-14 13:43:17 -08:00
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
ty::Predicate::Projection(..) | ty::Predicate::TypeOutlives(..) => {
|
|
|
|
let predicate =
|
|
|
|
infcx.resolve_type_vars_if_possible(&obligation.predicate);
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(
|
2015-12-15 04:31:58 -05:00
|
|
|
infcx.tcx.sess, obligation.cause.span, E0280,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the requirement `{}` is not satisfied",
|
|
|
|
predicate);
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::Predicate::ObjectSafe(trait_def_id) => {
|
2015-09-24 18:27:29 +03:00
|
|
|
let violations = object_safety_violations(
|
|
|
|
infcx.tcx, trait_def_id);
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = report_object_safety_error(infcx.tcx,
|
|
|
|
obligation.cause.span,
|
|
|
|
trait_def_id,
|
|
|
|
violations);
|
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::Predicate::WellFormed(ty) => {
|
|
|
|
// WF predicates cannot themselves make
|
|
|
|
// errors. They can only block due to
|
|
|
|
// ambiguity; otherwise, they always
|
|
|
|
// degenerate into other obligations
|
|
|
|
// (which may fail).
|
|
|
|
infcx.tcx.sess.span_bug(
|
|
|
|
obligation.cause.span,
|
|
|
|
&format!("WF predicate not satisfied for {:?}", ty));
|
2015-01-14 13:43:17 -08:00
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 13:43:17 -08:00
|
|
|
|
2014-12-06 11:39:25 -05:00
|
|
|
OutputTypeParameterMismatch(ref expected_trait_ref, ref actual_trait_ref, ref e) => {
|
2014-12-17 14:16:28 -05:00
|
|
|
let expected_trait_ref = infcx.resolve_type_vars_if_possible(&*expected_trait_ref);
|
|
|
|
let actual_trait_ref = infcx.resolve_type_vars_if_possible(&*actual_trait_ref);
|
2015-06-24 02:54:32 +03:00
|
|
|
if !actual_trait_ref.self_ty().references_error() {
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(
|
2015-12-15 04:31:58 -05:00
|
|
|
infcx.tcx.sess, obligation.cause.span, E0281,
|
2015-08-07 10:28:51 -04:00
|
|
|
"type mismatch: the type `{}` implements the trait `{}`, \
|
|
|
|
but the trait `{}` is required ({})",
|
|
|
|
expected_trait_ref.self_ty(),
|
|
|
|
expected_trait_ref,
|
|
|
|
actual_trait_ref,
|
|
|
|
e);
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-15 11:57:29 +12:00
|
|
|
|
|
|
|
TraitNotObjectSafe(did) => {
|
2015-09-24 18:27:29 +03:00
|
|
|
let violations = object_safety_violations(infcx.tcx, did);
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = report_object_safety_error(infcx.tcx, obligation.cause.span, did,
|
|
|
|
violations);
|
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-15 11:57:29 +12:00
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
pub fn report_object_safety_error<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
span: Span,
|
2015-08-16 06:32:28 -04:00
|
|
|
trait_def_id: DefId,
|
2015-12-15 04:31:58 -05:00
|
|
|
violations: Vec<ObjectSafetyViolation>)
|
2015-12-21 10:00:43 +13:00
|
|
|
-> DiagnosticBuilder<'tcx>
|
2015-08-07 10:28:51 -04:00
|
|
|
{
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(
|
2015-12-15 04:31:58 -05:00
|
|
|
tcx.sess, span, E0038,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the trait `{}` cannot be made into an object",
|
|
|
|
tcx.item_path_str(trait_def_id));
|
2015-04-15 11:57:29 +12:00
|
|
|
|
2015-09-25 02:40:57 +03:00
|
|
|
let mut reported_violations = FnvHashSet();
|
2015-09-24 18:27:29 +03:00
|
|
|
for violation in violations {
|
2015-09-25 02:40:57 +03:00
|
|
|
if !reported_violations.insert(violation.clone()) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-07 10:28:51 -04:00
|
|
|
match violation {
|
|
|
|
ObjectSafetyViolation::SizedSelf => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-08-07 10:28:51 -04:00
|
|
|
span,
|
|
|
|
"the trait cannot require that `Self : Sized`");
|
|
|
|
}
|
2015-04-15 11:57:29 +12:00
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
ObjectSafetyViolation::SupertraitSelf => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-08-07 10:28:51 -04:00
|
|
|
span,
|
|
|
|
"the trait cannot use `Self` as a type parameter \
|
|
|
|
in the supertrait listing");
|
|
|
|
}
|
2015-04-15 11:57:29 +12:00
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
ObjectSafetyViolation::Method(method,
|
|
|
|
MethodViolationCode::StaticMethod) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-08-07 10:28:51 -04:00
|
|
|
span,
|
|
|
|
&format!("method `{}` has no receiver",
|
|
|
|
method.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectSafetyViolation::Method(method,
|
|
|
|
MethodViolationCode::ReferencesSelf) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-08-07 10:28:51 -04:00
|
|
|
span,
|
|
|
|
&format!("method `{}` references the `Self` type \
|
|
|
|
in its arguments or return type",
|
|
|
|
method.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectSafetyViolation::Method(method,
|
|
|
|
MethodViolationCode::Generic) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-08-07 10:28:51 -04:00
|
|
|
span,
|
|
|
|
&format!("method `{}` has generic type parameters",
|
|
|
|
method.name));
|
2015-04-15 11:57:29 +12:00
|
|
|
}
|
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2015-12-21 10:00:43 +13:00
|
|
|
err
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
pub fn maybe_report_ambiguity<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
obligation: &PredicateObligation<'tcx>) {
|
2014-12-06 11:39:25 -05:00
|
|
|
// Unable to successfully determine, probably means
|
|
|
|
// insufficient type information, but could mean
|
|
|
|
// ambiguous impls. The latter *ought* to be a
|
|
|
|
// coherence violation, so we don't report it here.
|
|
|
|
|
2014-12-30 17:42:02 -05:00
|
|
|
let predicate = infcx.resolve_type_vars_if_possible(&obligation.predicate);
|
2014-12-06 11:39:25 -05:00
|
|
|
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("maybe_report_ambiguity(predicate={:?}, obligation={:?})",
|
|
|
|
predicate,
|
|
|
|
obligation);
|
2014-12-30 17:42:02 -05:00
|
|
|
|
|
|
|
match predicate {
|
|
|
|
ty::Predicate::Trait(ref data) => {
|
|
|
|
let trait_ref = data.to_poly_trait_ref();
|
|
|
|
let self_ty = trait_ref.self_ty();
|
|
|
|
let all_types = &trait_ref.substs().types;
|
2015-06-24 02:54:32 +03:00
|
|
|
if all_types.references_error() {
|
2015-12-15 04:31:58 -05:00
|
|
|
} else {
|
|
|
|
// Typically, this ambiguity should only happen if
|
|
|
|
// there are unresolved type inference variables
|
|
|
|
// (otherwise it would suggest a coherence
|
|
|
|
// failure). But given #21974 that is not necessarily
|
|
|
|
// the case -- we can have multiple where clauses that
|
|
|
|
// are only distinguished by a region, which results
|
|
|
|
// in an ambiguity even when all types are fully
|
|
|
|
// known, since we don't dispatch based on region
|
|
|
|
// relationships.
|
|
|
|
|
2014-12-30 17:42:02 -05:00
|
|
|
// This is kind of a hack: it frequently happens that some earlier
|
|
|
|
// error prevents types from being fully inferred, and then we get
|
|
|
|
// a bunch of uninteresting errors saying something like "<generic
|
|
|
|
// #0> doesn't implement Sized". It may even be true that we
|
|
|
|
// could just skip over all checks where the self-ty is an
|
|
|
|
// inference variable, but I was afraid that there might be an
|
|
|
|
// inference variable created, registered as an obligation, and
|
|
|
|
// then never forced by writeback, and hence by skipping here we'd
|
|
|
|
// be ignoring the fact that we don't KNOW the type works
|
|
|
|
// out. Though even that would probably be harmless, given that
|
|
|
|
// we're only talking about builtin traits, which are known to be
|
|
|
|
// inhabited. But in any case I just threw in this check for
|
|
|
|
// has_errors() to be sure that compilation isn't happening
|
|
|
|
// anyway. In that case, why inundate the user.
|
|
|
|
if !infcx.tcx.sess.has_errors() {
|
|
|
|
if
|
|
|
|
infcx.tcx.lang_items.sized_trait()
|
|
|
|
.map_or(false, |sized_id| sized_id == trait_ref.def_id())
|
|
|
|
{
|
2015-08-07 10:28:51 -04:00
|
|
|
need_type_info(infcx, obligation.cause.span, self_ty);
|
2014-12-30 17:42:02 -05:00
|
|
|
} else {
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(infcx.tcx.sess, obligation.cause.span, E0283,
|
|
|
|
"type annotations required: \
|
|
|
|
cannot resolve `{}`",
|
|
|
|
predicate);
|
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2014-12-30 17:42:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
ty::Predicate::WellFormed(ty) => {
|
|
|
|
// Same hacky approach as above to avoid deluging user
|
|
|
|
// with error messages.
|
|
|
|
if !ty.references_error() && !infcx.tcx.sess.has_errors() {
|
|
|
|
need_type_info(infcx, obligation.cause.span, ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 17:42:02 -05:00
|
|
|
_ => {
|
|
|
|
if !infcx.tcx.sess.has_errors() {
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(infcx.tcx.sess, obligation.cause.span, E0284,
|
|
|
|
"type annotations required: cannot resolve `{}`",
|
|
|
|
predicate);
|
|
|
|
note_obligation_cause(infcx, &mut err, obligation);
|
|
|
|
err.emit();
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
fn need_type_info<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
span: Span,
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
{
|
|
|
|
span_err!(infcx.tcx.sess, span, E0282,
|
|
|
|
"unable to infer enough type information about `{}`; \
|
|
|
|
type annotations or generic parameter binding required",
|
|
|
|
ty);
|
|
|
|
}
|
|
|
|
|
2015-03-20 06:48:40 -04:00
|
|
|
fn note_obligation_cause<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>,
|
2015-12-21 10:00:43 +13:00
|
|
|
err: &mut DiagnosticBuilder,
|
2015-03-20 06:48:40 -04:00
|
|
|
obligation: &Obligation<'tcx, T>)
|
2015-06-18 20:25:05 +03:00
|
|
|
where T: fmt::Display
|
2014-12-06 11:39:25 -05:00
|
|
|
{
|
|
|
|
note_obligation_cause_code(infcx,
|
2015-12-21 10:00:43 +13:00
|
|
|
err,
|
2014-12-17 14:16:28 -05:00
|
|
|
&obligation.predicate,
|
2014-12-06 11:39:25 -05:00
|
|
|
obligation.cause.span,
|
2014-12-17 14:16:28 -05:00
|
|
|
&obligation.cause.code);
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
|
2015-03-20 06:48:40 -04:00
|
|
|
fn note_obligation_cause_code<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>,
|
2015-12-21 10:00:43 +13:00
|
|
|
err: &mut DiagnosticBuilder,
|
2015-03-20 06:48:40 -04:00
|
|
|
predicate: &T,
|
|
|
|
cause_span: Span,
|
|
|
|
cause_code: &ObligationCauseCode<'tcx>)
|
2015-06-18 20:25:05 +03:00
|
|
|
where T: fmt::Display
|
2014-12-06 11:39:25 -05:00
|
|
|
{
|
|
|
|
let tcx = infcx.tcx;
|
|
|
|
match *cause_code {
|
|
|
|
ObligationCauseCode::MiscObligation => { }
|
2015-08-07 10:28:51 -04:00
|
|
|
ObligationCauseCode::SliceOrArrayElem => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-08-07 10:28:51 -04:00
|
|
|
cause_span,
|
2015-11-05 15:30:32 +01:00
|
|
|
"slice and array elements must have `Sized` type");
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
ObligationCauseCode::ProjectionWf(data) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-08-07 10:28:51 -04:00
|
|
|
cause_span,
|
|
|
|
&format!("required so that the projection `{}` is well-formed",
|
|
|
|
data));
|
|
|
|
}
|
|
|
|
ObligationCauseCode::ReferenceOutlivesReferent(ref_ty) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-08-07 10:28:51 -04:00
|
|
|
cause_span,
|
|
|
|
&format!("required so that reference `{}` does not outlive its referent",
|
|
|
|
ref_ty));
|
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
ObligationCauseCode::ItemObligation(item_def_id) => {
|
2015-06-25 23:42:17 +03:00
|
|
|
let item_name = tcx.item_path_str(item_def_id);
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2014-12-06 11:39:25 -05:00
|
|
|
cause_span,
|
2015-02-01 21:53:25 -05:00
|
|
|
&format!("required by `{}`", item_name));
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::ObjectCastObligation(object_ty) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2014-12-06 11:39:25 -05:00
|
|
|
cause_span,
|
2015-02-01 21:53:25 -05:00
|
|
|
&format!(
|
2014-12-17 14:16:28 -05:00
|
|
|
"required for the cast to the object type `{}`",
|
2015-02-01 21:53:25 -05:00
|
|
|
infcx.ty_to_string(object_ty)));
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::RepeatVec => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2014-12-06 11:39:25 -05:00
|
|
|
cause_span,
|
|
|
|
"the `Copy` trait is required because the \
|
|
|
|
repeated element will be copied");
|
|
|
|
}
|
|
|
|
ObligationCauseCode::VariableType(_) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2014-12-06 11:39:25 -05:00
|
|
|
cause_span,
|
|
|
|
"all local variables must have a statically known size");
|
|
|
|
}
|
|
|
|
ObligationCauseCode::ReturnType => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2014-12-06 11:39:25 -05:00
|
|
|
cause_span,
|
|
|
|
"the return type of a function must have a \
|
|
|
|
statically known size");
|
|
|
|
}
|
|
|
|
ObligationCauseCode::AssignmentLhsSized => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2014-12-06 11:39:25 -05:00
|
|
|
cause_span,
|
|
|
|
"the left-hand-side of an assignment must have a statically known size");
|
|
|
|
}
|
|
|
|
ObligationCauseCode::StructInitializerSized => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2014-12-06 11:39:25 -05:00
|
|
|
cause_span,
|
|
|
|
"structs must have a statically known size to be initialized");
|
|
|
|
}
|
2015-09-13 22:42:21 +03:00
|
|
|
ObligationCauseCode::ClosureCapture(var_id, _, builtin_bound) => {
|
2014-12-06 11:39:25 -05:00
|
|
|
let def_id = tcx.lang_items.from_builtin_kind(builtin_bound).unwrap();
|
2015-06-25 23:42:17 +03:00
|
|
|
let trait_name = tcx.item_path_str(def_id);
|
|
|
|
let name = tcx.local_var_name_str(var_id);
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-09-13 22:42:21 +03:00
|
|
|
cause_span,
|
|
|
|
&format!("the closure that captures `{}` requires that all captured variables \
|
|
|
|
implement the trait `{}`",
|
|
|
|
name,
|
|
|
|
trait_name));
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::FieldSized => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-09-13 22:42:21 +03:00
|
|
|
cause_span,
|
|
|
|
"only the last field of a struct or enum variant \
|
|
|
|
may have a dynamically sized type");
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::SharedStatic => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-09-13 22:42:21 +03:00
|
|
|
cause_span,
|
|
|
|
"shared static variables must have a type that implements `Sync`");
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
|
|
|
|
let parent_trait_ref = infcx.resolve_type_vars_if_possible(&data.parent_trait_ref);
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-09-13 22:42:21 +03:00
|
|
|
cause_span,
|
|
|
|
&format!("required because it appears within the type `{}`",
|
|
|
|
parent_trait_ref.0.self_ty()));
|
2015-06-23 11:50:50 -07:00
|
|
|
let parent_predicate = parent_trait_ref.to_predicate();
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause_code(infcx,
|
|
|
|
err,
|
|
|
|
&parent_predicate,
|
|
|
|
cause_span,
|
|
|
|
&*data.parent_code);
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
ObligationCauseCode::ImplDerivedObligation(ref data) => {
|
|
|
|
let parent_trait_ref = infcx.resolve_type_vars_if_possible(&data.parent_trait_ref);
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-09-13 22:42:21 +03:00
|
|
|
cause_span,
|
|
|
|
&format!("required because of the requirements on the impl of `{}` for `{}`",
|
|
|
|
parent_trait_ref,
|
|
|
|
parent_trait_ref.0.self_ty()));
|
2015-06-23 11:50:50 -07:00
|
|
|
let parent_predicate = parent_trait_ref.to_predicate();
|
2015-12-21 10:00:43 +13:00
|
|
|
note_obligation_cause_code(infcx,
|
|
|
|
err,
|
|
|
|
&parent_predicate,
|
|
|
|
cause_span,
|
|
|
|
&*data.parent_code);
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2015-01-14 13:43:17 -08:00
|
|
|
ObligationCauseCode::CompareImplMethodObligation => {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2015-09-13 22:42:21 +03:00
|
|
|
cause_span,
|
|
|
|
&format!("the requirement `{}` appears on the impl method \
|
|
|
|
but not on the corresponding trait method",
|
|
|
|
predicate));
|
2015-01-14 13:43:17 -08:00
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
fn suggest_new_overflow_limit(tcx: &ty::ctxt, err:&mut DiagnosticBuilder, span: Span) {
|
2015-01-02 04:01:30 -05:00
|
|
|
let current_limit = tcx.sess.recursion_limit.get();
|
2014-12-30 17:42:02 -05:00
|
|
|
let suggested_limit = current_limit * 2;
|
2015-12-21 10:00:43 +13:00
|
|
|
err.fileline_note(
|
2014-12-30 17:42:02 -05:00
|
|
|
span,
|
2015-01-07 11:58:31 -05:00
|
|
|
&format!(
|
2014-12-30 17:42:02 -05:00
|
|
|
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
|
2015-02-20 14:08:14 -05:00
|
|
|
suggested_limit));
|
2014-12-30 17:42:02 -05:00
|
|
|
}
|