M src/check.c => src/check.c +1 -0
@@ 3050,6 3050,7 @@ check_expr_switch(struct context *ctx,
"Invalid type for switch case");
return;
}
+ value = lower_implicit_cast(ctx, type, value);
enum eval_result r = eval_expr(ctx, value, evaled);
if (r != EVAL_OK) {
M tests/14-switch.ha => tests/14-switch.ha +28 -0
@@ 1,5 1,8 @@
use rt::{compile, exited, EXIT_SUCCESS, toutf8};
+type t = int;
+type e = !int;
+
let x = 1337;
fn basics() void = {
@@ 39,6 42,24 @@ fn basics() void = {
case => abort();
};
+ // assignability
+ switch (0) {
+ case 0i8 => void;
+ case 1i16 => abort();
+ case 2: !int => abort();
+ case 3: t => abort();
+ case 4: !t => abort();
+ case => abort();
+ };
+
+ // regression test
+ const x: e = 0;
+ switch (x) {
+ case 0 => void;
+ case 1: e => abort();
+ case => abort();
+ };
+
assert(compile("
fn test() void = switch (0.0) {
case 0.0 => void;
@@ 60,6 81,13 @@ fn basics() void = {
case => void;
};
") as exited != EXIT_SUCCESS);
+
+ assert(compile("
+ fn test() void = switch (0i32) {
+ case 0i64 => void;
+ case => void;
+ };
+ ") as exited != EXIT_SUCCESS);
};
fn tagged_result() void = {