c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

Extern items referenced in two functions cause "name not declared" error

Open chrysn opened this issue 3 years ago • 0 comments

If two functions in a transpilation unit both reference the same static item, the second can't be compiled:

/* test.c */

int first(void)
{
    extern int array[32];
    return array[1];
}

int second(void)
{
    extern int array[32];
    return array[2];
}
[{
  "arguments": ["clang", "test.c"],
  "directory": "/tmp/c2rust-issue",
  "file": "/tmp/c2rust-issue/test.c",
  "output": "/tmp/c2rust-issue/test.o"
}]
$ c2rust transpile test.json --fail-on-error --overwrite-existing
...
Running without flags.
Transpiling test.c
error: Failed to translate second: name not declared: 'array'

thread 'main' panicked at 'Translation failed, see error above', c2rust-transpile/src/translator/mod.rs:505:9
...

Transpilation works fine if it's only one of the two functions is present.

I haven't found time yet to investigate where in C2Rust this is failing.

[edit: Anyone curious about why one'd do that: it is happening in RIOT code when static inline functions use a global lookup table, and because the functions are static inline one would rather not declare the whole array as extern where the functions are defined, as that'd make the array's name visible for everyone who uses the header file.]

chrysn avatar May 28 '22 16:05 chrysn