@@ -4666,28 +4666,41 @@ i_am_a_function();
4666
4666
"## ,
4667
4667
4668
4668
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.
4670
4671
4671
4672
Erroneous code example:
4672
4673
4673
4674
```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 => {}
4679
4684
}
4680
4685
```
4681
4686
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
+
4682
4694
To fix this error, just specify the type of the variable. Example:
4683
4695
4684
4696
```
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 => {}
4691
4704
}
4692
4705
```
4693
4706
"## ,
@@ -4702,9 +4715,11 @@ let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]`
4702
4715
// as `[usize]`
4703
4716
```
4704
4717
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:
4708
4723
4709
4724
```
4710
4725
let x = &[1_usize, 2] as &[usize]; // ok!
@@ -4782,5 +4797,4 @@ register_diagnostics! {
4782
4797
E0568 , // auto-traits can not have predicates,
4783
4798
E0588 , // packed struct cannot transitively contain a `[repr(align)]` struct
4784
4799
E0592 , // duplicate definitions with name `{}`
4785
- E0619 , // intrinsic must be a function
4786
4800
}
0 commit comments