Bug 24435 – [ImportC] Compilation fails when casting to struct defined in separate file

Status
NEW
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2024-03-11T02:44:55Z
Last change time
2024-12-13T19:33:48Z
Assigned to
No Owner
Creator
Lance Bachmeier
Moved to GitHub: dmd#18220 →

Comments

Comment #0 by lance — 2024-03-11T02:44:55Z
structdef.c: typedef struct { int a; double b; } foo; test.c: #include <stdio.h> #include <stdlib.h> __import structdef; int main() { int * x = (int *) malloc(2*sizeof(int)); x[0] = 1; x[1] = 2; printf("%d\n", x[0]); printf("%d\n", x[1]); // Fails to compile because it's imported foo * y = (foo *) malloc(sizeof(foo)); } Compile with DMD 2.108.0-beta.1: test.c(13): Error: expression expected, not `)` test.c(13): Error: found `malloc` when expecting `)` If the definition of foo is moved into test.c, it compiles and runs. The error only occurs when foo is defined in a separate file.
Comment #1 by lance — 2024-03-26T14:48:25Z
This is a preprocessor problem with the typedef. The following works: structdef.c typedef struct foo_ { int a; double b; } foo; structtest.c #include <stdio.h> #include <stdlib.h> __import structdef; int main() { int * x = (int *) malloc(2*sizeof(int)); x[0] = 1; x[1] = 2; printf("%d\n", x[0]); printf("%d\n", x[1]); // Works foo * y = (struct foo_ *) malloc(sizeof(foo)); // Fails to compile because it's imported and typedef'd foo * y = (foo *) malloc(sizeof(foo)); }
Comment #2 by robert.schadek — 2024-12-13T19:33:48Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18220 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB