1
0
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:
Delyan Angelov 2023-06-22 22:39:05 +03:00 committed by GitHub
parent a12e82aa15
commit 0e4eea80ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -393,7 +393,11 @@ fn (mut g Gen) struct_decl(s ast.Struct, name string, is_anon bool) {
return
}
if name.contains('_T_') {
g.typedefs.writeln('typedef struct ${name} ${name};')
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

View 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]!
})
}