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

checker: allow voidptr arithmetic in translated code

This commit is contained in:
Alexander Medvednikov 2022-04-30 05:52:32 +03:00
parent cd30b6ea82
commit ce31a01a70
3 changed files with 6 additions and 3 deletions

View File

@ -1087,7 +1087,7 @@ pub mut:
expr_type Type // from type
}
// an enum value, like OS.macos or .macos
// An enum value, like OS.macos or .macos
pub struct EnumVal {
pub:
enum_name string

View File

@ -589,7 +589,7 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
if !c.inside_unsafe && !node.left.is_auto_deref_var() && !node.right.is_auto_deref_var() {
c.warn('pointer arithmetic is only allowed in `unsafe` blocks', left_right_pos)
}
if left_type == ast.voidptr_type {
if left_type == ast.voidptr_type && !c.pref.translated {
c.error('`$node.op` cannot be used with `voidptr`', left_pos)
}
}
@ -3962,6 +3962,8 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
// with this value.
pub fn (mut c Checker) enum_val(mut node ast.EnumVal) ast.Type {
mut typ_idx := if node.enum_name == '' {
// Get the type of the enum without enum name by looking at the expected type.
// e.g. `set_color(.green)`, V knows that `Green` is the expected type.
if c.expected_type == ast.void_type && c.expected_expr_type != ast.void_type {
c.expected_expr_type.idx()
} else {

View File

@ -5353,7 +5353,8 @@ fn (mut g Gen) enum_val(node ast.EnumVal) {
// && g.inside_switch
if g.pref.translated && node.typ.is_number() {
// Mostly in translated code, when C enums are used as ints in switches
g.write('/*enum val is_number $node.mod styp=$styp*/_const_main__$node.val')
sym := g.table.sym(node.typ)
g.write('/* $node enum val is_number $node.mod styp=$styp sym=$sym*/_const_main__$node.val')
} else {
g.write('${styp}__$node.val')
}