Skip to content

Commit eab148e

Browse files
authored
cgen: fix map with an Enum as key type, with size < 4 bytes on tcc (fix #23714) (#23738)
1 parent 2015aa3 commit eab148e

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3396,6 +3396,11 @@ fn (mut g Gen) map_fn_ptrs(key_sym ast.TypeSymbol) (string, string, string, stri
33963396
}
33973397
.enum {
33983398
einfo := (key_sym.info) as ast.Enum
3399+
if g.pref.ccompiler_type == .tinyc
3400+
&& einfo.typ in [ast.u8_type, ast.u16_type, ast.i8_type, ast.i16_type] {
3401+
// workaround for tcc, since we can not generate a packed Enum with size < 4 bytes
3402+
return g.map_fn_ptrs(g.table.sym(ast.int_type))
3403+
}
33993404
return g.map_fn_ptrs(g.table.sym(einfo.typ))
34003405
}
34013406
.int, .i32, .u32, .rune, .f32 {

‎vlib/v/tests/enum_packed_test.v‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module main
2+
3+
import flag
4+
import os
5+
6+
pub enum Number as u8 {
7+
zero = 0
8+
one = 1
9+
six = 6
10+
}
11+
12+
fn test_main() {
13+
mut fp := flag.new_flag_parser(os.args)
14+
_ := fp.finalize()!
15+
mut numbers := map[Number]string{}
16+
numbers[.zero] = '0'
17+
numbers[.one] = '1'
18+
assert numbers.str() == "{zero: '0', one: '1'}"
19+
}

0 commit comments

Comments
 (0)