Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: add support for alias bool assign operators #21684

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading