Skip to content

Commit 5acc1de

Browse files
Improve long error explanations for E0620 and E0621
1 parent ff0fb9d commit 5acc1de

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/librustc_typeck/diagnostics.rs

+30-16
Original file line numberDiff line numberDiff line change
@@ -4666,28 +4666,41 @@ i_am_a_function();
46664666
"##,
46674667

46684668
E0619: r##"
4669-
A not (yet) known type was used.
4669+
The type-checker needed to know the type of an expression, but that type had not
4670+
yet been inferred.
46704671
46714672
Erroneous code example:
46724673
46734674
```compile_fail,E0619
4674-
let x;
4675-
4676-
match x {
4677-
(..) => {} // error: the type of this value must be known in this context
4678-
_ => {}
4675+
let mut x = vec![];
4676+
match x.pop() {
4677+
Some(v) => {
4678+
// Here, the type of `v` is not (yet) known, so we
4679+
// cannot resolve this method call:
4680+
v.to_uppercase(); // error: the type of this value must be known in
4681+
// this context
4682+
}
4683+
None => {}
46794684
}
46804685
```
46814686
4687+
Type inference typically proceeds from the top of the function to the bottom,
4688+
figuring out types as it goes. In some cases -- notably method calls and
4689+
overloadable operators like `*` -- the type checker may not have enough
4690+
information *yet* to make progress. This can be true even if the rest of the
4691+
function provides enough context (because the type-checker hasn't looked that
4692+
far ahead yet). In this case, type annotations can be used to help it along.
4693+
46824694
To fix this error, just specify the type of the variable. Example:
46834695
46844696
```
4685-
let x: i32 = 0; // Here, we say that `x` is an `i32` (and give it a value to
4686-
// avoid another compiler error).
4687-
4688-
match x {
4689-
0 => {} // ok!
4690-
_ => {}
4697+
let mut x: Vec<String> = vec![]; // We precise the type of the vec elements.
4698+
match x.pop() {
4699+
Some(v) => {
4700+
v.to_uppercase(); // Since rustc now knows the type of the vec elements,
4701+
// we can use `v`'s methods.
4702+
}
4703+
None => {}
46914704
}
46924705
```
46934706
"##,
@@ -4702,9 +4715,11 @@ let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]`
47024715
// as `[usize]`
47034716
```
47044717
4705-
In Rust, some types don't have a size at compile-time (like slices and traits
4706-
for example). Therefore, you can't cast into them directly. Try casting to a
4707-
reference instead:
4718+
In Rust, some types don't have a known size at compile-time. For example, in a
4719+
slice type like `[u32]`, the number of elements is not known at compile-time and
4720+
hence the overall size cannot be computed. As a result, such types can only be
4721+
manipulated through a reference (e.g., `&T` or `&mut T`) or other pointer-type
4722+
(e.g., `Box` or `Rc`). Try casting to a reference instead:
47084723
47094724
```
47104725
let x = &[1_usize, 2] as &[usize]; // ok!
@@ -4782,5 +4797,4 @@ register_diagnostics! {
47824797
E0568, // auto-traits can not have predicates,
47834798
E0588, // packed struct cannot transitively contain a `[repr(align)]` struct
47844799
E0592, // duplicate definitions with name `{}`
4785-
E0619, // intrinsic must be a function
47864800
}

0 commit comments

Comments
 (0)