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;
|
2016-03-22 17:30:57 +02:00
|
|
|
use infer::type_variable;
|
|
|
|
use ty::{self, BoundRegion, Region, Ty, TyCtxt};
|
2015-09-06 21:51:58 +03:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use syntax::abi;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::ast::{self, Name};
|
2016-06-21 18:08:13 -04:00
|
|
|
use errors::DiagnosticBuilder;
|
|
|
|
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,
|
|
|
|
pub found: T
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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>),
|
|
|
|
FixedArraySize(ExpectedFound<usize>),
|
|
|
|
ArgCount,
|
2016-08-25 23:58:52 +03:00
|
|
|
RegionsDoesNotOutlive(&'tcx Region, &'tcx Region),
|
|
|
|
RegionsNotSame(&'tcx Region, &'tcx Region),
|
|
|
|
RegionsNoOverlap(&'tcx Region, &'tcx Region),
|
|
|
|
RegionsInsufficientlyPolymorphic(BoundRegion, &'tcx Region),
|
|
|
|
RegionsOverlyPolymorphic(BoundRegion, &'tcx Region),
|
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>),
|
|
|
|
BuiltinBoundsMismatch(ExpectedFound<ty::BuiltinBounds>),
|
|
|
|
VariadicMismatch(ExpectedFound<bool>),
|
|
|
|
CyclicTy,
|
|
|
|
ProjectionNameMismatched(ExpectedFound<Name>),
|
|
|
|
ProjectionBoundsLength(ExpectedFound<usize>),
|
|
|
|
TyParamDefaultMismatch(ExpectedFound<type_variable::Default<'tcx>>)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
|
|
|
CyclicTy => write!(f, "cyclic type of infinite size"),
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
RegionsNotSame(..) => {
|
|
|
|
write!(f, "lifetimes are not the same")
|
|
|
|
}
|
|
|
|
RegionsNoOverlap(..) => {
|
|
|
|
write!(f, "lifetimes do not intersect")
|
|
|
|
}
|
|
|
|
RegionsInsufficientlyPolymorphic(br, _) => {
|
|
|
|
write!(f, "expected bound lifetime parameter {}, \
|
|
|
|
found concrete lifetime", br)
|
|
|
|
}
|
|
|
|
RegionsOverlyPolymorphic(br, _) => {
|
|
|
|
write!(f, "expected concrete lifetime, \
|
|
|
|
found bound lifetime parameter {}", br)
|
|
|
|
}
|
|
|
|
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)))
|
|
|
|
}),
|
|
|
|
BuiltinBoundsMismatch(values) => {
|
|
|
|
if values.expected.is_empty() {
|
|
|
|
write!(f, "expected no bounds, found `{}`",
|
|
|
|
values.found)
|
|
|
|
} else if values.found.is_empty() {
|
|
|
|
write!(f, "expected bounds `{}`, found no bounds",
|
|
|
|
values.expected)
|
|
|
|
} else {
|
|
|
|
write!(f, "expected bounds `{}`, found bounds `{}`",
|
|
|
|
values.expected,
|
|
|
|
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" })
|
|
|
|
}
|
|
|
|
ProjectionNameMismatched(ref values) => {
|
|
|
|
write!(f, "expected {}, found {}",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
}
|
|
|
|
ProjectionBoundsLength(ref values) => {
|
|
|
|
write!(f, "expected {} associated type bindings, found {}",
|
|
|
|
values.expected,
|
|
|
|
values.found)
|
|
|
|
},
|
|
|
|
TyParamDefaultMismatch(ref values) => {
|
|
|
|
write!(f, "conflicting type parameter defaults `{}` and `{}`",
|
|
|
|
values.expected.ty,
|
|
|
|
values.found.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(),
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::TyTuple(ref tys) if tys.is_empty() => self.to_string(),
|
|
|
|
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(def, _) => format!("{} `{}`", def.descr(), tcx.item_path_str(def.did)),
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::TyBox(_) => "box".to_string(),
|
|
|
|
ty::TyArray(_, n) => format!("array of {} elements", n),
|
|
|
|
ty::TySlice(_) => "slice".to_string(),
|
|
|
|
ty::TyRawPtr(_) => "*-ptr".to_string(),
|
2016-08-11 21:47:56 -07:00
|
|
|
ty::TyRef(region, tymut) => {
|
|
|
|
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
|
|
|
|
{
|
|
|
|
match tymut {
|
|
|
|
ty::TypeAndMut{mutbl, ..} => {
|
|
|
|
format!("{}reference", match mutbl {
|
|
|
|
hir::Mutability::MutMutable => "mutable ",
|
|
|
|
_ => ""
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
format!("&{}", tymut_string)
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 13:15:03 -07:00
|
|
|
ty::TyFnDef(..) => format!("fn item"),
|
|
|
|
ty::TyFnPtr(_) => "fn pointer".to_string(),
|
2015-09-06 21:51:58 +03:00
|
|
|
ty::TyTrait(ref inner) => {
|
2016-08-04 15:52:57 +03:00
|
|
|
format!("trait {}", tcx.item_path_str(inner.principal.def_id()))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
ty::TyClosure(..) => "closure".to_string(),
|
|
|
|
ty::TyTuple(_) => "tuple".to_string(),
|
|
|
|
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(),
|
|
|
|
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() {
|
|
|
|
RegionsDoesNotOutlive(subregion, superregion) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
self.note_and_explain_region(db, "", subregion, "...");
|
|
|
|
self.note_and_explain_region(db, "...does not necessarily outlive ",
|
2015-09-06 21:51:58 +03:00
|
|
|
superregion, "");
|
|
|
|
}
|
|
|
|
RegionsNotSame(region1, region2) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
self.note_and_explain_region(db, "", region1, "...");
|
|
|
|
self.note_and_explain_region(db, "...is not the same lifetime as ",
|
2015-09-06 21:51:58 +03:00
|
|
|
region2, "");
|
|
|
|
}
|
|
|
|
RegionsNoOverlap(region1, region2) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
self.note_and_explain_region(db, "", region1, "...");
|
|
|
|
self.note_and_explain_region(db, "...does not overlap ",
|
2015-09-06 21:51:58 +03:00
|
|
|
region2, "");
|
|
|
|
}
|
|
|
|
RegionsInsufficientlyPolymorphic(_, conc_region) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
self.note_and_explain_region(db, "concrete lifetime that was found is ",
|
2015-09-06 21:51:58 +03:00
|
|
|
conc_region, "");
|
|
|
|
}
|
2016-08-25 23:58:52 +03:00
|
|
|
RegionsOverlyPolymorphic(_, &ty::ReVar(_)) => {
|
2015-09-06 21:51:58 +03:00
|
|
|
// don't bother to print out the message below for
|
|
|
|
// inference variables, it's not very illuminating.
|
|
|
|
}
|
|
|
|
RegionsOverlyPolymorphic(_, conc_region) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
self.note_and_explain_region(db, "expected concrete lifetime is ",
|
2015-09-06 21:51:58 +03:00
|
|
|
conc_region, "");
|
|
|
|
}
|
|
|
|
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" {
|
2015-12-21 10:00:43 +13:00
|
|
|
db.span_note(sp,
|
2015-11-05 15:30:32 +01:00
|
|
|
"no two closures, even if identical, have the same type");
|
2015-12-21 10:00:43 +13:00
|
|
|
db.span_help(sp,
|
2015-11-05 15:30:32 +01:00
|
|
|
"consider boxing your closure and/or using it as a trait object");
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
TyParamDefaultMismatch(values) => {
|
|
|
|
let expected = values.expected;
|
|
|
|
let found = values.found;
|
2015-12-21 10:00:43 +13:00
|
|
|
db.span_note(sp, &format!("conflicting type parameter defaults `{}` and `{}`",
|
|
|
|
expected.ty,
|
|
|
|
found.ty));
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2015-09-04 13:52:28 -04:00
|
|
|
match
|
|
|
|
self.map.as_local_node_id(expected.def_id)
|
|
|
|
.and_then(|node_id| self.map.opt_span(node_id))
|
|
|
|
{
|
|
|
|
Some(span) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
db.span_note(span, "a default was defined here...");
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2015-09-04 13:52:28 -04:00
|
|
|
None => {
|
2015-12-21 10:00:43 +13:00
|
|
|
db.note(&format!("a default is defined on `{}`",
|
|
|
|
self.item_path_str(expected.def_id)));
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
db.span_note(
|
2015-09-06 21:51:58 +03:00
|
|
|
expected.origin_span,
|
2015-11-05 15:30:32 +01:00
|
|
|
"...that was applied to an unconstrained type variable here");
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2015-09-04 13:52:28 -04:00
|
|
|
match
|
|
|
|
self.map.as_local_node_id(found.def_id)
|
|
|
|
.and_then(|node_id| self.map.opt_span(node_id))
|
|
|
|
{
|
|
|
|
Some(span) => {
|
2015-12-21 10:00:43 +13:00
|
|
|
db.span_note(span, "a second default was defined here...");
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2015-09-04 13:52:28 -04:00
|
|
|
None => {
|
2015-12-21 10:00:43 +13:00
|
|
|
db.note(&format!("a second default is defined on `{}`",
|
|
|
|
self.item_path_str(found.def_id)));
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
db.span_note(found.origin_span,
|
|
|
|
"...that also applies to the same type variable here");
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|