Bug 108877 - Explicit immutable struct import internal compiler error
Summary: Explicit immutable struct import internal compiler error
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: d (show other bugs)
Version: 12.2.0
: P3 normal
Target Milestone: ---
Assignee: Iain Buclaw
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-02-21 20:04 UTC by Eugen Wissner
Modified: 2023-03-03 02:46 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eugen Wissner 2023-02-21 20:04:22 UTC
We have 2 files. util/Version.d:

```
module util.Version;

immutable struct Ver
{
}
```
and the main file:

```
import util.Version : Ver;
```

Compiling with „gdc-12 -c util/Version.d version_test.d“ results in the error:

> version_test.d:1:8: internal compiler error: in make_import, at d/imports.cc:48
>     1 | import util.Version : Ver;
>       |        ^
> 0x1bc1e87 internal_error(char const*, ...)
> 	???:0
> 0x7d27f3 fancy_abort(char const*, int, char const*)
> 	???:0
> 0x9d4214 ImportVisitor::visit(AggregateDeclaration*)
> 	???:0
> 0x9d3cfe build_import_decl(Dsymbol*)
> 	???:0
> 0x9ca0ff DeclVisitor::visit(Import*)
> 	???:0
> 0x9c70a6 build_decl_tree(Dsymbol*)
> 	???:0
> 0x9d6fc0 build_module_tree(Module*)
> 	???:0
> 0x9c9f4b DeclVisitor::visit(Module*)
> 	???:0
> 0x9c70a6 build_decl_tree(Dsymbol*)
> 	???:0
> Please submit a full bug report, with preprocessed source (by using -freport-bug).
> Please include the complete backtrace with any bug report.
> See <https://gcc.gnu.org/bugs/> for instructions.

This is reproducable with gdc-12.2.0 and gdc 11.2.0. GDC 12.2.0 info:

> gdc-12 -v
> Using built-in specs.
> COLLECT_GCC=gdc-12
> COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-slackware-linux/12/lto-wrapper
> Target: x86_64-slackware-linux
> Configured with: ../gcc-12.2.0/configure --prefix=/usr --libdir=/usr/lib64 --mandir=/usr/man --infodir=/usr/info --enable-shared --disable-bootstrap --enable-languages=c,c++,d --enable-threads=posix --enable-checking=release --with-system-zlib --disable-libquadmath-support --with-default-libstdcxx-abi=new --disable-libstdcxx-pch --disable-libunwind-exceptions --enable-__cxa_atexit --disable-libssp --enable-gnu-unique-object --enable-plugin --enable-lto --disable-install-libiberty --disable-werror --with-gcc-major-version-only --with-isl --program-suffix=-12 --with-arch-directory=amd64 --disable-gtktest --enable-clocale=gnu --disable-multilib --target=x86_64-slackware-linux --build=x86_64-slackware-linux --host=x86_64-slackware-linux
> Thread model: posix
> Supported LTO compression algorithms: zlib zstd
> gcc version 12.2.0 (GCC)
Comment 1 Iain Buclaw 2023-02-26 00:17:55 UTC
Immutable is the keyword here. Stub debug symbols are only attached to the main variant of a type.  I think this would also be reproducible with `immutable class` and `immutable enum` as well.

Have fix really to commit and backport once I test it.

---

diff --git a/gcc/d/imports.cc b/gcc/d/imports.cc
index 3b46d1b7560..2efef4ed54f 100644
--- a/gcc/d/imports.cc
+++ b/gcc/d/imports.cc
@@ -106,12 +106,16 @@ public:
     tree type = build_ctype (d->type);
     /* Not all kinds of D enums create a TYPE_DECL.  */
     if (TREE_CODE (type) == ENUMERAL_TYPE)
-      this->result_ = this->make_import (TYPE_STUB_DECL (type));
+      {
+	type = TYPE_MAIN_VARIANT (type);
+	this->result_ = this->make_import (TYPE_STUB_DECL (type));
+      }
   }
 
   void visit (AggregateDeclaration *d) final override
   {
     tree type = build_ctype (d->type);
+    type = TYPE_MAIN_VARIANT (type);
     this->result_ = this->make_import (TYPE_STUB_DECL (type));
   }
 
@@ -119,6 +123,7 @@ public:
   {
     /* Want the RECORD_TYPE, not POINTER_TYPE.  */
     tree type = TREE_TYPE (build_ctype (d->type));
+    type = TYPE_MAIN_VARIANT (type);
     this->result_ = this->make_import (TYPE_STUB_DECL (type));
   }
