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,
|
2016-03-29 20:12:31 +03:00
|
|
|
ObligationCause,
|
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,
|
2016-03-29 20:12:31 +03:00
|
|
|
SelectionContext,
|
2014-12-17 14:16:28 -05:00
|
|
|
SelectionError,
|
2015-04-15 11:57:29 +12:00
|
|
|
ObjectSafetyViolation,
|
|
|
|
MethodViolationCode,
|
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};
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def_id::DefId;
|
2016-03-22 17:30:57 +02:00
|
|
|
use infer::InferCtxt;
|
2016-03-29 20:12:31 +03:00
|
|
|
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt};
|
2016-03-22 17:30:57 +02:00
|
|
|
use ty::fast_reject;
|
2016-03-29 20:12:31 +03:00
|
|
|
use ty::fold::{TypeFoldable, TypeFolder};
|
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};
|
2016-05-01 09:59:28 +03:00
|
|
|
use syntax::ast;
|
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,
|
2016-05-01 09:59:28 +03:00
|
|
|
warning_node_id: Option<ast::NodeId>,
|
2015-09-24 19:58:00 +03:00
|
|
|
predicate: ty::Predicate<'tcx>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TraitErrorKey<'tcx> {
|
2016-05-03 05:23:22 +03:00
|
|
|
fn from_error<'a>(infcx: &InferCtxt<'a, 'tcx, 'tcx>,
|
2016-05-01 09:59:28 +03:00
|
|
|
e: &FulfillmentError<'tcx>,
|
|
|
|
warning_node_id: Option<ast::NodeId>) -> Self {
|
2015-09-24 19:58:00 +03:00
|
|
|
let predicate =
|
|
|
|
infcx.resolve_type_vars_if_possible(&e.obligation.predicate);
|
|
|
|
TraitErrorKey {
|
|
|
|
span: e.obligation.cause.span,
|
2016-05-01 09:59:28 +03:00
|
|
|
predicate: infcx.tcx.erase_regions(&predicate),
|
|
|
|
warning_node_id: warning_node_id
|
2015-09-24 19:58:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'tcx> InferCtxt<'a, 'tcx, 'tcx> {
|
2016-03-17 00:15:31 +02:00
|
|
|
pub fn report_fulfillment_errors(&self, errors: &Vec<FulfillmentError<'tcx>>) {
|
2015-01-31 12:20:46 -05:00
|
|
|
for error in errors {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.report_fulfillment_error(error, None);
|
2016-05-01 09:59:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
pub fn report_fulfillment_errors_as_warnings(&self,
|
|
|
|
errors: &Vec<FulfillmentError<'tcx>>,
|
|
|
|
node_id: ast::NodeId) {
|
2016-05-01 09:59:28 +03:00
|
|
|
for error in errors {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.report_fulfillment_error(error, Some(node_id));
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
fn report_fulfillment_error(&self,
|
|
|
|
error: &FulfillmentError<'tcx>,
|
|
|
|
warning_node_id: Option<ast::NodeId>) {
|
|
|
|
let error_key = TraitErrorKey::from_error(self, error, warning_node_id);
|
2015-09-24 19:58:00 +03:00
|
|
|
debug!("report_fulfillment_errors({:?}) - key={:?}",
|
|
|
|
error, error_key);
|
2016-03-17 00:15:31 +02:00
|
|
|
if !self.reported_trait_errors.borrow_mut().insert(error_key) {
|
2015-09-24 19:58:00 +03:00
|
|
|
debug!("report_fulfillment_errors: skipping duplicate");
|
|
|
|
return;
|
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
match error.code {
|
|
|
|
FulfillmentErrorCode::CodeSelectionError(ref e) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.report_selection_error(&error.obligation, e, warning_node_id);
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
FulfillmentErrorCode::CodeProjectionError(ref e) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.report_projection_error(&error.obligation, e, warning_node_id);
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
FulfillmentErrorCode::CodeAmbiguity => {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.maybe_report_ambiguity(&error.obligation);
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
fn report_projection_error(&self,
|
|
|
|
obligation: &PredicateObligation<'tcx>,
|
|
|
|
error: &MismatchedProjectionTypes<'tcx>,
|
|
|
|
warning_node_id: Option<ast::NodeId>)
|
2014-12-17 14:16:28 -05:00
|
|
|
{
|
|
|
|
let predicate =
|
2016-03-17 00:15:31 +02:00
|
|
|
self.resolve_type_vars_if_possible(&obligation.predicate);
|
2015-08-07 10:28:51 -04:00
|
|
|
|
2016-03-29 19:10:39 +03:00
|
|
|
if !predicate.references_error() {
|
2016-05-01 09:59:28 +03:00
|
|
|
if let Some(warning_node_id) = warning_node_id {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.sess.add_lint(
|
2016-05-01 09:59:28 +03:00
|
|
|
::lint::builtin::UNSIZED_IN_TUPLE,
|
|
|
|
warning_node_id,
|
|
|
|
obligation.cause.span,
|
|
|
|
format!("type mismatch resolving `{}`: {}",
|
|
|
|
predicate,
|
|
|
|
error.err));
|
|
|
|
} else {
|
2016-03-17 00:15:31 +02:00
|
|
|
let mut err = struct_span_err!(self.tcx.sess, obligation.cause.span, E0271,
|
2016-05-01 09:59:28 +03:00
|
|
|
"type mismatch resolving `{}`: {}",
|
|
|
|
predicate,
|
|
|
|
error.err);
|
2016-03-17 00:15:31 +02:00
|
|
|
self.note_obligation_cause(&mut err, obligation);
|
2016-05-01 09:59:28 +03:00
|
|
|
err.emit();
|
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
fn on_unimplemented_note(&self,
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
span: Span) -> Option<String> {
|
2016-03-29 20:12:31 +03:00
|
|
|
let trait_ref = trait_ref.skip_binder();
|
2015-01-10 23:51:27 +05:30
|
|
|
let def_id = trait_ref.def_id;
|
|
|
|
let mut report = None;
|
2016-03-17 00:15:31 +02:00
|
|
|
for item in self.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);
|
2016-03-17 00:15:31 +02:00
|
|
|
let def = self.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 => {
|
2016-03-17 00:15:31 +02:00
|
|
|
span_err!(self.tcx.sess, err_sp, E0272,
|
2015-01-18 16:58:25 -08:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
2016-03-17 00:15:31 +02:00
|
|
|
span_err!(self.tcx.sess, err_sp, E0273,
|
2015-01-18 16:58:25 -08:00
|
|
|
"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 {
|
2016-03-17 00:15:31 +02:00
|
|
|
span_err!(self.tcx.sess, err_sp, E0274,
|
2015-01-18 16:58:25 -08:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
fn report_similar_impl_candidates(&self,
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
err: &mut DiagnosticBuilder)
|
2016-04-05 23:19:29 +03:00
|
|
|
{
|
2016-03-17 00:15:31 +02:00
|
|
|
let simp = fast_reject::simplify_type(self.tcx,
|
2016-04-05 23:19:29 +03:00
|
|
|
trait_ref.skip_binder().self_ty(),
|
|
|
|
true);
|
|
|
|
let mut impl_candidates = Vec::new();
|
2016-03-17 00:15:31 +02:00
|
|
|
let trait_def = self.tcx.lookup_trait_def(trait_ref.def_id());
|
2016-04-05 23:19:29 +03:00
|
|
|
|
|
|
|
match simp {
|
2016-03-17 00:15:31 +02:00
|
|
|
Some(simp) => trait_def.for_each_impl(self.tcx, |def_id| {
|
|
|
|
let imp = self.tcx.impl_trait_ref(def_id).unwrap();
|
|
|
|
let imp_simp = fast_reject::simplify_type(self.tcx,
|
2016-04-05 23:19:29 +03:00
|
|
|
imp.self_ty(),
|
|
|
|
true);
|
|
|
|
if let Some(imp_simp) = imp_simp {
|
|
|
|
if simp != imp_simp {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl_candidates.push(imp);
|
|
|
|
}),
|
2016-03-17 00:15:31 +02:00
|
|
|
None => trait_def.for_each_impl(self.tcx, |def_id| {
|
2016-04-05 23:19:29 +03:00
|
|
|
impl_candidates.push(
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.impl_trait_ref(def_id).unwrap());
|
2016-04-05 23:19:29 +03:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
if impl_candidates.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-20 14:49:16 -04:00
|
|
|
err.help(&format!("the following implementations were found:"));
|
2016-04-05 23:19:29 +03:00
|
|
|
|
|
|
|
let end = cmp::min(4, impl_candidates.len());
|
|
|
|
for candidate in &impl_candidates[0..end] {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.help(&format!(" {:?}", candidate));
|
2016-04-05 23:19:29 +03:00
|
|
|
}
|
|
|
|
if impl_candidates.len() > 4 {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.help(&format!("and {} others", impl_candidates.len()-4));
|
2016-04-05 23:19:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2016-03-17 00:15:31 +02:00
|
|
|
pub fn report_overflow_error<T>(&self,
|
|
|
|
obligation: &Obligation<'tcx, T>,
|
|
|
|
suggest_increasing_limit: bool) -> !
|
2015-11-18 09:38:57 +00:00
|
|
|
where T: fmt::Display + TypeFoldable<'tcx>
|
2015-03-20 06:48:40 -04:00
|
|
|
{
|
|
|
|
let predicate =
|
2016-03-17 00:15:31 +02:00
|
|
|
self.resolve_type_vars_if_possible(&obligation.predicate);
|
|
|
|
let mut err = struct_span_err!(self.tcx.sess, obligation.cause.span, E0275,
|
2015-12-21 10:00:43 +13:00
|
|
|
"overflow evaluating the requirement `{}`",
|
|
|
|
predicate);
|
2015-03-20 06:48:40 -04:00
|
|
|
|
2016-01-08 21:41:37 -05:00
|
|
|
if suggest_increasing_limit {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.suggest_new_overflow_limit(&mut err);
|
2016-01-08 21:41:37 -05:00
|
|
|
}
|
2015-03-20 06:48:40 -04:00
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
self.note_obligation_cause(&mut err, obligation);
|
2015-03-20 06:48:40 -04:00
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
err.emit();
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.sess.abort_if_errors();
|
2016-03-25 18:46:11 +01:00
|
|
|
bug!();
|
2015-03-20 06:48:40 -04:00
|
|
|
}
|
|
|
|
|
2016-01-08 21:41:37 -05:00
|
|
|
/// Reports that a cycle was detected which led to overflow and halts
|
|
|
|
/// compilation. This is equivalent to `report_overflow_error` except
|
|
|
|
/// that we can give a more helpful error message (and, in particular,
|
|
|
|
/// we do not suggest increasing the overflow limit, which is not
|
|
|
|
/// going to help).
|
2016-03-17 00:15:31 +02:00
|
|
|
pub fn report_overflow_error_cycle(&self, cycle: &Vec<PredicateObligation<'tcx>>) -> ! {
|
2016-01-08 21:41:37 -05:00
|
|
|
assert!(cycle.len() > 1);
|
|
|
|
|
|
|
|
debug!("report_overflow_error_cycle(cycle length = {})", cycle.len());
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
let cycle = self.resolve_type_vars_if_possible(cycle);
|
2016-01-08 21:41:37 -05:00
|
|
|
|
|
|
|
debug!("report_overflow_error_cycle: cycle={:?}", cycle);
|
|
|
|
|
|
|
|
assert_eq!(&cycle[0].predicate, &cycle.last().unwrap().predicate);
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
self.try_report_overflow_error_type_of_infinite_size(&cycle);
|
|
|
|
self.report_overflow_error(&cycle[0], false);
|
2016-01-08 21:41:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If a cycle results from evaluated whether something is Sized, that
|
|
|
|
/// is a particular special case that always results from a struct or
|
|
|
|
/// enum definition that lacks indirection (e.g., `struct Foo { x: Foo
|
|
|
|
/// }`). We wish to report a targeted error for this case.
|
2016-03-17 00:15:31 +02:00
|
|
|
pub fn try_report_overflow_error_type_of_infinite_size(&self,
|
2016-01-08 21:41:37 -05:00
|
|
|
cycle: &[PredicateObligation<'tcx>])
|
|
|
|
{
|
2016-03-17 00:15:31 +02:00
|
|
|
let sized_trait = match self.tcx.lang_items.sized_trait() {
|
2016-01-08 21:41:37 -05:00
|
|
|
Some(v) => v,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
let top_is_sized = {
|
|
|
|
match cycle[0].predicate {
|
|
|
|
ty::Predicate::Trait(ref data) => data.def_id() == sized_trait,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !top_is_sized {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The only way to have a type of infinite size is to have,
|
|
|
|
// somewhere, a struct/enum type involved. Identify all such types
|
|
|
|
// and report the cycle to the user.
|
|
|
|
|
|
|
|
let struct_enum_tys: Vec<_> =
|
|
|
|
cycle.iter()
|
|
|
|
.flat_map(|obligation| match obligation.predicate {
|
|
|
|
ty::Predicate::Trait(ref data) => {
|
|
|
|
assert_eq!(data.def_id(), sized_trait);
|
|
|
|
let self_ty = data.skip_binder().trait_ref.self_ty(); // (*)
|
|
|
|
// (*) ok to skip binder because this is just
|
|
|
|
// error reporting and regions don't really
|
|
|
|
// matter
|
|
|
|
match self_ty.sty {
|
|
|
|
ty::TyEnum(..) | ty::TyStruct(..) => Some(self_ty),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2016-03-25 18:31:27 +01:00
|
|
|
span_bug!(obligation.cause.span,
|
|
|
|
"Sized cycle involving non-trait-ref: {:?}",
|
|
|
|
obligation.predicate);
|
2016-01-08 21:41:37 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert!(!struct_enum_tys.is_empty());
|
|
|
|
|
|
|
|
// This is a bit tricky. We want to pick a "main type" in the
|
|
|
|
// listing that is local to the current crate, so we can give a
|
|
|
|
// good span to the user. But it might not be the first one in our
|
|
|
|
// cycle list. So find the first one that is local and then
|
|
|
|
// rotate.
|
|
|
|
let (main_index, main_def_id) =
|
|
|
|
struct_enum_tys.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, ty)| match ty.sty {
|
2016-01-11 15:56:21 -05:00
|
|
|
ty::TyEnum(adt_def, _) | ty::TyStruct(adt_def, _)
|
|
|
|
if adt_def.did.is_local() =>
|
2016-01-08 21:41:37 -05:00
|
|
|
Some((index, adt_def.did)),
|
|
|
|
_ =>
|
|
|
|
None,
|
|
|
|
})
|
|
|
|
.next()
|
|
|
|
.unwrap(); // should always be SOME local type involved!
|
|
|
|
|
|
|
|
// Rotate so that the "main" type is at index 0.
|
|
|
|
let struct_enum_tys: Vec<_> =
|
|
|
|
struct_enum_tys.iter()
|
|
|
|
.cloned()
|
|
|
|
.skip(main_index)
|
|
|
|
.chain(struct_enum_tys.iter().cloned().take(main_index))
|
|
|
|
.collect();
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
let tcx = self.tcx;
|
|
|
|
let mut err = tcx.recursive_type_with_infinite_size_error(main_def_id);
|
2016-01-08 21:41:37 -05:00
|
|
|
let len = struct_enum_tys.len();
|
|
|
|
if len > 2 {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&format!("type `{}` is embedded within `{}`...",
|
|
|
|
struct_enum_tys[0],
|
|
|
|
struct_enum_tys[1]));
|
2016-01-08 21:41:37 -05:00
|
|
|
for &next_ty in &struct_enum_tys[1..len-1] {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&format!("...which in turn is embedded within `{}`...", next_ty));
|
2016-01-08 21:41:37 -05:00
|
|
|
}
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&format!("...which in turn is embedded within `{}`, \
|
|
|
|
completing the cycle.",
|
|
|
|
struct_enum_tys[len-1]));
|
2016-01-08 21:41:37 -05:00
|
|
|
}
|
|
|
|
err.emit();
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.sess.abort_if_errors();
|
2016-03-25 18:46:11 +01:00
|
|
|
bug!();
|
2016-01-08 21:41:37 -05:00
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
pub fn report_selection_error(&self,
|
|
|
|
obligation: &PredicateObligation<'tcx>,
|
|
|
|
error: &SelectionError<'tcx>,
|
|
|
|
warning_node_id: Option<ast::NodeId>)
|
2016-01-08 21:41:37 -05:00
|
|
|
{
|
2016-03-17 00:15:31 +02:00
|
|
|
let span = obligation.cause.span;
|
|
|
|
let mut err = 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!(
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.sess, 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);
|
2016-03-17 00:15:31 +02:00
|
|
|
return;
|
2015-08-07 10:28:51 -04:00
|
|
|
} else {
|
|
|
|
match obligation.predicate {
|
|
|
|
ty::Predicate::Trait(ref trait_predicate) => {
|
|
|
|
let trait_predicate =
|
2016-03-17 00:15:31 +02:00
|
|
|
self.resolve_type_vars_if_possible(trait_predicate);
|
2015-08-07 10:28:51 -04:00
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
if self.tcx.sess.has_errors() && trait_predicate.references_error() {
|
|
|
|
return;
|
|
|
|
} else {
|
2015-08-07 10:28:51 -04:00
|
|
|
let trait_ref = trait_predicate.to_poly_trait_ref();
|
2016-05-01 09:59:28 +03:00
|
|
|
|
|
|
|
if let Some(warning_node_id) = warning_node_id {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.sess.add_lint(
|
2016-05-01 09:59:28 +03:00
|
|
|
::lint::builtin::UNSIZED_IN_TUPLE,
|
|
|
|
warning_node_id,
|
|
|
|
obligation.cause.span,
|
|
|
|
format!("the trait bound `{}` is not satisfied",
|
|
|
|
trait_ref.to_predicate()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
let mut err = struct_span_err!(
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.sess, span, E0277,
|
2016-03-31 21:42:23 +03:00
|
|
|
"the trait bound `{}` is not satisfied",
|
2016-03-29 20:12:31 +03:00
|
|
|
trait_ref.to_predicate());
|
|
|
|
|
2016-04-05 23:19:29 +03:00
|
|
|
// Try to report a help message
|
2016-03-29 20:12:31 +03:00
|
|
|
|
|
|
|
if !trait_ref.has_infer_types() &&
|
2016-03-17 00:15:31 +02:00
|
|
|
self.predicate_can_apply(trait_ref)
|
2016-03-29 20:12:31 +03:00
|
|
|
{
|
2016-04-05 23:19:29 +03:00
|
|
|
// If a where-clause may be useful, remind the
|
|
|
|
// user that they can add it.
|
|
|
|
//
|
|
|
|
// don't display an on-unimplemented note, as
|
|
|
|
// these notes will often be of the form
|
|
|
|
// "the type `T` can't be frobnicated"
|
|
|
|
// which is somewhat confusing.
|
2016-04-20 14:49:16 -04:00
|
|
|
err.help(&format!("consider adding a `where {}` bound",
|
2016-03-29 20:12:31 +03:00
|
|
|
trait_ref.to_predicate()
|
|
|
|
));
|
2016-03-17 00:15:31 +02:00
|
|
|
} else if let Some(s) = self.on_unimplemented_note(trait_ref, span) {
|
2016-04-05 23:19:29 +03:00
|
|
|
// Otherwise, if there is an on-unimplemented note,
|
|
|
|
// display it.
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&s);
|
2015-11-28 11:41:52 +01:00
|
|
|
} else {
|
2016-04-05 23:19:29 +03:00
|
|
|
// If we can't show anything useful, try to find
|
|
|
|
// similar impls.
|
2015-12-17 23:58:56 +01:00
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
self.report_similar_impl_candidates(trait_ref, &mut err);
|
2015-01-14 13:43:17 -08:00
|
|
|
}
|
2016-03-17 00:15:31 +02:00
|
|
|
err
|
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) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let predicate = self.resolve_type_vars_if_possible(predicate);
|
|
|
|
let err = self.equality_predicate(span,
|
|
|
|
&predicate).err().unwrap();
|
|
|
|
struct_span_err!(self.tcx.sess, span, E0278,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the requirement `{}` is not satisfied (`{}`)",
|
2016-03-17 00:15:31 +02:00
|
|
|
predicate, err)
|
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) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let predicate = self.resolve_type_vars_if_possible(predicate);
|
|
|
|
let err = self.region_outlives_predicate(span,
|
|
|
|
&predicate).err().unwrap();
|
|
|
|
struct_span_err!(self.tcx.sess, span, E0279,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the requirement `{}` is not satisfied (`{}`)",
|
2016-03-17 00:15:31 +02:00
|
|
|
predicate, err)
|
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 =
|
2016-03-17 00:15:31 +02:00
|
|
|
self.resolve_type_vars_if_possible(&obligation.predicate);
|
|
|
|
struct_span_err!(self.tcx.sess, span, E0280,
|
2015-08-07 10:28:51 -04:00
|
|
|
"the requirement `{}` is not satisfied",
|
2016-03-17 00:15:31 +02:00
|
|
|
predicate)
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::Predicate::ObjectSafe(trait_def_id) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let violations = self.tcx.object_safety_violations(trait_def_id);
|
|
|
|
let err = self.tcx.report_object_safety_error(span,
|
|
|
|
trait_def_id,
|
|
|
|
warning_node_id,
|
|
|
|
violations);
|
|
|
|
if let Some(err) = err {
|
|
|
|
err
|
|
|
|
} else {
|
|
|
|
return;
|
2016-05-01 09:59:28 +03:00
|
|
|
}
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
|
2016-04-06 00:20:59 -07:00
|
|
|
ty::Predicate::ClosureKind(closure_def_id, kind) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let found_kind = self.closure_kind(closure_def_id).unwrap();
|
|
|
|
let closure_span = self.tcx.map.span_if_local(closure_def_id).unwrap();
|
2016-04-06 00:20:59 -07:00
|
|
|
let mut err = struct_span_err!(
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.sess, closure_span, E0525,
|
2016-04-13 07:48:53 -07:00
|
|
|
"expected a closure that implements the `{}` trait, but this closure \
|
|
|
|
only implements `{}`",
|
|
|
|
kind,
|
|
|
|
found_kind);
|
2016-04-06 00:20:59 -07:00
|
|
|
err.span_note(
|
|
|
|
obligation.cause.span,
|
|
|
|
&format!("the requirement to implement `{}` derives from here", kind));
|
|
|
|
err.emit();
|
2016-03-17 00:15:31 +02:00
|
|
|
return;
|
2016-04-06 00:20:59 -07:00
|
|
|
}
|
|
|
|
|
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).
|
2016-03-17 00:15:31 +02:00
|
|
|
span_bug!(span, "WF predicate not satisfied for {:?}", ty);
|
2015-01-14 13:43:17 -08:00
|
|
|
}
|
2016-05-01 09:59:28 +03:00
|
|
|
|
|
|
|
ty::Predicate::Rfc1592(ref data) => {
|
|
|
|
span_bug!(
|
|
|
|
obligation.cause.span,
|
|
|
|
"RFC1592 predicate not satisfied for {:?}",
|
|
|
|
data);
|
|
|
|
}
|
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) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let expected_trait_ref = self.resolve_type_vars_if_possible(&*expected_trait_ref);
|
|
|
|
let actual_trait_ref = self.resolve_type_vars_if_possible(&*actual_trait_ref);
|
|
|
|
if actual_trait_ref.self_ty().references_error() {
|
|
|
|
return;
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2016-03-17 00:15:31 +02:00
|
|
|
struct_span_err!(self.tcx.sess, span, E0281,
|
|
|
|
"type mismatch: the type `{}` implements the trait `{}`, \
|
|
|
|
but the trait `{}` is required ({})",
|
|
|
|
expected_trait_ref.self_ty(),
|
|
|
|
expected_trait_ref,
|
|
|
|
actual_trait_ref,
|
|
|
|
e)
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2015-04-15 11:57:29 +12:00
|
|
|
|
|
|
|
TraitNotObjectSafe(did) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let violations = self.tcx.object_safety_violations(did);
|
|
|
|
let err = self.tcx.report_object_safety_error(span, did,
|
|
|
|
warning_node_id,
|
|
|
|
violations);
|
|
|
|
if let Some(err) = err {
|
|
|
|
err
|
|
|
|
} else {
|
|
|
|
return;
|
2016-05-01 09:59:28 +03:00
|
|
|
}
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
2016-03-17 00:15:31 +02:00
|
|
|
};
|
|
|
|
self.note_obligation_cause(&mut err, obligation);
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn recursive_type_with_infinite_size_error(self,
|
2016-03-17 00:15:31 +02:00
|
|
|
type_def_id: DefId)
|
|
|
|
-> DiagnosticBuilder<'tcx>
|
|
|
|
{
|
|
|
|
assert!(type_def_id.is_local());
|
|
|
|
let span = self.map.span_if_local(type_def_id).unwrap();
|
|
|
|
let mut err = struct_span_err!(self.sess, span, E0072, "recursive type `{}` has infinite size",
|
|
|
|
self.item_path_str(type_def_id));
|
|
|
|
err.help(&format!("insert indirection (e.g., a `Box`, `Rc`, or `&`) \
|
|
|
|
at some point to make `{}` representable",
|
|
|
|
self.item_path_str(type_def_id)));
|
|
|
|
err
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
2015-04-15 11:57:29 +12:00
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn report_object_safety_error(self,
|
2016-03-17 00:15:31 +02:00
|
|
|
span: Span,
|
|
|
|
trait_def_id: DefId,
|
|
|
|
warning_node_id: Option<ast::NodeId>,
|
|
|
|
violations: Vec<ObjectSafetyViolation>)
|
|
|
|
-> Option<DiagnosticBuilder<'tcx>>
|
2015-08-07 10:28:51 -04:00
|
|
|
{
|
2016-05-01 09:59:28 +03:00
|
|
|
let mut err = match warning_node_id {
|
|
|
|
Some(_) => None,
|
|
|
|
None => {
|
|
|
|
Some(struct_span_err!(
|
2016-03-17 00:15:31 +02:00
|
|
|
self.sess, span, E0038,
|
2016-05-01 09:59:28 +03:00
|
|
|
"the trait `{}` cannot be made into an object",
|
2016-03-17 00:15:31 +02:00
|
|
|
self.item_path_str(trait_def_id)))
|
2016-05-01 09:59:28 +03:00
|
|
|
}
|
|
|
|
};
|
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;
|
|
|
|
}
|
2016-05-01 09:59:28 +03:00
|
|
|
let buf;
|
|
|
|
let note = match violation {
|
2015-08-07 10:28:51 -04:00
|
|
|
ObjectSafetyViolation::SizedSelf => {
|
2016-05-01 09:59:28 +03:00
|
|
|
"the trait cannot require that `Self : Sized`"
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
2015-04-15 11:57:29 +12:00
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
ObjectSafetyViolation::SupertraitSelf => {
|
2016-05-01 09:59:28 +03:00
|
|
|
"the trait cannot use `Self` as a type parameter \
|
|
|
|
in the supertrait listing"
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
2015-04-15 11:57:29 +12:00
|
|
|
|
2015-08-07 10:28:51 -04:00
|
|
|
ObjectSafetyViolation::Method(method,
|
|
|
|
MethodViolationCode::StaticMethod) => {
|
2016-05-01 09:59:28 +03:00
|
|
|
buf = format!("method `{}` has no receiver",
|
|
|
|
method.name);
|
|
|
|
&buf
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectSafetyViolation::Method(method,
|
|
|
|
MethodViolationCode::ReferencesSelf) => {
|
2016-05-01 09:59:28 +03:00
|
|
|
buf = format!("method `{}` references the `Self` type \
|
2016-04-20 14:49:16 -04:00
|
|
|
in its arguments or return type",
|
2016-05-01 09:59:28 +03:00
|
|
|
method.name);
|
|
|
|
&buf
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectSafetyViolation::Method(method,
|
|
|
|
MethodViolationCode::Generic) => {
|
2016-05-01 09:59:28 +03:00
|
|
|
buf = format!("method `{}` has generic type parameters",
|
|
|
|
method.name);
|
|
|
|
&buf
|
2015-04-15 11:57:29 +12:00
|
|
|
}
|
2016-05-01 09:59:28 +03:00
|
|
|
};
|
|
|
|
match (warning_node_id, &mut err) {
|
|
|
|
(Some(node_id), &mut None) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
self.sess.add_lint(
|
2016-05-01 09:59:28 +03:00
|
|
|
::lint::builtin::OBJECT_UNSAFE_FRAGMENT,
|
|
|
|
node_id,
|
|
|
|
span,
|
|
|
|
note.to_string());
|
|
|
|
}
|
|
|
|
(None, &mut Some(ref mut err)) => {
|
|
|
|
err.note(note);
|
|
|
|
}
|
|
|
|
_ => unreachable!()
|
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
|
|
|
}
|
2016-03-17 00:15:31 +02:00
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
impl<'a, 'tcx> InferCtxt<'a, 'tcx, 'tcx> {
|
2016-03-17 00:15:31 +02:00
|
|
|
fn maybe_report_ambiguity(&self, 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.
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
let predicate = self.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
|
|
|
|
2016-03-15 04:49:10 -04:00
|
|
|
// Ambiguity errors are often caused as fallout from earlier
|
|
|
|
// errors. So just ignore them if this infcx is tainted.
|
2016-03-17 00:15:31 +02:00
|
|
|
if self.is_tainted_by_errors() {
|
2016-03-15 04:49:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.
|
2016-03-17 00:15:31 +02:00
|
|
|
if !self.tcx.sess.has_errors() {
|
2014-12-30 17:42:02 -05:00
|
|
|
if
|
2016-03-17 00:15:31 +02:00
|
|
|
self.tcx.lang_items.sized_trait()
|
2014-12-30 17:42:02 -05:00
|
|
|
.map_or(false, |sized_id| sized_id == trait_ref.def_id())
|
|
|
|
{
|
2016-03-17 00:15:31 +02:00
|
|
|
self.need_type_info(obligation.cause.span, self_ty);
|
2014-12-30 17:42:02 -05:00
|
|
|
} else {
|
2016-03-17 00:15:31 +02:00
|
|
|
let mut err = struct_span_err!(self.tcx.sess, obligation.cause.span, E0283,
|
2015-12-21 10:00:43 +13:00
|
|
|
"type annotations required: \
|
|
|
|
cannot resolve `{}`",
|
|
|
|
predicate);
|
2016-03-17 00:15:31 +02:00
|
|
|
self.note_obligation_cause(&mut err, obligation);
|
2015-12-21 10:00:43 +13:00
|
|
|
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.
|
2016-03-17 00:15:31 +02:00
|
|
|
if !ty.references_error() && !self.tcx.sess.has_errors() {
|
|
|
|
self.need_type_info(obligation.cause.span, ty);
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 17:42:02 -05:00
|
|
|
_ => {
|
2016-03-17 00:15:31 +02:00
|
|
|
if !self.tcx.sess.has_errors() {
|
|
|
|
let mut err = struct_span_err!(self.tcx.sess, obligation.cause.span, E0284,
|
2015-12-21 10:00:43 +13:00
|
|
|
"type annotations required: cannot resolve `{}`",
|
|
|
|
predicate);
|
2016-03-17 00:15:31 +02:00
|
|
|
self.note_obligation_cause(&mut err, obligation);
|
2015-12-21 10:00:43 +13:00
|
|
|
err.emit();
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 20:12:31 +03:00
|
|
|
/// Returns whether the trait predicate may apply for *some* assignment
|
|
|
|
/// to the type parameters.
|
2016-03-17 00:15:31 +02:00
|
|
|
fn predicate_can_apply(&self, pred: ty::PolyTraitRef<'tcx>) -> bool {
|
2016-03-29 20:12:31 +03:00
|
|
|
struct ParamToVarFolder<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
infcx: &'a InferCtxt<'a, 'tcx, 'tcx>,
|
2016-03-29 20:12:31 +03:00
|
|
|
var_map: FnvHashMap<Ty<'tcx>, Ty<'tcx>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for ParamToVarFolder<'a, 'tcx>
|
|
|
|
{
|
2016-05-03 05:23:22 +03:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> { self.infcx.tcx }
|
2016-03-29 20:12:31 +03:00
|
|
|
|
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
if let ty::TyParam(..) = ty.sty {
|
|
|
|
let infcx = self.infcx;
|
|
|
|
self.var_map.entry(ty).or_insert_with(|| infcx.next_ty_var())
|
|
|
|
} else {
|
|
|
|
ty.super_fold_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
self.probe(|_| {
|
|
|
|
let mut selcx = SelectionContext::new(self);
|
2016-03-29 20:12:31 +03:00
|
|
|
|
|
|
|
let cleaned_pred = pred.fold_with(&mut ParamToVarFolder {
|
2016-03-17 00:15:31 +02:00
|
|
|
infcx: self,
|
2016-03-29 20:12:31 +03:00
|
|
|
var_map: FnvHashMap()
|
|
|
|
});
|
|
|
|
|
|
|
|
let cleaned_pred = super::project::normalize(
|
|
|
|
&mut selcx,
|
|
|
|
ObligationCause::dummy(),
|
|
|
|
&cleaned_pred
|
|
|
|
).value;
|
|
|
|
|
|
|
|
let obligation = Obligation::new(
|
|
|
|
ObligationCause::dummy(),
|
|
|
|
cleaned_pred.to_predicate()
|
|
|
|
);
|
|
|
|
|
|
|
|
selcx.evaluate_obligation(&obligation)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
fn need_type_info(&self, span: Span, ty: Ty<'tcx>) {
|
|
|
|
span_err!(self.tcx.sess, span, E0282,
|
2015-08-07 10:28:51 -04:00
|
|
|
"unable to infer enough type information about `{}`; \
|
|
|
|
type annotations or generic parameter binding required",
|
|
|
|
ty);
|
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
fn note_obligation_cause<T>(&self,
|
|
|
|
err: &mut DiagnosticBuilder,
|
|
|
|
obligation: &Obligation<'tcx, T>)
|
2015-06-18 20:25:05 +03:00
|
|
|
where T: fmt::Display
|
2014-12-06 11:39:25 -05:00
|
|
|
{
|
2016-03-17 00:15:31 +02:00
|
|
|
self.note_obligation_cause_code(err,
|
|
|
|
&obligation.predicate,
|
|
|
|
&obligation.cause.code);
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
fn note_obligation_cause_code<T>(&self,
|
|
|
|
err: &mut DiagnosticBuilder,
|
|
|
|
predicate: &T,
|
|
|
|
cause_code: &ObligationCauseCode<'tcx>)
|
2015-06-18 20:25:05 +03:00
|
|
|
where T: fmt::Display
|
2014-12-06 11:39:25 -05:00
|
|
|
{
|
2016-03-17 00:15:31 +02:00
|
|
|
let tcx = self.tcx;
|
2014-12-06 11:39:25 -05:00
|
|
|
match *cause_code {
|
|
|
|
ObligationCauseCode::MiscObligation => { }
|
2015-08-07 10:28:51 -04:00
|
|
|
ObligationCauseCode::SliceOrArrayElem => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note("slice and array elements must have `Sized` type");
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
2016-04-22 01:46:23 +03:00
|
|
|
ObligationCauseCode::TupleElem => {
|
2016-05-01 09:59:28 +03:00
|
|
|
err.note("tuple elements must have `Sized` type");
|
2016-04-22 01:46:23 +03:00
|
|
|
}
|
2015-08-07 10:28:51 -04:00
|
|
|
ObligationCauseCode::ProjectionWf(data) => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&format!("required so that the projection `{}` is well-formed",
|
|
|
|
data));
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
|
|
|
ObligationCauseCode::ReferenceOutlivesReferent(ref_ty) => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&format!("required so that reference `{}` does not outlive its referent",
|
|
|
|
ref_ty));
|
2015-08-07 10:28:51 -04:00
|
|
|
}
|
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);
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&format!("required by `{}`", item_name));
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::ObjectCastObligation(object_ty) => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&format!("required for the cast to the object type `{}`",
|
2016-03-17 00:15:31 +02:00
|
|
|
self.ty_to_string(object_ty)));
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::RepeatVec => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note("the `Copy` trait is required because the \
|
|
|
|
repeated element will be copied");
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::VariableType(_) => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note("all local variables must have a statically known size");
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::ReturnType => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note("the return type of a function must have a \
|
|
|
|
statically known size");
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::AssignmentLhsSized => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note("the left-hand-side of an assignment must have a statically known size");
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
|
|
|
ObligationCauseCode::StructInitializerSized => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note("structs must have a statically known size to be initialized");
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
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);
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(
|
2015-09-13 22:42:21 +03:00
|
|
|
&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 => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note("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 => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note("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) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&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();
|
2016-03-17 00:15:31 +02:00
|
|
|
self.note_obligation_cause_code(err,
|
|
|
|
&parent_predicate,
|
|
|
|
&data.parent_code);
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
ObligationCauseCode::ImplDerivedObligation(ref data) => {
|
2016-03-17 00:15:31 +02:00
|
|
|
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(
|
2015-09-13 22:42:21 +03:00
|
|
|
&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();
|
2016-03-17 00:15:31 +02:00
|
|
|
self.note_obligation_cause_code(err,
|
|
|
|
&parent_predicate,
|
|
|
|
&data.parent_code);
|
2014-12-06 11:39:25 -05:00
|
|
|
}
|
2015-01-14 13:43:17 -08:00
|
|
|
ObligationCauseCode::CompareImplMethodObligation => {
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(
|
2015-09-13 22:42:21 +03:00
|
|
|
&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
|
|
|
|
2016-03-17 00:15:31 +02:00
|
|
|
fn suggest_new_overflow_limit(&self, err: &mut DiagnosticBuilder) {
|
|
|
|
let current_limit = self.tcx.sess.recursion_limit.get();
|
2014-12-30 17:42:02 -05:00
|
|
|
let suggested_limit = current_limit * 2;
|
2016-04-20 14:49:16 -04:00
|
|
|
err.note(&format!(
|
|
|
|
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
|
|
|
|
suggested_limit));
|
2014-12-30 17:42:02 -05:00
|
|
|
}
|
2016-03-17 00:15:31 +02:00
|
|
|
}
|