Skip to content

Commit cc227d8

Browse files
committed
checker: fix non-numeric type check for translated code
1 parent d10135e commit cc227d8

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

vlib/v/checker/checker.v

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,8 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
651651
}
652652
return ast.bool_type
653653
}
654-
.plus, .minus, .mul, .div, .mod, .xor, .amp, .pipe { // binary operators that expect matching types
654+
.plus, .minus, .mul, .div, .mod, .xor, .amp, .pipe {
655+
// binary operators that expect matching types
655656
if right_sym.info is ast.Alias && (right_sym.info as ast.Alias).language != .c
656657
&& c.mod == c.table.type_to_str(right_type).split('.')[0]
657658
&& c.table.sym((right_sym.info as ast.Alias).parent_type).is_primitive() {
@@ -3515,9 +3516,9 @@ pub fn (mut c Checker) postfix_expr(mut node ast.PostfixExpr) ast.Type {
35153516
if !c.inside_unsafe && is_non_void_pointer && !node.expr.is_auto_deref_var() {
35163517
c.warn('pointer arithmetic is only allowed in `unsafe` blocks', node.pos)
35173518
}
3518-
if !(typ_sym.is_number() || (c.inside_unsafe && is_non_void_pointer)) {
3519-
c.error('invalid operation: $node.op.str() (non-numeric type `$typ_sym.name`)',
3520-
node.pos)
3519+
if !(typ_sym.is_number() || ((c.inside_unsafe || c.pref.translated) && is_non_void_pointer)) {
3520+
typ_str := c.table.type_to_str(typ)
3521+
c.error('invalid operation: $node.op.str() (non-numeric type `$typ_str`)', node.pos)
35213522
} else {
35223523
node.auto_locked, _ = c.fail_if_immutable(node.expr)
35233524
}

vlib/v/checker/tests/pointer_ops.out

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@ vlib/v/checker/tests/pointer_ops.vv:10:3: error: operator `-=` not defined on le
3939
10 | p -= 3
4040
| ^
4141
11 | _ = p[3]
42-
12 | }
42+
12 | mut foo := &Foo{}
4343
vlib/v/checker/tests/pointer_ops.vv:11:8: error: type `voidptr` does not support indexing
4444
9 | p--
4545
10 | p -= 3
4646
11 | _ = p[3]
4747
| ~~~
48-
12 | }
49-
13 | }
48+
12 | mut foo := &Foo{}
49+
13 | foo % 3
50+
vlib/v/checker/tests/pointer_ops.vv:13:3: error: invalid operator `%` to `&Foo` and `int literal`
51+
11 | _ = p[3]
52+
12 | mut foo := &Foo{}
53+
13 | foo % 3
54+
| ~~~~~~~
55+
14 | }
56+
15 | }

vlib/v/checker/tests/pointer_ops.vv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ fn test_voidptr() {
99
p--
1010
p -= 3
1111
_ = p[3]
12+
mut foo := &Foo{}
13+
foo % 3
1214
}
1315
}
16+
17+
struct Foo{}

vlib/v/util/util.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const builtin_module_names = ['builtin', 'strconv', 'strings', 'dlmalloc']
4141
pub fn module_is_builtin(mod string) bool {
4242
// NOTE: using util.builtin_module_parts here breaks -usecache on macos
4343
return mod in util.builtin_module_names
44+
// return mod in util.builtin_module_parts
4445
}
4546

4647
pub fn tabs(n int) string {

0 commit comments

Comments
 (0)