1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: allow ~T(0) where T is int (#17886)

This commit is contained in:
Swastik Baranwal 2023-04-05 15:35:18 +05:30 committed by GitHub
parent 39b3a0ca17
commit 902d0dc93d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -3843,7 +3843,8 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
c.error('cannot dereference to void', node.pos)
}
}
if node.op == .bit_not && !right_type.is_int() && !c.pref.translated && !c.file.is_translated {
if node.op == .bit_not && !c.unwrap_generic(right_type).is_int() && !c.pref.translated
&& !c.file.is_translated {
c.type_error_for_operator('~', 'integer', right_sym.name, node.pos)
}
if node.op == .not && right_type != ast.bool_type_idx && !c.pref.translated

View File

@ -5,6 +5,11 @@ fn simple[T](p T) T {
return p
}
fn bit_not_op_generic[T]() T {
x := ~T(0)
return x
}
fn test_infer() {
call(3)
i := 4
@ -17,6 +22,8 @@ fn test_explicit_calls_should_also_work() {
assert true
simple[int](5)
assert true
x := bit_not_op_generic[u32]()
assert x == u32(4294967295)
}
fn get_type_name[T](x T) string {