branching type resolution doesn't exclude noreturn and other coercible values
Zig Version
0.17.0-dev.292+fc1c83a36
ZLS Version
0.17.0-dev.44+8da87d4f
Client / Code Editor / Extensions
VSCodium (1.116.0) with ziglang.vscode-zig (0.6.18)
Steps to Reproduce and Observed Behavior
const S = struct {
alpha: u32,
};
/// a value that ZLS cannot resolve
const unknown = unknown;
// good
const A = switch (unknown) {
1 => S,
else => unknown,
};
// bad
const B = switch (unknown) {
1 => S,
else => unreachable,
};
// bad
const C = switch (unknown) {
1 => S,
else => undefined,
};
// kind of works. Hover indicates that it isn't cleanly resolve to just `S`
const D = switch (unknown) {
1 => S,
else => @compileError(""),
};
Expected Behavior
The else branch of the switch cases should be ignored in all examples so that A, B, C, D all resolve to S.
Attach ZLS log output
No response
Marking as contributor friendly.
This logic is handled Type.fromEither which doesn't handle noreturn or undefined. The issue with D can be left as a follow up issue as it's specific to hover.
Presumably this reproduction being unexpected behaviour invalidates some test cases?
Namely, currently in tests/analysis/either.zig we have:
const non_type_and_type: if (true) undefined else i32 = unknown_value;
// ^^^^^^^^^^^^^^^^^ (unknown)()
const type_and_non_type: if (true) i32 else undefined = unknown_value;
// ^^^^^^^^^^^^^^^^^ (unknown)()
But the implication is that these should both resolve to i32, as far as I can tell.
Yes, these should resolve to i32. This would also apply to the test below for @compileError("Foo") else i32.
There should be tests for both cases. One for i32 else undefined while the existing test you mentioned should be updated to i32 else 5. Based on the declaration name and commit history, the test is checking what happens when a type and value are mixed in a conditional. That should continue to produce unknown.