checker: allow casting bool to byte (#9676)

pull/9681/head
Enzo 2021-04-11 12:24:24 +02:00 committed by GitHub
parent 85e9cf1bd3
commit a851901620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -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 } 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() && 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) type_name := c.table.type_to_str(node.expr_type)
c.error('cannot cast type `$type_name` to `byte`', node.pos) c.error('cannot cast type `$type_name` to `byte`', node.pos)
} else if to_type_sym.kind == .struct_ && !node.typ.is_ptr() } else if to_type_sym.kind == .struct_ && !node.typ.is_ptr()

View File

@ -30,3 +30,12 @@ fn test_casting_an_int_to_byte() {
b := byte(x) b := byte(x)
assert 'x: $x | b: $b.hex()' == 'x: 12 | b: 0c' 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'
}