Applying a UDA to a File-type variable makes DMD crash
H. S. Teoh
hsteoh at qfbox.info
Sun Dec 21 00:38:29 UTC 2025
On Sat, Dec 20, 2025 at 01:58:50PM +0100, Marcos Cruz via Digitalmars-d wrote:
[...]
> I have been learning and using D for several months, and following the
> forum groups by email. In fact D has become my language of choice.
Welcome to the D community! :-)
> Some weeks ago I found a strange issue. The following code makes DMD
> (2.111.0) crash with message "terminated by signal SIGSEGV (Address
> boundary error)", while LDC (1.41.0) and GDC (14.2.0) compile it (I
> use Debian 13.2, x86_64):
Anything that causes the compiler to crash is a compiler bug. Please
report a bug:
https://github.com/dlang/dmd/issues
>
> ```D
> enum var;
> import std.stdio : File;
> @var File file = File("tmp", "w");
> ```
[...]
It's probably a bad idea to do this in module global scope, because
std.stdio.File is referenced counted with a dtor that closes the file
upon exiting the scope. Since module global scope never exits, this
probably interacts badly with built-in assumptions about how dtors are
used in structs. Likely this is what causes the compiler to hit an
internal compiler error.
If you need a globally-accessible File, you should probably initialize
it in main(), or, barring that, in a module static ctor, something like
this:
```
import std.stdio L File;
File file;
/*shared*/ static this() {
file = File("tmp", "w");
}
```
T
--
Error: Keyboard not attached. Press F1 to continue. -- Yoon Ha Lee, CONLANG
More information about the Digitalmars-d
mailing list