Bug 24022 – ImportC: Error: attribute `__anonymous` is used as a type

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-06-28T14:07:21Z
Last change time
2023-08-14T06:38:18Z
Keywords
ImportC
Assigned to
Dmytro Katyukha
Creator
Dmytro Katyukha
See also
https://issues.dlang.org/show_bug.cgi?id=23662

Comments

Comment #0 by firemage.dima — 2023-06-28T14:07:21Z
Hi, This issue is related to Issue 23662. Minimal example =============== ```c // testlib.c typedef enum { A = 1, } E; ``` ```d // test.d import testlib; auto some_d_func(E v) { return v; } void main(string[] args) { E expected = A; auto res = some_d_func(A); assert (res == A); assert (res == expected); } ``` This example produces following error: ``` dmd test.d testlib.c test.d(3): Error: attribute `__anonymous` is used as a type ``` Other example ============= ```c // testlib.c typedef enum { A = 1, } E; ``` ```d // test2.d import testlib; auto some_d_other_func() { const struct R { E r; this(in E vparam) { r = vparam; } } return R(A); } void main(string[] args) { E expected = A; auto res = some_d_other_func(A); assert (res.r == A); assert (res.r == expected); } ``` This example produces following error: ``` dmd test2.d testlib.c test2.d(5): Error: attribute `__anonymous` is used as a type test2.d(7): Error: attribute `__anonymous` is used as a type test2.d(16): Error: attribute `__anonymous` is used as a type ``` Real example with libpq ======================= ```c //libpq.c #include <postgresql/libpq-fe.h> #include <postgresql/postgres_ext.h> ``` ```d // sample code from some d file. @safe struct Result { // ... auto status() { const struct ResultStatus { ExecStatusType statusType; this(in ExecStatusType statusType) { this.statusType = statusType; } /// Return string representation of result status string toString() const { return PQresStatus(statusType).fromStringz.idup; } } return ResultStatus(PQresultStatus(_result._pg_result)); } } // .... ```
Comment #1 by dlang-bot — 2023-06-28T16:39:18Z
@katyukha created dlang/dmd pull request #15365 "Implement test case for Issue 24022" mentioning this issue: - Implement test case for Issue 24022 https://github.com/dlang/dmd/pull/15365
Comment #2 by carsten.schlote — 2023-06-30T08:05:25Z
Hi, i stumbled over this regession bug, too. Simplyfied example to reproduce: enum_decl.c: --------------- /// @brief Type ID for signature block typedef enum FWSIG_TYPE { E_FWSIG_TYPE_RSA2048 = 0, E_FWSIG_TYPE_RSA3072, E_FWSIG_TYPE_RSA4096, E_FWSIG_TYPE_EC256 = 16, E_FWSIG_TYPE_ECP384, E_FWSIG_TYPE_ECP521, E_FWSIG_TYPE_ED25519 = 32, E_FWSIG_TYPE_UNKNOWN = -1 } E_FWSIG_TYPE; --------------- use_enum_decl.d: --------------- import enum_decl; E_FWSIG_TYPE getEnumType(string blah) { E_FWSIG_TYPE type = E_FWSIG_TYPE_UNKNOWN; return type; } --------------- $ dmd --version DMD64 D Compiler v2.103.1 $ dmd -c use_enum_decl.d use_enum_decl.d(3): Error: attribute `__anonymous` is used as a type $ dmd-beta --version DMD64 D Compiler v2.104.1-beta.1-163-gb549aba8d6 $ dmd-beta -c use_enum_decl.d use_enum_decl.d(3): Error: attribute `__anonymous` is used as a type $ ldc2 --version LDC - the LLVM D compiler (1.32.1): based on DMD v2.102.2 and LLVM 15.0.7 $ ldc2 -c use_enum_decl.d # Compiles fine as previous versions of DMD did.
Comment #3 by firemage.dima — 2023-06-30T18:06:43Z
The fix seems to be ready: https://github.com/dlang/dmd/pull/15365 Waiting for review. @Carsten Schlote, Please, test the fix on your example (if you have time).
Comment #4 by schlote — 2023-07-03T08:00:14Z
I rebased the MR to stable (v2.104.1-2-g5693cbcdd0), compiled it and tested it against my example. Compiles now without error and produces senseful code.
Comment #5 by dlang-bot — 2023-07-05T11:00:53Z
dlang/dmd pull request #15365 "ImportC: Issue 24022 - Error: attribute `__anonymous` is used as a type" was merged into stable: - 0c004d2fb731b063cfb2523d67f5d107987915a5 by Dmytro Katyukha: Implement test case for Issue 24022 - 32744ee558c39930c30bd88a2bff725e72761a78 by Dmytro Katyukha: [FIX] Issue 24022 Bug investigation info ====================== Currently (before this fix) on attempt to use `enum` declared as `typedef enum {A=1} E;` as type of argument in D function, it (`enum`) will be resolved as `kind=attribute` on during semantic analysis for D ([typesem.d](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/typesem.d#L1504)), but it have to be resolved as type. The `enum` declared this way is handled by this [code](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1907), and `declareTag` returns the attribute instead of type, but later, there is [code that create alias for attribute](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1918C29-L1918C79), though, it seems, that alias have to be created to type, instead of attribute. Investigating [declareTag](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1715) function shows that the returned value changed by call to [applySpecifier](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L5234), that applies `AlignDeclaration` when `if (!specifier.packalign.isDefault())`. Fix info ======== With this commit the alias will be created to TypeTag, instead of attribute produced by declared tag. Also, the code simplified, because there is no more need for special handling of enums in modified piece of code. https://github.com/dlang/dmd/pull/15365
Comment #6 by dlang-bot — 2023-07-12T14:41:29Z
@RazvanN7 updated dlang/dmd pull request #15406 "Fix Issue 23951 - traits(getMember) does not follow alias this" mentioning this issue: - ImportC: Issue 24022 - Error: attribute `__anonymous` is used as a type (#15365) * Implement test case for Issue 24022 * [FIX] Issue 24022 Bug investigation info ====================== Currently (before this fix) on attempt to use `enum` declared as `typedef enum {A=1} E;` as type of argument in D function, it (`enum`) will be resolved as `kind=attribute` on during semantic analysis for D ([typesem.d](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/typesem.d#L1504)), but it have to be resolved as type. The `enum` declared this way is handled by this [code](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1907), and `declareTag` returns the attribute instead of type, but later, there is [code that create alias for attribute](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1918C29-L1918C79), though, it seems, that alias have to be created to type, instead of attribute. Investigating [declareTag](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1715) function shows that the returned value changed by call to [applySpecifier](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L5234), that applies `AlignDeclaration` when `if (!specifier.packalign.isDefault())`. Fix info ======== With this commit the alias will be created to TypeTag, instead of attribute produced by declared tag. Also, the code simplified, because there is no more need for special handling of enums in modified piece of code. https://github.com/dlang/dmd/pull/15406
Comment #7 by dlang-bot — 2023-07-15T13:44:57Z
@ibuclaw created dlang/dmd pull request #15417 "merge stable" mentioning this issue: - ImportC: Issue 24022 - Error: attribute `__anonymous` is used as a type (#15365) * Implement test case for Issue 24022 * [FIX] Issue 24022 Bug investigation info ====================== Currently (before this fix) on attempt to use `enum` declared as `typedef enum {A=1} E;` as type of argument in D function, it (`enum`) will be resolved as `kind=attribute` on during semantic analysis for D ([typesem.d](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/typesem.d#L1504)), but it have to be resolved as type. The `enum` declared this way is handled by this [code](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1907), and `declareTag` returns the attribute instead of type, but later, there is [code that create alias for attribute](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1918C29-L1918C79), though, it seems, that alias have to be created to type, instead of attribute. Investigating [declareTag](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1715) function shows that the returned value changed by call to [applySpecifier](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L5234), that applies `AlignDeclaration` when `if (!specifier.packalign.isDefault())`. Fix info ======== With this commit the alias will be created to TypeTag, instead of attribute produced by declared tag. Also, the code simplified, because there is no more need for special handling of enums in modified piece of code. https://github.com/dlang/dmd/pull/15417
Comment #8 by dlang-bot — 2023-07-15T15:30:06Z
dlang/dmd pull request #15417 "merge stable" was merged into master: - 11d8f17c0f0917ed53154577a8cc7b2d5196e909 by Dmytro Katyukha: ImportC: Issue 24022 - Error: attribute `__anonymous` is used as a type (#15365) * Implement test case for Issue 24022 * [FIX] Issue 24022 Bug investigation info ====================== Currently (before this fix) on attempt to use `enum` declared as `typedef enum {A=1} E;` as type of argument in D function, it (`enum`) will be resolved as `kind=attribute` on during semantic analysis for D ([typesem.d](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/typesem.d#L1504)), but it have to be resolved as type. The `enum` declared this way is handled by this [code](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1907), and `declareTag` returns the attribute instead of type, but later, there is [code that create alias for attribute](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1918C29-L1918C79), though, it seems, that alias have to be created to type, instead of attribute. Investigating [declareTag](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L1715) function shows that the returned value changed by call to [applySpecifier](https://github.com/dlang/dmd/blob/5e1e97078faff33afbb999bc986ead7bdb0b2653/compiler/src/dmd/cparse.d#L5234), that applies `AlignDeclaration` when `if (!specifier.packalign.isDefault())`. Fix info ======== With this commit the alias will be created to TypeTag, instead of attribute produced by declared tag. Also, the code simplified, because there is no more need for special handling of enums in modified piece of code. https://github.com/dlang/dmd/pull/15417