summary refs log tree commit diff
path: root/src/librustc/ty/error.rs
blob: 77613b548cfdb438b55b6f9796dd71abc4d82934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
use crate::hir::def_id::DefId;
use crate::ty::{self, BoundRegion, Region, Ty, TyCtxt};
use std::borrow::Cow;
use std::fmt;
use rustc_target::spec::abi;
use syntax::ast;
use syntax::errors::pluralise;
use errors::{Applicability, DiagnosticBuilder};
use syntax_pos::Span;

use crate::hir;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
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<u64>),
    ArgCount,

    RegionsDoesNotOutlive(Region<'tcx>, Region<'tcx>),
    RegionsInsufficientlyPolymorphic(BoundRegion, Region<'tcx>),
    RegionsOverlyPolymorphic(BoundRegion, Region<'tcx>),
    RegionsPlaceholderMismatch,

    Sorts(ExpectedFound<Ty<'tcx>>),
    IntMismatch(ExpectedFound<ty::IntVarValue>),
    FloatMismatch(ExpectedFound<ast::FloatTy>),
    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>),
    ProjectionBoundsLength(ExpectedFound<usize>),
    ExistentialMismatch(ExpectedFound<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>>),
    ObjectUnsafeCoercion(DefId),
    ConstMismatch(ExpectedFound<&'tcx ty::Const<'tcx>>),

    IntrinsicCast,
}

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: &str, found: &str) -> 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)
            }
        }

        let br_string = |br: ty::BoundRegion| {
            match br {
                ty::BrNamed(_, name) => format!(" {}", name),
                _ => String::new(),
            }
        };

        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)
            }
            Mutability => write!(f, "types differ in mutability"),
            TupleSize(values) => {
                write!(f, "expected a tuple with {} element{}, \
                           found one with {} element{}",
                       values.expected,
                       pluralise!(values.expected),
                       values.found,
                       pluralise!(values.found))
            }
            FixedArraySize(values) => {
                write!(f, "expected an array with a fixed size of {} element{}, \
                           found one with {} element{}",
                       values.expected,
                       pluralise!(values.expected),
                       values.found,
                       pluralise!(values.found))
            }
            ArgCount => {
                write!(f, "incorrect number of function parameters")
            }
            RegionsDoesNotOutlive(..) => {
                write!(f, "lifetime mismatch")
            }
            RegionsInsufficientlyPolymorphic(br, _) => {
                write!(f,
                       "expected bound lifetime parameter{}, found concrete lifetime",
                       br_string(br))
            }
            RegionsOverlyPolymorphic(br, _) => {
                write!(f,
                       "expected concrete lifetime, found bound lifetime parameter{}",
                       br_string(br))
            }
            RegionsPlaceholderMismatch => {
                write!(f, "one type is more general than the other")
            }
            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.def_path_str(values.expected)),
                                       &format!("trait `{}`",
                                                tcx.def_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" })
            }
            ProjectionMismatched(ref values) => ty::tls::with(|tcx| {
                write!(f, "expected {}, found {}",
                       tcx.def_path_str(values.expected),
                       tcx.def_path_str(values.found))
            }),
            ProjectionBoundsLength(ref values) => {
                write!(f, "expected {} associated type binding{}, found {}",
                       values.expected,
                       pluralise!(values.expected),
                       values.found)
            },
            ExistentialMismatch(ref values) => {
                report_maybe_different(f, &format!("trait `{}`", values.expected),
                                       &format!("trait `{}`", values.found))
            }
            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"),
        }
    }
}

impl<'tcx> ty::TyS<'tcx> {
    pub fn sort_string(&self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
        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(),

            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(),
            ty::Array(_, n) => {
                let n = tcx.lift(&n).unwrap();
                match n.try_eval_usize(tcx, ty::ParamEnv::empty()) {
                    Some(n) => {
                        format!("array of {} element{}", n, pluralise!(n)).into()
                    }
                    None => "array".into(),
                }
            }
            ty::Slice(_) => "slice".into(),
            ty::RawPtr(_) => "*-ptr".into(),
            ty::Ref(region, ty, mutbl) => {
                let tymut = ty::TypeAndMut { ty, mutbl };
                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
                {
                    format!("{}reference", match mutbl {
                        hir::Mutability::MutMutable => "mutable ",
                        _ => ""
                    }).into()
                } else {
                    format!("&{}", tymut_string).into()
                }
            }
            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(_) => "type parameter".into(),
            ty::Opaque(..) => "opaque type".into(),
            ty::Error => "type error".into(),
        }
    }
}

impl<'tcx> TyCtxt<'tcx> {
    pub fn note_and_explain_type_err(
        self,
        db: &mut DiagnosticBuilder<'_>,
        err: &TypeError<'tcx>,
        sp: Span,
    ) {
        use self::TypeError::*;

        match err {
            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");
                }
                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.
                        db.help("if both `Future`s have the same `Output` type, consider \
                                 `.await`ing on both of them");
                    }
                }
                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) {
                        if snippet.chars().all(|c| c.is_digit(10) || c == '-' || c == '_') {
                            db.span_suggestion(
                                sp,
                                "use a float literal",
                                format!("{}.0", snippet),
                                Applicability::MachineApplicable
                            );
                        }
                    },
                    (ty::Param(_), ty::Param(_)) => {
                        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(_), _) | (_, ty::Param(_)) => {
                        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");
                    }
                    _ => {}
                }
                debug!(
                    "note_and_explain_type_err expected={:?} ({:?}) found={:?} ({:?})",
                    values.expected,
                    values.expected.kind,
                    values.found,
                    values.found.kind,
                );
            },
            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");
                }
            }
            _ => {}
        }
    }
}