Comment 2 Eugen Wissner 2023-02-27 12:39:14 UTC
Not sure about class and enum, but „const struct“ results in a similar error.
Comment 3 GCC Commits 2023-03-03 00:26:24 UTC
The master branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:ce1cea3e22f58bbddde017f8a92e59bae8892339

commit r13-6432-gce1cea3e22f58bbddde017f8a92e59bae8892339
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Mon Feb 27 20:46:18 2023 +0100

    d: Fix ICE on explicit immutable struct import [PR108877]
    
    Const and immutable types are built as variants of the type they are
    derived from, and TYPE_STUB_DECL is not set for these variants.
    
            PR d/108877
    
    gcc/d/ChangeLog:
    
            * imports.cc (ImportVisitor::visit (EnumDeclaration *)): Call
            make_import on TYPE_MAIN_VARIANT.
            (ImportVisitor::visit (AggregateDeclaration *)): Likewise.
            (ImportVisitor::visit (ClassDeclaration *)): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * gdc.dg/imports/pr108877a.d: New test.
            * gdc.dg/pr108877.d: New test.
Comment 4 GCC Commits 2023-03-03 02:28:19 UTC
The releases/gcc-12 branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:2583365912c8700abe1f4a23ed611acb80fac09d

commit r12-9212-g2583365912c8700abe1f4a23ed611acb80fac09d
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Mon Feb 27 20:46:18 2023 +0100

    d: Fix ICE on explicit immutable struct import [PR108877]
    
    Const and immutable types are built as variants of the type they are
    derived from, and TYPE_STUB_DECL is not set for these variants.
    
            PR d/108877
    
    gcc/d/ChangeLog:
    
            * imports.cc (ImportVisitor::visit (EnumDeclaration *)): Call
            make_import on TYPE_MAIN_VARIANT.
            (ImportVisitor::visit (AggregateDeclaration *)): Likewise.
            (ImportVisitor::visit (ClassDeclaration *)): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * gdc.dg/imports/pr108877a.d: New test.
            * gdc.dg/pr108877.d: New test.
    
    (cherry picked from commit ce1cea3e22f58bbddde017f8a92e59bae8892339)
Comment 5 GCC Commits 2023-03-03 02:33:23 UTC
The releases/gcc-11 branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:fe6cd1ba23ecbce9c0206c08db182cb5164e3b7d

commit r11-10554-gfe6cd1ba23ecbce9c0206c08db182cb5164e3b7d
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Mon Feb 27 20:46:18 2023 +0100

    d: Fix ICE on explicit immutable struct import [PR108877]
    
    Const and immutable types are built as variants of the type they are
    derived from, and TYPE_STUB_DECL is not set for these variants.
    
            PR d/108877
    
    gcc/d/ChangeLog:
    
            * imports.cc (ImportVisitor::visit (EnumDeclaration *)): Call
            make_import on TYPE_MAIN_VARIANT.
            (ImportVisitor::visit (AggregateDeclaration *)): Likewise.
            (ImportVisitor::visit (ClassDeclaration *)): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * gdc.dg/imports/pr108877a.d: New test.
            * gdc.dg/pr108877.d: New test.
    
    (cherry picked from commit ce1cea3e22f58bbddde017f8a92e59bae8892339)
Comment 6 GCC Commits 2023-03-03 02:43:35 UTC
The releases/gcc-10 branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>:

https://gcc.gnu.org/g:c90e68bffa37edd655dd2f5d14bb7b213c9e2431

commit r10-11235-gc90e68bffa37edd655dd2f5d14bb7b213c9e2431
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Mon Feb 27 20:46:18 2023 +0100

    d: Fix ICE on explicit immutable struct import [PR108877]
    
    Const and immutable types are built as variants of the type they are
    derived from, and TYPE_STUB_DECL is not set for these variants.
    
            PR d/108877
    
    gcc/d/ChangeLog:
    
            * imports.cc (ImportVisitor::visit (EnumDeclaration *)): Call
            make_import on TYPE_MAIN_VARIANT.
            (ImportVisitor::visit (AggregateDeclaration *)): Likewise.
            (ImportVisitor::visit (ClassDeclaration *)): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * gdc.dg/imports/pr108877a.d: New test.
            * gdc.dg/pr108877.d: New test.
    
    (cherry picked from commit ce1cea3e22f58bbddde017f8a92e59bae8892339)
Comment 7 Iain Buclaw 2023-03-03 02:46:18 UTC
Fix committed and backported.