Skip to content

Commit

Permalink
cgen: fix map with an Enum as key type, with size < 4 bytes on tcc (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Feb 16, 2025
1 parent 2015aa3 commit eab148e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3396,6 +3396,11 @@ fn (mut g Gen) map_fn_ptrs(key_sym ast.TypeSymbol) (string, string, string, stri
}
.enum {
einfo := (key_sym.info) as ast.Enum
if g.pref.ccompiler_type == .tinyc
&& einfo.typ in [ast.u8_type, ast.u16_type, ast.i8_type, ast.i16_type] {
// workaround for tcc, since we can not generate a packed Enum with size < 4 bytes
return g.map_fn_ptrs(g.table.sym(ast.int_type))
}
return g.map_fn_ptrs(g.table.sym(einfo.typ))
}
.int, .i32, .u32, .rune, .f32 {
Expand Down
19 changes: 19 additions & 0 deletions vlib/v/tests/enum_packed_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module main

import flag
import os

pub enum Number as u8 {
zero = 0
one = 1
six = 6
}

fn test_main() {
mut fp := flag.new_flag_parser(os.args)
_ := fp.finalize()!
mut numbers := map[Number]string{}
numbers[.zero] = '0'
numbers[.one] = '1'
assert numbers.str() == "{zero: '0', one: '1'}"
}

0 comments on commit eab148e

Please sign in to comment.