rust/src/librustc/ty/error.rs

408 lines
17 KiB
Rust
Raw Normal View History

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};
use std::borrow::Cow;
2015-09-06 21:51:58 +03:00
use std::fmt;
use rustc_target::spec::abi;
use syntax::ast;
2019-11-05 15:10:24 -05:00
use syntax::errors::pluralize;
use errors::{Applicability, DiagnosticBuilder};
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
2019-11-13 21:36:57 +01:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable)]
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
2019-11-13 21:36:57 +01:00
#[derive(Clone, Debug, TypeFoldable)]
2015-09-06 21:51:58 +03:00
pub enum TypeError<'tcx> {
Mismatch,
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
AbiMismatch(ExpectedFound<abi::Abi>),
Mutability,
TupleSize(ExpectedFound<usize>),
FixedArraySize(ExpectedFound<u64>),
2015-09-06 21:51:58 +03:00
ArgCount,
RegionsDoesNotOutlive(Region<'tcx>, Region<'tcx>),
2019-02-20 05:39:04 -05:00
RegionsInsufficientlyPolymorphic(BoundRegion, Region<'tcx>),
RegionsOverlyPolymorphic(BoundRegion, Region<'tcx>),
RegionsPlaceholderMismatch,
2015-09-06 21:51:58 +03:00
Sorts(ExpectedFound<Ty<'tcx>>),
IntMismatch(ExpectedFound<ty::IntVarValue>),
FloatMismatch(ExpectedFound<ast::FloatTy>),
2015-09-06 21:51:58 +03:00
Traits(ExpectedFound<DefId>),
VariadicMismatch(ExpectedFound<bool>),
/// Instantiating a type variable with the given type would have
/// created a cycle (because it appears somewhere within that
/// type).
CyclicTy(Ty<'tcx>),
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>>>),
ObjectUnsafeCoercion(DefId),
2019-03-18 20:55:19 +00:00
ConstMismatch(ExpectedFound<&'tcx ty::Const<'tcx>>),
IntrinsicCast,
2015-09-06 21:51:58 +03:00
}
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 {
2015-09-06 21:51:58 +03:00
use self::TypeError::*;
fn report_maybe_different(f: &mut fmt::Formatter<'_>,
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)
}
}
let br_string = |br: ty::BoundRegion| {
match br {
ty::BrNamed(_, name) => format!(" {}", name),
_ => String::new(),
}
};
2015-09-06 21:51:58 +03:00
match *self {
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) => {
write!(f, "expected a tuple with {} element{}, \
found one with {} element{}",
values.expected,
2019-11-05 15:10:24 -05:00
pluralize!(values.expected),
values.found,
2019-11-05 15:10:24 -05:00
pluralize!(values.found))
}
FixedArraySize(values) => {
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-11-05 15:10:24 -05:00
pluralize!(values.expected),
values.found,
2019-11-05 15:10:24 -05:00
pluralize!(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,
"expected bound lifetime parameter{}, found concrete lifetime",
br_string(br))
2019-02-20 05:39:04 -05:00
}
RegionsOverlyPolymorphic(br, _) => {
write!(f,
"expected concrete lifetime, found bound lifetime parameter{}",
br_string(br))
2019-02-20 05:39: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| {
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,
&format!("trait `{}`",
tcx.def_path_str(values.expected)),
&format!("trait `{}`",
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" })
}
ProjectionMismatched(ref values) => ty::tls::with(|tcx| {
2015-09-06 21:51:58 +03:00
write!(f, "expected {}, found {}",
tcx.def_path_str(values.expected),
tcx.def_path_str(values.found))
}),
2015-09-06 21:51:58 +03:00
ProjectionBoundsLength(ref values) => {
write!(f, "expected {} associated type binding{}, found {}",
2015-09-06 21:51:58 +03:00
values.expected,
2019-11-05 15:10:24 -05:00
pluralize!(values.expected),
2015-09-06 21:51:58 +03:00
values.found)
},
ExistentialMismatch(ref values) => {
report_maybe_different(f, &format!("trait `{}`", values.expected),
&format!("trait `{}`", values.found))
}
2019-03-12 19:27:06 +00:00
ConstMismatch(ref values) => {
write!(f, "expected `{}`, found `{}`", values.expected, values.found)
}
IntrinsicCast => {
write!(f, "cannot coerce intrinsics to function pointers")
}
ObjectUnsafeCoercion(_) => write!(f, "coercion to object-unsafe trait object"),
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> {
2019-09-16 19:08:35 +01:00
match self.kind {
ty::Bool | ty::Char | ty::Int(_) |
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
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) => {
2019-09-25 15:40:21 -04:00
let n = tcx.lift(&n).unwrap();
match n.try_eval_usize(tcx, ty::ParamEnv::empty()) {
Some(n) => {
2019-11-05 15:10:24 -05:00
format!("array of {} element{}", n, pluralize!(n)).into()
}
2019-06-14 18:09:57 +02:00
None => "array".into(),
}
}
ty::Slice(_) => "slice".into(),
ty::RawPtr(_) => "*-ptr".into(),
ty::Ref(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
2016-08-11 21:47:56 -07:00
{
format!("{}reference", match mutbl {
hir::Mutability::Mutable => "mutable ",
_ => ""
}).into()
2016-08-11 21:47:56 -07:00
} else {
format!("&{}", tymut_string).into()
2016-08-11 21:47:56 -07:00
}
}
ty::FnDef(..) => "fn item".into(),
ty::FnPtr(_) => "fn pointer".into(),
ty::Dynamic(ref inner, ..) => {
if let Some(principal) = inner.principal() {
format!("trait {}", tcx.def_path_str(principal.def_id())).into()
} else {
"trait".into()
}
}
ty::Closure(..) => "closure".into(),
ty::Generator(..) => "generator".into(),
ty::GeneratorWitness(..) => "generator witness".into(),
ty::Tuple(..) => "tuple".into(),
ty::Infer(ty::TyVar(_)) => "inferred type".into(),
ty::Infer(ty::IntVar(_)) => "integer".into(),
ty::Infer(ty::FloatVar(_)) => "floating-point number".into(),
ty::Placeholder(..) => "placeholder type".into(),
ty::Bound(..) => "bound type".into(),
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(),
ty::Param(p) => format!("type parameter `{}`", p).into(),
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> {
2019-08-21 11:46:31 -07:00
pub fn note_and_explain_type_err(
self,
db: &mut DiagnosticBuilder<'_>,
err: &TypeError<'tcx>,
sp: Span,
body_owner_def_id: DefId,
2019-08-21 11:46:31 -07:00
) {
2015-09-06 21:51:58 +03:00
use self::TypeError::*;
2019-08-21 11:46:31 -07:00
match err {
2015-09-06 21:51:58 +03:00
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" {
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
}
2019-08-21 11:46:31 -07:00
if expected_str == found_str && expected_str == "opaque type" { // Issue #63167
db.note("distinct uses of `impl Trait` result in different opaque types");
let e_str = values.expected.to_string();
let f_str = values.found.to_string();
if &e_str == &f_str && &e_str == "impl std::future::Future" {
// FIXME: use non-string based check.
2019-08-21 15:35:38 -07:00
db.help("if both `Future`s have the same `Output` type, consider \
`.await`ing on both of them");
2019-08-21 11:46:31 -07:00
}
}
2019-09-16 19:08:35 +01:00
match (&values.expected.kind, &values.found.kind) {
(ty::Float(_), ty::Infer(ty::IntVar(_))) => if let Ok( // Issue #53280
snippet,
) = self.sess.source_map().span_to_snippet(sp) {
2018-10-01 15:37:14 +02:00
if snippet.chars().all(|c| c.is_digit(10) || c == '-' || c == '_') {
db.span_suggestion(
2018-10-01 15:37:14 +02:00
sp,
"use a float literal",
format!("{}.0", snippet),
Applicability::MachineApplicable
);
}
},
(ty::Param(expected), ty::Param(found)) => {
let generics = self.generics_of(body_owner_def_id);
let e_span = self.def_span(generics.type_param(expected, self).def_id);
if !sp.contains(e_span) {
db.span_label(e_span, "expected type parameter");
}
let f_span = self.def_span(generics.type_param(found, self).def_id);
if !sp.contains(f_span) {
db.span_label(f_span, "found type parameter");
}
db.note("a type parameter was expected, but a different one was found; \
you might be missing a type parameter or trait bound");
db.note("for more information, visit \
https://doc.rust-lang.org/book/ch10-02-traits.html\
#traits-as-parameters");
}
(ty::Projection(_), ty::Projection(_)) => {
db.note("an associated type was expected, but a different one was found");
}
(ty::Param(_), ty::Projection(_)) | (ty::Projection(_), ty::Param(_)) => {
db.note("you might be missing a type parameter or trait bound");
}
(ty::Param(p), _) | (_, ty::Param(p)) => {
let generics = self.generics_of(body_owner_def_id);
let p_span = self.def_span(generics.type_param(p, self).def_id);
if !sp.contains(p_span) {
db.span_label(p_span, "this type parameter");
}
db.help("type parameters must be constrained to match other types");
if self.sess.teach(&db.get_code().unwrap()) {
db.help("given a type parameter `T` and a method `foo`:
```
trait Trait<T> { fn foo(&self) -> T; }
```
the only ways to implement method `foo` are:
- constrain `T` with an explicit type:
```
impl Trait<String> for X {
fn foo(&self) -> String { String::new() }
}
```
- add a trait bound to `T` and call a method on that trait that returns `Self`:
```
impl<T: std::default::Default> Trait<T> for X {
fn foo(&self) -> T { <T as std::default::Default>::default() }
}
```
- change `foo` to return an argument of type `T`:
```
impl<T> Trait<T> for X {
fn foo(&self, x: T) -> T { x }
}
```");
}
db.note("for more information, visit \
https://doc.rust-lang.org/book/ch10-02-traits.html\
#traits-as-parameters");
}
(ty::Projection(_), _) => {
db.note(&format!(
"consider constraining the associated type `{}` to `{}` or calling a \
method that returns `{}`",
values.expected,
values.found,
values.expected,
));
if self.sess.teach(&db.get_code().unwrap()) {
db.help("given an associated type `T` and a method `foo`:
```
trait Trait {
type T;
fn foo(&self) -> Self::T;
}
```
the only way of implementing method `foo` is to constrain `T` with an explicit associated type:
```
impl Trait for X {
type T = String;
fn foo(&self) -> Self::T { String::new() }
}
```");
}
db.note("for more information, visit \
https://doc.rust-lang.org/book/ch19-03-advanced-traits.html");
}
(_, ty::Projection(_)) => {
db.note(&format!(
"consider constraining the associated type `{}` to `{}`",
values.found,
values.expected,
));
db.note("for more information, visit \
https://doc.rust-lang.org/book/ch19-03-advanced-traits.html");
2018-10-01 15:37:14 +02:00
}
_ => {}
}
debug!(
"note_and_explain_type_err expected={:?} ({:?}) found={:?} ({:?})",
values.expected,
2019-09-25 15:57:54 +01:00
values.expected.kind,
values.found,
2019-09-25 15:57:54 +01:00
values.found.kind,
);
2015-09-06 21:51:58 +03: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
_ => {}
}
}
}