2015-09-06 21:51:58 +03:00
|
|
|
// Copyright 2012-2015 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.
|
|
|
|
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def_id::DefId;
|
2017-07-16 04:55:48 -04:00
|
|
|
use ty::{self, BoundRegion, Region, Ty, TyCtxt};
|
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;
|
2018-08-11 22:54:57 -07: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
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use hir;
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
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>),
|
2017-08-05 16:11:24 +03: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>),
|
2017-07-29 17:19:57 +03:00
|
|
|
RegionsInsufficientlyPolymorphic(BoundRegion, Region<'tcx>),
|
|
|
|
RegionsOverlyPolymorphic(BoundRegion, Region<'tcx>),
|
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>),
|
2016-11-16 09:21:49 -07:00
|
|
|
ExistentialMismatch(ExpectedFound<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>>),
|
2017-08-30 16:06:05 -04:00
|
|
|
|
|
|
|
OldStyleLUB(Box<TypeError<'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> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::TypeError::*;
|
|
|
|
fn report_maybe_different(f: &mut fmt::Formatter,
|
|
|
|
expected: String, found: String) -> fmt::Result {
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
FixedArraySize(values) => {
|
|
|
|
write!(f, "expected an array with a fixed size of {} elements, \
|
|
|
|
found one with {} elements",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
}
|
|
|
|
TupleSize(values) => {
|
|
|
|
write!(f, "expected a tuple with {} elements, \
|
|
|
|
found one with {} elements",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
}
|
|
|
|
ArgCount => {
|
|
|
|
write!(f, "incorrect number of function parameters")
|
|
|
|
}
|
|
|
|
RegionsDoesNotOutlive(..) => {
|
|
|
|
write!(f, "lifetime mismatch")
|
|
|
|
}
|
2017-07-29 17:19:57 +03:00
|
|
|
RegionsInsufficientlyPolymorphic(br, _) => {
|
2017-04-23 15:37:16 -07:00
|
|
|
write!(f,
|
|
|
|
"expected bound lifetime parameter{}{}, found concrete lifetime",
|
|
|
|
if br.is_named() { " " } else { "" },
|
|
|
|
br)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2017-07-29 17:19:57 +03:00
|
|
|
RegionsOverlyPolymorphic(br, _) => {
|
2017-04-23 15:37:16 -07:00
|
|
|
write!(f,
|
|
|
|
"expected concrete lifetime, found bound lifetime parameter{}{}",
|
|
|
|
if br.is_named() { " " } else { "" },
|
|
|
|
br)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
Sorts(values) => ty::tls::with(|tcx| {
|
|
|
|
report_maybe_different(f, values.expected.sort_string(tcx),
|
|
|
|
values.found.sort_string(tcx))
|
|
|
|
}),
|
|
|
|
Traits(values) => ty::tls::with(|tcx| {
|
|
|
|
report_maybe_different(f,
|
|
|
|
format!("trait `{}`",
|
|
|
|
tcx.item_path_str(values.expected)),
|
|
|
|
format!("trait `{}`",
|
|
|
|
tcx.item_path_str(values.found)))
|
|
|
|
}),
|
|
|
|
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 {}",
|
2017-07-11 10:33:09 -04:00
|
|
|
tcx.item_path_str(values.expected),
|
|
|
|
tcx.item_path_str(values.found))
|
|
|
|
}),
|
2015-09-06 21:51:58 +03:00
|
|
|
ProjectionBoundsLength(ref values) => {
|
|
|
|
write!(f, "expected {} associated type bindings, found {}",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
},
|
2016-11-16 09:21:49 -07:00
|
|
|
ExistentialMismatch(ref values) => {
|
|
|
|
report_maybe_different(f, format!("trait `{}`", values.expected),
|
|
|
|
format!("trait `{}`", values.found))
|
|
|
|
}
|
2017-08-30 16:06:05 -04:00
|
|
|
OldStyleLUB(ref err) => {
|
|
|
|
write!(f, "{}", err)
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
|
2016-09-07 18:40:31 -07:00
|
|
|
pub fn sort_string(&self, tcx: TyCtxt<'a, 'gcx, 'lcx>) -> String {
|
2015-09-06 21:51:58 +03:00
|
|
|
match self.sty {
|
|
|
|
ty::TyBool | ty::TyChar | ty::TyInt(_) |
|
2016-08-02 15:56:20 +08:00
|
|
|
ty::TyUint(_) | ty::TyFloat(_) | ty::TyStr | ty::TyNever => self.to_string(),
|
2018-01-21 13:33:21 +08:00
|
|
|
ty::TyTuple(ref tys) if tys.is_empty() => self.to_string(),
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(def, _) => format!("{} `{}`", def.descr(), tcx.item_path_str(def.did)),
|
2017-09-03 19:53:58 +01:00
|
|
|
ty::TyForeign(def_id) => format!("extern type `{}`", tcx.item_path_str(def_id)),
|
2017-08-05 16:11:24 +03:00
|
|
|
ty::TyArray(_, n) => {
|
2018-04-26 09:18:19 +02:00
|
|
|
match n.assert_usize(tcx) {
|
2018-01-27 17:15:40 +01:00
|
|
|
Some(n) => format!("array of {} elements", n),
|
2018-01-29 08:50:47 +01:00
|
|
|
None => "array".to_string(),
|
2017-08-05 16:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::TySlice(_) => "slice".to_string(),
|
|
|
|
ty::TyRawPtr(_) => "*-ptr".to_string(),
|
2018-05-02 15:21:05 +02:00
|
|
|
ty::TyRef(region, ty, mutbl) => {
|
|
|
|
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",
|
|
|
|
region.to_string() != "" //... or a complex type
|
|
|
|
{
|
2018-05-02 15:21:05 +02:00
|
|
|
format!("{}reference", match mutbl {
|
|
|
|
hir::Mutability::MutMutable => "mutable ",
|
|
|
|
_ => ""
|
|
|
|
})
|
2016-08-11 21:47:56 -07:00
|
|
|
} else {
|
|
|
|
format!("&{}", tymut_string)
|
|
|
|
}
|
|
|
|
}
|
2018-07-28 14:40:32 +02:00
|
|
|
ty::TyFnDef(..) => "fn item".to_string(),
|
2015-06-13 13:15:03 -07:00
|
|
|
ty::TyFnPtr(_) => "fn pointer".to_string(),
|
2016-11-16 09:21:49 -07:00
|
|
|
ty::TyDynamic(ref inner, ..) => {
|
2016-11-12 15:46:16 -07:00
|
|
|
inner.principal().map_or_else(|| "trait".to_string(),
|
|
|
|
|p| format!("trait {}", tcx.item_path_str(p.def_id())))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
ty::TyClosure(..) => "closure".to_string(),
|
2016-12-26 14:34:03 +01:00
|
|
|
ty::TyGenerator(..) => "generator".to_string(),
|
2017-10-07 16:36:28 +02:00
|
|
|
ty::TyGeneratorWitness(..) => "generator witness".to_string(),
|
2017-01-11 15:58:37 +08:00
|
|
|
ty::TyTuple(..) => "tuple".to_string(),
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::TyInfer(ty::TyVar(_)) => "inferred type".to_string(),
|
|
|
|
ty::TyInfer(ty::IntVar(_)) => "integral variable".to_string(),
|
|
|
|
ty::TyInfer(ty::FloatVar(_)) => "floating-point variable".to_string(),
|
2018-02-09 10:39:36 -05:00
|
|
|
ty::TyInfer(ty::CanonicalTy(_)) |
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::TyInfer(ty::FreshTy(_)) => "skolemized type".to_string(),
|
|
|
|
ty::TyInfer(ty::FreshIntTy(_)) => "skolemized integral type".to_string(),
|
|
|
|
ty::TyInfer(ty::FreshFloatTy(_)) => "skolemized floating-point type".to_string(),
|
|
|
|
ty::TyProjection(_) => "associated type".to_string(),
|
|
|
|
ty::TyParam(ref p) => {
|
2016-08-15 01:07:09 +03:00
|
|
|
if p.is_self() {
|
2015-09-06 21:51:58 +03:00
|
|
|
"Self".to_string()
|
|
|
|
} else {
|
|
|
|
"type parameter".to_string()
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 18:56:22 +03:00
|
|
|
ty::TyAnon(..) => "anonymized type".to_string(),
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::TyError => "type error".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:00:23 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn note_and_explain_type_err(self,
|
2015-12-21 10:00:43 +13:00
|
|
|
db: &mut DiagnosticBuilder,
|
|
|
|
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-08-11 22:54:57 -07:00
|
|
|
match (&values.found.sty, &values.expected.sty) { // Issue #53280
|
|
|
|
(ty::TyInfer(ty::IntVar(_)), ty::TyFloat(_)) => {
|
2018-08-18 12:14:09 +02:00
|
|
|
if let Ok(snippet) = self.sess.source_map().span_to_snippet(sp) {
|
2018-08-11 22:54:57 -07:00
|
|
|
if snippet.chars().all(|c| c.is_digit(10) || c == '-' || c == '_') {
|
|
|
|
db.span_suggestion_with_applicability(
|
|
|
|
sp,
|
|
|
|
"use a float literal",
|
|
|
|
format!("{}.0", snippet),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
},
|
2017-11-02 09:42:30 -04:00
|
|
|
OldStyleLUB(err) => {
|
|
|
|
db.note("this was previously accepted by the compiler but has been phased out");
|
|
|
|
db.note("for more information, see https://github.com/rust-lang/rust/issues/45852");
|
|
|
|
|
|
|
|
self.note_and_explain_type_err(db, &err, sp);
|
|
|
|
}
|
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
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|