Skip to content

Commit 902d0dc

Browse files
authored
checker: allow ~T(0) where T is int (#17886)
1 parent 39b3a0c commit 902d0dc

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

vlib/v/checker/checker.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3843,7 +3843,8 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
38433843
c.error('cannot dereference to void', node.pos)
38443844
}
38453845
}
3846-
if node.op == .bit_not && !right_type.is_int() && !c.pref.translated && !c.file.is_translated {
3846+
if node.op == .bit_not && !c.unwrap_generic(right_type).is_int() && !c.pref.translated
3847+
&& !c.file.is_translated {
38473848
c.type_error_for_operator('~', 'integer', right_sym.name, node.pos)
38483849
}
38493850
if node.op == .not && right_type != ast.bool_type_idx && !c.pref.translated

vlib/v/tests/generic_fn_infer_test.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ fn simple[T](p T) T {
55
return p
66
}
77

8+
fn bit_not_op_generic[T]() T {
9+
x := ~T(0)
10+
return x
11+
}
12+
813
fn test_infer() {
914
call(3)
1015
i := 4
@@ -17,6 +22,8 @@ fn test_explicit_calls_should_also_work() {
1722
assert true
1823
simple[int](5)
1924
assert true
25+
x := bit_not_op_generic[u32]()
26+
assert x == u32(4294967295)
2027
}
2128

2229
fn get_type_name[T](x T) string {

0 commit comments

Comments
 (0)