match: else required even if all cases are covered
Describe the bug
In the following code, the match is covering all cases but compiling still trigger an error saying an else is required.
Reproduction Steps
fn perlin_grad_2d(hash int, x f64, y f64) f64 {
return match hash & 0xF {
0x0 { x + y }
0x1 { -x + y }
0x2 { x - y }
0x3 { -x - y }
0x4 { x }
0x5 { -x }
0x6 { x }
0x7 { -x }
0x8 { y }
0x9 { -y }
0xA { y }
0xB { -y }
0xC { y + x }
0xD { -y }
0xE { y - x }
0xF { -y }
}
}
fn main() {
nb := perlin_grad_2d(5, 2.3, 5.4)
println(nb)
}
Expected Behavior
The code should instead compile correctly.
Current Behavior
It trigger this error :
C:\Users\loicp\Documents\GitHub\vsl>v run test.v
test.v:2:9: error: match must be exhaustive (add `else {}` at the end)
1 | fn perlin_grad_2d(hash int, x f64, y f64) f64 {
2 | return match hash & 0xF {
| ~~~~~~~~~~~~~~~~~~
3 | 0x0 { x + y }
4 | 0x1 { -x + y }
Possible Solution
I think compiler check the type of hash instead of hash & 0xF to determine if all cases are covered.
Additional Information/Context
No response
V version
V 0.4.10 86536e4
Environment details (OS name and version, etc.)
| V full version | V 0.4.10 dd083e7687d0e4aafd99bdf9f34011757d6e0628.86536e4 |
|---|---|
| OS | windows, Microsoft Windows 11 Professionnel 26100 64-bit |
| Processor | 16 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i9-11900K @ 3.50GHz |
| Memory | 18.95GB/31.83GB |
| V executable | C:\Users\loicp\Documents\GitHub\v\v.exe |
| V last modified time | 2025-03-31 22:15:36 |
| V home dir | OK, value: C:\Users\loicp\Documents\GitHub\v |
| VMODULES | OK, value: C:\Users\loicp.vmodules |
| VTMP | OK, value: C:\Users\loicp\AppData\Local\Temp\v_0 |
| Current working dir | OK, value: C:\Users\loicp\Documents\GitHub\vsl |
| Git version | git version 2.44.0.windows.1 |
| V git status | weekly.2025.03-437-g86536e45 |
| .git/config present | true |
| cc version | N/A |
| gcc version | gcc (GCC) 11.2.0 |
| clang version | N/A |
| msvc version | N/A |
| tcc version | tcc version 0.9.27 (x86_64 Windows) |
| tcc git status | thirdparty-windows-amd64 b425ac82 |
| emcc version | N/A |
| glibc version | N/A |
[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.
Connected to Huly®: V_0.6-22490
Here V is requesting an else branch that will have dead code. I think is difficult for the compiler to consider all potential cases to prevent dead code. Recalls me this issue.
Next function match covers all u8 cases
fn func(u u8) {
match u {
0...254 {
println('low')
}
255 {
println('hi')
}
}
}
even though the compiler says:
code.v:2:2: error: match must be exhaustive (add `else {}` at the end)
1 | fn func(u u8) {
2 | match u {
| ~~~~~~~~~
3 | 0...254 {
4 | println('low')
我觉得应该要else{}, 方便以后修改代码。
我觉得应该要else{}, 方便以后修改代码。
I agree