fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

C compiler warns about uninitialized fzCur

Open fridis opened this issue 2 years ago • 2 comments

After #1187 has been pulled in, the following example works interpreted, but causes a warning in the C backend:

a        Any is a.this
b(x i32) Any is b.this
_ := a
_ := b 3
 > ./build/bin/fz -c m4d.fz
#universe.c:553:21: error: variable 'fzCur' is uninitialized when used here [-Werror,-Wuninitialized]
    fzM_0->fields = fzCur;
                    ^~~~~
#universe.c:543:3: note: variable 'fzCur' is declared here
  fzT_a fzCur;
  ^
1 error generated.

error 1: C backend: C compiler failed
C compiler call 'clang -Wall -Werror -Wno-gnu-empty-struct -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable -Wno-unused-function -Wno-infinite-recursion -O3 -lm -lpthread -o #universe #universe.c' failed with exit code '1'

one error.

The problem is that fzT_a contains only the result field, which is unaccessible anyway but never initialized. For fzt_b, the same problem exists, just the C compiler does not detect it because firld x is initialized. We might have to disable the uninitialized warnings or try to find a way to exclude the fzCur local vars from these warnings.

fridis avatar Mar 02 '23 17:03 fridis

It is possible to selectively disable warnings in both GCC and clang: https://nelkinda.com/blog/suppress-warnings-in-gcc-and-clang/

maxteufel avatar Mar 07 '23 08:03 maxteufel

Disabling these warnings seems a bit unsatisfying to me. The question is how to handle it better? What happens when fzC_a is called and fzCur is assigned to ->fields. Is it guaranteed to not crash? My C knowledge is not sufficient here unfortunately.

// code for clazz#117 a:
fzT__RAny* fzC_a()
{
  // instance does not escape, put it on stack
  fzT_a fzCur;

  start:
  {
    //    0: Current
    //    1: Box a => ref a
    // Box a
    fzT__Ra* fzM_0;
    fzM_0 = malloc(sizeof(fzT__Ra));
    fzM_0->clazzId = 116;
    fzM_0->fields = fzCur;
    //    2: Current
    //    3: Assign to a.result
    fzCur.fzF_0_result = (fzT__RAny*)fzM_0;
    return fzCur.fzF_0_result;
  }
}

michaellilltokiwa avatar Mar 07 '23 08:03 michaellilltokiwa