Skip to content

unions and structs are not translated correctly if typedef is not used #190

@dy-tea

Description

@dy-tea

Here is some C code that uses unions:

#include <stdio.h>

int main() {
  union {
    int i;
    int j;
  } u;

  u.i = 3;
  u.j = 5;

  printf("%d, %d\n", u.i, u.j);

  return 0;
}

I run v translate test.c and it outputs the below test.v:

@[translated]
module main

fn main() {
	u.i = 3
	u.j = 5
	C.printf(c'%d, %d\n', u.i, u.j)
	return
}

The above code will not compile successfully, as u is not defined:

test.v:5:2: error: undefined ident: `u`
    3 | 
    4 | fn main() {
    5 |     u.i = 3
      |     ^
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
test.v:5:4: error: `u` does not return a value
    3 | 
    4 | fn main() {
    5 |     u.i = 3
      |       ^
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
test.v:5:4: error: unexpected symbol `void`
    3 | 
    4 | fn main() {
    5 |     u.i = 3
      |       ^
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
test.v:6:2: error: undefined ident: `u`
    4 | fn main() {
    5 |     u.i = 3
    6 |     u.j = 5
      |     ^
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
    8 |     return
test.v:6:4: error: `u` does not return a value
    4 | fn main() {
    5 |     u.i = 3
    6 |     u.j = 5
      |       ^
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
    8 |     return
test.v:6:4: error: unexpected symbol `void`
    4 | fn main() {
    5 |     u.i = 3
    6 |     u.j = 5
      |       ^
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
    8 |     return
test.v:7:24: error: undefined ident: `u`
    5 |     u.i = 3
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
      |                           ^
    8 |     return
    9 | }
test.v:7:26: error: `u` does not return a value
    5 |     u.i = 3
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
      |                             ^
    8 |     return
    9 | }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions