Issue 24121 - ImportC: typedef enum fails to compile when generating .di file
Summary: ImportC: typedef enum fails to compile when generating .di file
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 normal
Assignee: Walter Bright
URL:
Keywords: ImportC, pull, rejects-valid
: 24201 (view as issue list)
Depends on:
Blocks:
 
Reported: 2023-08-30 15:05 UTC by Adam Wilson
Modified: 2023-10-27 07:44 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adam Wilson 2023-08-30 15:05:50 UTC
The following C code causes a segfault in DMD:

typedef enum
{
	SQL_IS_YEAR				= 1,
	SQL_IS_MONTH				= 2,
	SQL_IS_DAY				= 3,
	SQL_IS_HOUR				= 4,
	SQL_IS_MINUTE				= 5,
	SQL_IS_SECOND				= 6,
	SQL_IS_YEAR_TO_MONTH			= 7,
	SQL_IS_DAY_TO_HOUR			= 8,
	SQL_IS_DAY_TO_MINUTE			= 9,
	SQL_IS_DAY_TO_SECOND			= 10,
	SQL_IS_HOUR_TO_MINUTE			= 11,
	SQL_IS_HOUR_TO_SECOND			= 12,
	SQL_IS_MINUTE_TO_SECOND			= 13
} SQLINTERVAL;

May be a regression of: https://issues.dlang.org/show_bug.cgi?id=22726
Comment 1 Rory 2023-09-01 11:16:44 UTC
I came here to report a similar issue with typedef enums but my code now works. I tried the enum posted here and the code compiles fine for me.

Do you know which version of DMD this segfault was in?

I'm using linux DMD64 D Compiler v2.105.0.
Comment 2 Adam Wilson 2023-09-02 09:59:10 UTC
(In reply to Rory from comment #1)
> I came here to report a similar issue with typedef enums but my code now
> works. I tried the enum posted here and the code compiles fine for me.
> 
> Do you know which version of DMD this segfault was in?
> 
> I'm using linux DMD64 D Compiler v2.105.0.

I am using Windows DMD64 2.105 and I just verified the segfault.

The commandline used is: dmd sql.i -Hf=sql.di -verrors=0 -main

If I remove the -Hf=sql.di switch then it works.
Comment 3 Walter Bright 2023-09-12 00:29:07 UTC
The problem is that C code is not always representable as D code. D doesn't have anonymous enum types, for example, which I'm pretty sure is the cause of the seg fault.

One reason for ImportC is it enables the D compiler to accommodate exact C semantics. The D compiler can import a C file, and use its declarations directly. If the C file was converted to a .di file, then the D compiler can import the .di file and use its declarations directly.

Consequently, there should be no need to covert a C file to a .di file.
Comment 4 Walter Bright 2023-10-25 19:26:14 UTC
Adam and Bruce have convinced me that there is indeed a need for C to di conversion, so I'll see about addressing this.
Comment 5 Walter Bright 2023-10-25 22:54:25 UTC
Currently, the .i file:

typedef enum
{
    SQL_IS_YEAR  = 1,
    SQL_IS_MONTH = 2
} SQLINTERVAL;


produces a .di file:

// D import file generated from 'test.i'
extern (C)
{
        align enum SQLINTERVAL
        {
                SQL_IS_YEAR = 1,
                SQL_IS_MONTH = 2,
        }
        alias SQLINTERVAL = enum SQLINTERVAL;
}

which is just wrong.
Comment 6 Walter Bright 2023-10-27 01:48:09 UTC
*** Issue 24201 has been marked as a duplicate of this issue. ***
Comment 7 Dlang Bot 2023-10-27 06:12:32 UTC
@WalterBright created dlang/dmd pull request #15736 "fix Issue 24121 - ImportC: typedef enum fails to compile when generat…" fixing this issue:

- fix Issue 24121 - ImportC: typedef enum fails to compile when generating .di file

https://github.com/dlang/dmd/pull/15736
Comment 8 Dlang Bot 2023-10-27 07:44:31 UTC
dlang/dmd pull request #15736 "fix Issue 24121 - ImportC: typedef enum fails to compile when generat…" was merged into master:

- 7a3ae70494706f49aa8b54f8752d3f2444523a41 by Walter Bright:
  fix Issue 24121 - ImportC: typedef enum fails to compile when generating .di file

https://github.com/dlang/dmd/pull/15736