mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix code generation for generic unions (#18513)
This commit is contained in:
parent
a12e82aa15
commit
0e4eea80ca
@ -393,8 +393,12 @@ fn (mut g Gen) struct_decl(s ast.Struct, name string, is_anon bool) {
|
||||
return
|
||||
}
|
||||
if name.contains('_T_') {
|
||||
if s.is_union {
|
||||
g.typedefs.writeln('typedef union ${name} ${name};')
|
||||
} else {
|
||||
g.typedefs.writeln('typedef struct ${name} ${name};')
|
||||
}
|
||||
}
|
||||
// TODO avoid buffer manip
|
||||
start_pos := g.type_definitions.len
|
||||
|
||||
|
28
vlib/v/tests/generics_union_dump_test.v
Normal file
28
vlib/v/tests/generics_union_dump_test.v
Normal file
@ -0,0 +1,28 @@
|
||||
union Convertor[T] {
|
||||
value T
|
||||
bytes [8]u8
|
||||
}
|
||||
|
||||
fn test_conversion_works() {
|
||||
a := Convertor[i64]{
|
||||
value: 21474837714
|
||||
}
|
||||
$if little_endian {
|
||||
assert unsafe { a.bytes } == [u8(210), 4, 0, 0, 5, 0, 0, 0]!
|
||||
}
|
||||
}
|
||||
|
||||
fn test_dumping_of_a_generic_union_value() {
|
||||
dump(Convertor[u8]{
|
||||
bytes: [u8(210), 4, 0, 0, 5, 0, 0, 0]!
|
||||
})
|
||||
dump(Convertor[i16]{
|
||||
bytes: [u8(210), 4, 0, 0, 5, 0, 0, 0]!
|
||||
})
|
||||
dump(Convertor[int]{
|
||||
bytes: [u8(210), 4, 0, 0, 5, 0, 0, 0]!
|
||||
})
|
||||
dump(Convertor[i64]{
|
||||
bytes: [u8(210), 4, 0, 0, 5, 0, 0, 0]!
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user