Improve long error explanations for E0620 and E0621

This commit is contained in:
Guillaume Gomez 2017-06-27 23:07:56 +02:00
parent ff0fb9d906
commit 5acc1deaf8

View File

@ -4666,28 +4666,41 @@ fn i_am_a_function() {}
"##, "##,
E0619: r##" E0619: r##"
A not (yet) known type was used. The type-checker needed to know the type of an expression, but that type had not
yet been inferred.
Erroneous code example: Erroneous code example:
```compile_fail,E0619 ```compile_fail,E0619
let x; let mut x = vec![];
match x.pop() {
match x { Some(v) => {
(..) => {} // error: the type of this value must be known in this context // Here, the type of `v` is not (yet) known, so we
_ => {} // cannot resolve this method call:
v.to_uppercase(); // error: the type of this value must be known in
// this context
}
None => {}
} }
``` ```
Type inference typically proceeds from the top of the function to the bottom,
figuring out types as it goes. In some cases -- notably method calls and
overloadable operators like `*` -- the type checker may not have enough
information *yet* to make progress. This can be true even if the rest of the
function provides enough context (because the type-checker hasn't looked that
far ahead yet). In this case, type annotations can be used to help it along.
To fix this error, just specify the type of the variable. Example: To fix this error, just specify the type of the variable. Example:
``` ```
let x: i32 = 0; // Here, we say that `x` is an `i32` (and give it a value to let mut x: Vec<String> = vec![]; // We precise the type of the vec elements.
// avoid another compiler error). match x.pop() {
Some(v) => {
match x { v.to_uppercase(); // Since rustc now knows the type of the vec elements,
0 => {} // ok! // we can use `v`'s methods.
_ => {} }
None => {}
} }
``` ```
"##, "##,
@ -4702,9 +4715,11 @@ fn i_am_a_function() {}
// as `[usize]` // as `[usize]`
``` ```
In Rust, some types don't have a size at compile-time (like slices and traits In Rust, some types don't have a known size at compile-time. For example, in a
for example). Therefore, you can't cast into them directly. Try casting to a slice type like `[u32]`, the number of elements is not known at compile-time and
reference instead: hence the overall size cannot be computed. As a result, such types can only be
manipulated through a reference (e.g., `&T` or `&mut T`) or other pointer-type
(e.g., `Box` or `Rc`). Try casting to a reference instead:
``` ```
let x = &[1_usize, 2] as &[usize]; // ok! let x = &[1_usize, 2] as &[usize]; // ok!
@ -4782,5 +4797,4 @@ fn i_am_a_function() {}
E0568, // auto-traits can not have predicates, E0568, // auto-traits can not have predicates,
E0588, // packed struct cannot transitively contain a `[repr(align)]` struct E0588, // packed struct cannot transitively contain a `[repr(align)]` struct
E0592, // duplicate definitions with name `{}` E0592, // duplicate definitions with name `{}`
E0619, // intrinsic must be a function
} }