2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::def_id::DefId;
|
2019-02-20 05:39:04 -05:00
|
|
|
use crate::ty::{self, BoundRegion, Region, Ty, TyCtxt};
|
2018-10-01 15:31:27 +02:00
|
|
|
use std::borrow::Cow;
|
2015-09-06 21:51:58 +03:00
|
|
|
use std::fmt;
|
2018-04-25 19:30:39 +03:00
|
|
|
use rustc_target::spec::abi;
|
2017-07-11 10:33:09 -04:00
|
|
|
use syntax::ast;
|
2019-02-08 00:56:05 +09:00
|
|
|
use errors::{Applicability, DiagnosticBuilder};
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir;
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2018-11-19 10:26:04 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2015-09-06 21:51:58 +03:00
|
|
|
pub struct ExpectedFound<T> {
|
|
|
|
pub expected: T,
|
2017-03-13 02:12:13 +02:00
|
|
|
pub found: T,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Data structures used in type unification
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum TypeError<'tcx> {
|
|
|
|
Mismatch,
|
|
|
|
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
|
|
|
|
AbiMismatch(ExpectedFound<abi::Abi>),
|
|
|
|
Mutability,
|
|
|
|
TupleSize(ExpectedFound<usize>),
|
2019-05-27 14:05:29 +01:00
|
|
|
FixedArraySize(ExpectedFound<u64>),
|
2015-09-06 21:51:58 +03:00
|
|
|
ArgCount,
|
2017-08-30 02:39:06 +03:00
|
|
|
|
2017-04-20 04:45:53 -04:00
|
|
|
RegionsDoesNotOutlive(Region<'tcx>, Region<'tcx>),
|
2019-02-20 05:39:04 -05:00
|
|
|
RegionsInsufficientlyPolymorphic(BoundRegion, Region<'tcx>),
|
|
|
|
RegionsOverlyPolymorphic(BoundRegion, Region<'tcx>),
|
2018-11-19 10:26:04 -05:00
|
|
|
RegionsPlaceholderMismatch,
|
2017-08-30 02:39:06 +03:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
Sorts(ExpectedFound<Ty<'tcx>>),
|
|
|
|
IntMismatch(ExpectedFound<ty::IntVarValue>),
|
2015-09-14 21:58:20 +12:00
|
|
|
FloatMismatch(ExpectedFound<ast::FloatTy>),
|
2015-09-06 21:51:58 +03:00
|
|
|
Traits(ExpectedFound<DefId>),
|
|
|
|
VariadicMismatch(ExpectedFound<bool>),
|
2017-11-17 11:13:13 -05:00
|
|
|
|
|
|
|
/// Instantiating a type variable with the given type would have
|
|
|
|
/// created a cycle (because it appears somewhere within that
|
|
|
|
/// type).
|
|
|
|
CyclicTy(Ty<'tcx>),
|
2017-07-11 10:33:09 -04:00
|
|
|
ProjectionMismatched(ExpectedFound<DefId>),
|
2015-09-06 21:51:58 +03:00
|
|
|
ProjectionBoundsLength(ExpectedFound<usize>),
|
2018-08-22 00:35:01 +01:00
|
|
|
ExistentialMismatch(ExpectedFound<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>>),
|
2019-03-08 01:13:38 +00:00
|
|
|
|
2019-03-18 20:55:19 +00:00
|
|
|
ConstMismatch(ExpectedFound<&'tcx ty::Const<'tcx>>),
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
|
|
|
|
pub enum UnconstrainedNumeric {
|
|
|
|
UnconstrainedFloat,
|
|
|
|
UnconstrainedInt,
|
|
|
|
Neither,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Explains the source of a type err in a short, human readable way. This is meant to be placed
|
|
|
|
/// in parentheses after some larger message. You should also invoke `note_and_explain_type_err()`
|
|
|
|
/// afterwards to present additional details, particularly when it comes to lifetime-related
|
|
|
|
/// errors.
|
|
|
|
impl<'tcx> fmt::Display for TypeError<'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-09-06 21:51:58 +03:00
|
|
|
use self::TypeError::*;
|
2018-08-29 22:02:42 -07:00
|
|
|
fn report_maybe_different(f: &mut fmt::Formatter<'_>,
|
2018-10-01 15:31:27 +02:00
|
|
|
expected: &str, found: &str) -> fmt::Result {
|
2015-09-06 21:51:58 +03:00
|
|
|
// A naive approach to making sure that we're not reporting silly errors such as:
|
|
|
|
// (expected closure, found closure).
|
|
|
|
if expected == found {
|
|
|
|
write!(f, "expected {}, found a different {}", expected, found)
|
|
|
|
} else {
|
|
|
|
write!(f, "expected {}, found {}", expected, found)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 16:26:03 +02:00
|
|
|
let br_string = |br: ty::BoundRegion| {
|
|
|
|
match br {
|
|
|
|
ty::BrNamed(_, name) => format!(" {}", name),
|
|
|
|
_ => String::new(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-27 14:28:20 +01:00
|
|
|
macro_rules! pluralise {
|
|
|
|
($x:expr) => {
|
|
|
|
if $x != 1 { "s" } else { "" }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
match *self {
|
2017-11-17 11:13:13 -05:00
|
|
|
CyclicTy(_) => write!(f, "cyclic type of infinite size"),
|
2015-09-06 21:51:58 +03:00
|
|
|
Mismatch => write!(f, "types differ"),
|
|
|
|
UnsafetyMismatch(values) => {
|
|
|
|
write!(f, "expected {} fn, found {} fn",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
}
|
|
|
|
AbiMismatch(values) => {
|
|
|
|
write!(f, "expected {} fn, found {} fn",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
}
|
2016-08-19 16:05:37 -07:00
|
|
|
Mutability => write!(f, "types differ in mutability"),
|
2015-09-06 21:51:58 +03:00
|
|
|
TupleSize(values) => {
|
2019-05-27 14:28:20 +01:00
|
|
|
write!(f, "expected a tuple with {} element{}, \
|
|
|
|
found one with {} element{}",
|
2019-05-27 14:05:29 +01:00
|
|
|
values.expected,
|
2019-05-27 14:28:20 +01:00
|
|
|
pluralise!(values.expected),
|
|
|
|
values.found,
|
|
|
|
pluralise!(values.found))
|
2019-05-27 14:05:29 +01:00
|
|
|
}
|
|
|
|
FixedArraySize(values) => {
|
2019-05-27 14:28:20 +01:00
|
|
|
write!(f, "expected an array with a fixed size of {} element{}, \
|
|
|
|
found one with {} element{}",
|
2015-09-06 21:51:58 +03:00
|
|
|
values.expected,
|
2019-05-27 14:28:20 +01:00
|
|
|
pluralise!(values.expected),
|
|
|
|
values.found,
|
|
|
|
pluralise!(values.found))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
ArgCount => {
|
|
|
|
write!(f, "incorrect number of function parameters")
|
|
|
|
}
|
|
|
|
RegionsDoesNotOutlive(..) => {
|
|
|
|
write!(f, "lifetime mismatch")
|
|
|
|
}
|
2019-02-20 05:39:04 -05:00
|
|
|
RegionsInsufficientlyPolymorphic(br, _) => {
|
|
|
|
write!(f,
|
2019-01-14 16:26:03 +02:00
|
|
|
"expected bound lifetime parameter{}, found concrete lifetime",
|
|
|
|
br_string(br))
|
2019-02-20 05:39:04 -05:00
|
|
|
}
|
|
|
|
RegionsOverlyPolymorphic(br, _) => {
|
|
|
|
write!(f,
|
2019-01-14 16:26:03 +02:00
|
|
|
"expected concrete lifetime, found bound lifetime parameter{}",
|
|
|
|
br_string(br))
|
2019-02-20 05:39:04 -05:00
|
|
|
}
|
2018-11-19 10:26:04 -05:00
|
|
|
RegionsPlaceholderMismatch => {
|
|
|
|
write!(f, "one type is more general than the other")
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
Sorts(values) => ty::tls::with(|tcx| {
|
2018-10-01 15:31:27 +02:00
|
|
|
report_maybe_different(f, &values.expected.sort_string(tcx),
|
|
|
|
&values.found.sort_string(tcx))
|
2015-09-06 21:51:58 +03:00
|
|
|
}),
|
|
|
|
Traits(values) => ty::tls::with(|tcx| {
|
|
|
|
report_maybe_different(f,
|
2018-10-01 15:31:27 +02:00
|
|
|
&format!("trait `{}`",
|
2018-12-19 12:31:35 +02:00
|
|
|
tcx.def_path_str(values.expected)),
|
2018-10-01 15:31:27 +02:00
|
|
|
&format!("trait `{}`",
|
2018-12-19 12:31:35 +02:00
|
|
|
tcx.def_path_str(values.found)))
|
2015-09-06 21:51:58 +03:00
|
|
|
}),
|
|
|
|
IntMismatch(ref values) => {
|
|
|
|
write!(f, "expected `{:?}`, found `{:?}`",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
}
|
|
|
|
FloatMismatch(ref values) => {
|
|
|
|
write!(f, "expected `{:?}`, found `{:?}`",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
}
|
|
|
|
VariadicMismatch(ref values) => {
|
|
|
|
write!(f, "expected {} fn, found {} function",
|
|
|
|
if values.expected { "variadic" } else { "non-variadic" },
|
|
|
|
if values.found { "variadic" } else { "non-variadic" })
|
|
|
|
}
|
2017-07-11 10:33:09 -04:00
|
|
|
ProjectionMismatched(ref values) => ty::tls::with(|tcx| {
|
2015-09-06 21:51:58 +03:00
|
|
|
write!(f, "expected {}, found {}",
|
2018-12-19 12:31:35 +02:00
|
|
|
tcx.def_path_str(values.expected),
|
|
|
|
tcx.def_path_str(values.found))
|
2017-07-11 10:33:09 -04:00
|
|
|
}),
|
2015-09-06 21:51:58 +03:00
|
|
|
ProjectionBoundsLength(ref values) => {
|
2019-05-27 14:28:20 +01:00
|
|
|
write!(f, "expected {} associated type binding{}, found {}",
|
2015-09-06 21:51:58 +03:00
|
|
|
values.expected,
|
2019-05-27 14:28:20 +01:00
|
|
|
pluralise!(values.expected),
|
2015-09-06 21:51:58 +03:00
|
|
|
values.found)
|
|
|
|
},
|
2016-11-16 09:21:49 -07:00
|
|
|
ExistentialMismatch(ref values) => {
|
2018-10-01 15:31:27 +02:00
|
|
|
report_maybe_different(f, &format!("trait `{}`", values.expected),
|
|
|
|
&format!("trait `{}`", values.found))
|
2016-11-16 09:21:49 -07:00
|
|
|
}
|
2019-03-12 19:27:06 +00:00
|
|
|
ConstMismatch(ref values) => {
|
2019-05-25 21:00:29 +01:00
|
|
|
write!(f, "expected `{}`, found `{}`", values.expected, values.found)
|
2019-03-08 01:13:38 +00:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> ty::TyS<'tcx> {
|
|
|
|
pub fn sort_string(&self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
|
2015-09-06 21:51:58 +03:00
|
|
|
match self.sty {
|
2018-08-22 01:35:55 +01:00
|
|
|
ty::Bool | ty::Char | ty::Int(_) |
|
2018-10-01 15:31:27 +02:00
|
|
|
ty::Uint(_) | ty::Float(_) | ty::Str | ty::Never => self.to_string().into(),
|
|
|
|
ty::Tuple(ref tys) if tys.is_empty() => self.to_string().into(),
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2018-12-19 12:31:35 +02:00
|
|
|
ty::Adt(def, _) => format!("{} `{}`", def.descr(), tcx.def_path_str(def.did)).into(),
|
|
|
|
ty::Foreign(def_id) => format!("extern type `{}`", tcx.def_path_str(def_id)).into(),
|
2019-06-14 18:09:57 +02:00
|
|
|
ty::Array(_, n) => {
|
|
|
|
let n = tcx.lift_to_global(&n).unwrap();
|
2019-03-22 18:30:35 +01:00
|
|
|
match n.try_eval_usize(tcx) {
|
2019-06-14 18:09:57 +02:00
|
|
|
Some(n) => format!("array of {} elements", n).into(),
|
|
|
|
None => "array".into(),
|
|
|
|
}
|
2017-08-05 16:11:24 +03:00
|
|
|
}
|
2018-10-01 15:31:27 +02:00
|
|
|
ty::Slice(_) => "slice".into(),
|
|
|
|
ty::RawPtr(_) => "*-ptr".into(),
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Ref(region, ty, mutbl) => {
|
2018-05-02 15:21:05 +02:00
|
|
|
let tymut = ty::TypeAndMut { ty, mutbl };
|
2016-08-11 21:47:56 -07:00
|
|
|
let tymut_string = tymut.to_string();
|
|
|
|
if tymut_string == "_" || //unknown type name,
|
|
|
|
tymut_string.len() > 10 || //name longer than saying "reference",
|
2019-01-23 19:36:39 +02:00
|
|
|
region.to_string() != "'_" //... or a complex type
|
2016-08-11 21:47:56 -07:00
|
|
|
{
|
2018-05-02 15:21:05 +02:00
|
|
|
format!("{}reference", match mutbl {
|
|
|
|
hir::Mutability::MutMutable => "mutable ",
|
|
|
|
_ => ""
|
2018-10-01 15:31:27 +02:00
|
|
|
}).into()
|
2016-08-11 21:47:56 -07:00
|
|
|
} else {
|
2018-10-01 15:31:27 +02:00
|
|
|
format!("&{}", tymut_string).into()
|
2016-08-11 21:47:56 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-01 15:31:27 +02:00
|
|
|
ty::FnDef(..) => "fn item".into(),
|
|
|
|
ty::FnPtr(_) => "fn pointer".into(),
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Dynamic(ref inner, ..) => {
|
2018-12-04 13:28:06 +02:00
|
|
|
if let Some(principal) = inner.principal() {
|
2018-12-19 12:31:35 +02:00
|
|
|
format!("trait {}", tcx.def_path_str(principal.def_id())).into()
|
2018-12-04 13:28:06 +02:00
|
|
|
} else {
|
|
|
|
"trait".into()
|
|
|
|
}
|
2018-10-01 15:31:27 +02:00
|
|
|
}
|
|
|
|
ty::Closure(..) => "closure".into(),
|
|
|
|
ty::Generator(..) => "generator".into(),
|
|
|
|
ty::GeneratorWitness(..) => "generator witness".into(),
|
|
|
|
ty::Tuple(..) => "tuple".into(),
|
|
|
|
ty::Infer(ty::TyVar(_)) => "inferred type".into(),
|
2018-12-31 20:43:08 -05:00
|
|
|
ty::Infer(ty::IntVar(_)) => "integer".into(),
|
|
|
|
ty::Infer(ty::FloatVar(_)) => "floating-point number".into(),
|
2018-11-02 18:48:24 +01:00
|
|
|
ty::Placeholder(..) => "placeholder type".into(),
|
2018-11-03 15:15:33 +01:00
|
|
|
ty::Bound(..) => "bound type".into(),
|
2018-10-01 15:31:27 +02:00
|
|
|
ty::Infer(ty::FreshTy(_)) => "fresh type".into(),
|
|
|
|
ty::Infer(ty::FreshIntTy(_)) => "fresh integral type".into(),
|
|
|
|
ty::Infer(ty::FreshFloatTy(_)) => "fresh floating-point type".into(),
|
|
|
|
ty::Projection(_) => "associated type".into(),
|
|
|
|
ty::UnnormalizedProjection(_) => "non-normalized associated type".into(),
|
2018-08-22 01:35:29 +01:00
|
|
|
ty::Param(ref p) => {
|
2016-08-15 01:07:09 +03:00
|
|
|
if p.is_self() {
|
2018-10-01 15:31:27 +02:00
|
|
|
"Self".into()
|
2015-09-06 21:51:58 +03:00
|
|
|
} else {
|
2018-10-01 15:31:27 +02:00
|
|
|
"type parameter".into()
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-01 15:31:27 +02:00
|
|
|
ty::Opaque(..) => "opaque type".into(),
|
|
|
|
ty::Error => "type error".into(),
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn note_and_explain_type_err(self,
|
2018-08-29 22:02:42 -07:00
|
|
|
db: &mut DiagnosticBuilder<'_>,
|
2015-12-21 10:00:43 +13:00
|
|
|
err: &TypeError<'tcx>,
|
|
|
|
sp: Span) {
|
2015-09-06 21:51:58 +03:00
|
|
|
use self::TypeError::*;
|
|
|
|
|
|
|
|
match err.clone() {
|
|
|
|
Sorts(values) => {
|
|
|
|
let expected_str = values.expected.sort_string(self);
|
|
|
|
let found_str = values.found.sort_string(self);
|
|
|
|
if expected_str == found_str && expected_str == "closure" {
|
2017-12-19 19:04:09 -08:00
|
|
|
db.note("no two closures, even if identical, have the same type");
|
|
|
|
db.help("consider boxing your closure and/or using it as a trait object");
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2018-10-01 15:37:14 +02:00
|
|
|
if let (ty::Infer(ty::IntVar(_)), ty::Float(_)) =
|
|
|
|
(&values.found.sty, &values.expected.sty) // Issue #53280
|
|
|
|
{
|
|
|
|
if let Ok(snippet) = self.sess.source_map().span_to_snippet(sp) {
|
|
|
|
if snippet.chars().all(|c| c.is_digit(10) || c == '-' || c == '_') {
|
2019-01-25 16:03:27 -05:00
|
|
|
db.span_suggestion(
|
2018-10-01 15:37:14 +02:00
|
|
|
sp,
|
|
|
|
"use a float literal",
|
|
|
|
format!("{}.0", snippet),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
2018-08-11 22:54:57 -07:00
|
|
|
}
|
2018-10-01 15:37:14 +02:00
|
|
|
}
|
2018-08-11 22:54:57 -07:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
},
|
2017-11-17 11:13:13 -05:00
|
|
|
CyclicTy(ty) => {
|
|
|
|
// Watch out for various cases of cyclic types and try to explain.
|
|
|
|
if ty.is_closure() || ty.is_generator() {
|
|
|
|
db.note("closures cannot capture themselves or take themselves as argument;\n\
|
|
|
|
this error may be the result of a recent compiler bug-fix,\n\
|
|
|
|
see https://github.com/rust-lang/rust/issues/46062 for more details");
|
|
|
|
}
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|