You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the code sample below, there are two ultimately identical functions with the same problem, but while the compiler correctly reports the incorrect match block in the first function (test_func1()), it fails to do so in the second (test_func2()):
fnmain(){let _ = test_func1(1);let _ = test_func2(1);}fntest_func1(n:i32) -> i32{match n {12 => 'b',
_ => 42,}}fntest_func2(n:i32) -> i32{let x = match n {12 => 'b',
_ => 42,};
x
}
which produces the following correct error for the first function:
error[E0308]: match arms have incompatible types
--> ./test.rs:7:5
|
7 | / match n {
8 | | 12 => 'b',
| | --- match arm with an incompatible type
9 | | _ => 42,
10 | | }
| |_____^ expected i32, found char
but an incorrect error for the second message:
error[E0308]: match arms have incompatible types
--> ./test.rs:14:13
|
14 | let x = match n {
| _____________^
15 | | 12 => 'b',
16 | | _ => 42,
| | -- match arm with an incompatible type
17 | | };
| |_____^ expected char, found integral variable
|
= note: expected type `char`
found type `{integer}`
When test_func2() is extended with an explicit type hint (that should not be necessary as it can be derived from the existing code), the resulting error message is correct once again:
fntest_func3(n:i32) -> i32{let x:i32 = match n {12 => 'b',
_ => 42,};
x
}
producing
error[E0308]: match arms have incompatible types
--> ./test.rs:23:18
|
23 | let x: i32 = match n {
| __________________^
24 | | 12 => 'b',
| | --- match arm with an incompatible type
25 | | _ => 42,
26 | | };
| |_____^ expected i32, found char
The text was updated successfully, but these errors were encountered:
This is because the compiler is trying to unify a type for the entire match block before it gets to unifying x and the return type i32. Because of this, it attempts to unify every single arm going in order. In this case, that means it takes char first and assumes that the rest of the match arms will have to be char too. It will be hard to avoid these errors without delaying the emission of them until after the entire block has unified...
Tweak "incompatible match arms" error
- Point at the body expression of the match arm with the type error.
- Point at the prior match arms explicitly stating the evaluated type.
- Point at the entire match expr in a secondary span, instead of primary.
- For type errors in the first match arm, the cause is outside of the
match, treat as implicit block error to give a more appropriate error.
Fixrust-lang#46776, fixrust-lang#57206.
CC rust-lang#24157, rust-lang#38234.
Centril
added a commit
to Centril/rust
that referenced
this issue
Feb 14, 2019
Tweak "incompatible match arms" error
- Point at the body expression of the match arm with the type error.
- Point at the prior match arms explicitly stating the evaluated type.
- Point at the entire match expr in a secondary span, instead of primary.
- For type errors in the first match arm, the cause is outside of the
match, treat as implicit block error to give a more appropriate error.
Fixrust-lang#46776, fixrust-lang#57206.
CC rust-lang#24157, rust-lang#38234.
In the code sample below, there are two ultimately identical functions with the same problem, but while the compiler correctly reports the incorrect match block in the first function (
test_func1()
), it fails to do so in the second (test_func2()
):which produces the following correct error for the first function:
but an incorrect error for the second message:
When
test_func2()
is extended with an explicit type hint (that should not be necessary as it can be derived from the existing code), the resulting error message is correct once again:producing
The text was updated successfully, but these errors were encountered: