From a851901620e86a3fab1bb2cefd3cc7df05c5f68f Mon Sep 17 00:00:00 2001 From: Enzo Date: Sun, 11 Apr 2021 12:24:24 +0200 Subject: [PATCH] checker: allow casting bool to byte (#9676) --- vlib/v/checker/checker.v | 2 +- vlib/v/tests/cast_to_byte_test.v | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 87da3dd193..1a55bd623e 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4431,7 +4431,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { } } else if to_type_sym.kind == .byte && node.expr_type != ast.voidptr_type && from_type_sym.kind != .enum_ && !node.expr_type.is_int() && !node.expr_type.is_float() - && !node.expr_type.is_ptr() { + && node.expr_type != ast.bool_type && !node.expr_type.is_ptr() { type_name := c.table.type_to_str(node.expr_type) c.error('cannot cast type `$type_name` to `byte`', node.pos) } else if to_type_sym.kind == .struct_ && !node.typ.is_ptr() diff --git a/vlib/v/tests/cast_to_byte_test.v b/vlib/v/tests/cast_to_byte_test.v index 5147f81ad9..e5b1b3f878 100644 --- a/vlib/v/tests/cast_to_byte_test.v +++ b/vlib/v/tests/cast_to_byte_test.v @@ -30,3 +30,12 @@ fn test_casting_an_int_to_byte() { b := byte(x) assert 'x: $x | b: $b.hex()' == 'x: 12 | b: 0c' } + +fn test_casting_a_bool_to_byte() { + x := true + b1 := byte(x) + assert 'x: $x | b: $b1.hex()' == 'x: true | b: 01' + y := false + b2 := byte(y) + assert 'y: $y | b: $b2.hex()' == 'y: false | b: 00' +}