From ba9aad1d92c6cedb2875c9326076988124b00001 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 5 Jan 2022 15:27:10 +0800 Subject: [PATCH] checker: fix cast to byte in comptime if (#13036) --- vlib/v/checker/checker.v | 2 +- vlib/v/tests/cast_in_comptime_if_test.v | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/cast_in_comptime_if_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b97e6de224..59b7cafcda 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2629,7 +2629,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { // node.typ: `Outside` node.expr_type = c.expr(node.expr) // type to be casted - mut from_type := node.expr_type + mut from_type := c.unwrap_generic(node.expr_type) from_sym := c.table.sym(from_type) final_from_sym := c.table.final_sym(from_type) diff --git a/vlib/v/tests/cast_in_comptime_if_test.v b/vlib/v/tests/cast_in_comptime_if_test.v new file mode 100644 index 0000000000..bd3f8d45ea --- /dev/null +++ b/vlib/v/tests/cast_in_comptime_if_test.v @@ -0,0 +1,18 @@ +fn test_cast_in_comptime_if() { + generic_bool(true) +} + +fn generic_bool(val T) { + $if T is bool { + println(byte(val)) + assert byte(val) == 1 + + println(i8(val)) + assert i8(val) == 1 + + println(i16(val)) + assert i16(val) == 1 + } $else { + assert false + } +}