Skip to content

Commit

Permalink
all: support &&= and ||= for bool alias types as well (#21684)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 authored Jun 16, 2024
1 parent df1e4f1 commit bebe943
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7731,4 +7731,5 @@ Assignment Operators
+= -= *= /= %=
&= |= ^=
>>= <<= >>>=
&&= ||=
```
4 changes: 2 additions & 2 deletions vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,10 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
}
.and, .logical_or {
if !c.pref.translated && !c.file.is_translated {
if node.left_type != ast.bool_type_idx {
if left_final_sym.kind != .bool {
c.error('left operand for `${node.op}` is not a boolean', node.left.pos())
}
if node.right_type != ast.bool_type_idx {
if right_final_sym.kind != .bool {
c.error('right operand for `${node.op}` is not a boolean', node.right.pos())
}
}
Expand Down
5 changes: 3 additions & 2 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,9 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
op_overloaded = true
}
}

if left_sym.kind == .bool && right_sym.kind == .bool
final_left_sym := g.table.final_sym(g.unwrap_generic(var_type))
final_right_sym := g.table.final_sym(unwrapped_val_type)
if final_left_sym.kind == .bool && final_right_sym.kind == .bool
&& node.op in [.boolean_or_assign, .boolean_and_assign] {
extracted_op := match node.op {
.boolean_or_assign {
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/tests/bool_assign_operator_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ fn test_bool_assign_operator() {
flag &&= false
assert flag == false
}

type Bool = bool

fn test_alias_bool_assign_operator() {
mut flag := Bool(true)
flag = flag || false
assert flag == true

flag ||= false
assert flag == true

flag &&= false
assert flag == false
}

0 comments on commit bebe943

Please sign in to comment.