Bug 15575 – partial initialization of a union member causes wrong diagnostic during CTFE

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-01-18T00:28:56Z
Last change time
2024-12-13T18:46:36Z
Keywords
CTFE, rejects-valid
Assigned to
No Owner
Creator
Cédric Picard
Moved to GitHub: dmd#19091 →

Comments

Comment #0 by cpicard — 2016-01-18T00:28:56Z
DMD v2.069, no flag, crashes when dealing with the following code: ----------------------------------------------------------------------------- struct Color { union { ubyte[4] components; struct { ubyte r; ubyte g; ubyte b; ubyte a; } } this(ubyte red, ubyte green, ubyte blue, ubyte alpha=255) { if(__ctfe) this.components[0] = cast(ubyte) red; else this.r = cast(ubyte) red; this.g = cast(ubyte) green; this.b = cast(ubyte) blue; this.a = cast(ubyte) alpha; } } static immutable colorA = Color(0xcc, 0x88, 0x00); void main(string[] args) { pragma(msg, colorA.components); // [cast(ubyte)204u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u] pragma(msg, colorA); // Color([cast(ubyte)204u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u], , // cast(ubyte)136u, cast(ubyte)0u, cast(ubyte)255u, ) } ----------------------------------------------------------------------------- The message is: t.d(32): Error: Internal Compiler Error: null field components test.d(32): while evaluating pragma(msg, colorA.components) Color(, , cast(ubyte)136u, cast(ubyte)0u, cast(ubyte)255u, ) LDC doesn't produce an error message but produces garbage instead.
Comment #1 by cpicard — 2016-01-18T00:31:22Z
I should mention that the code is inspired by Adam Ruppe's arsd/color.d and that a comment next to the "if (__ctfe)" part mentions that it is a hack arround bug 10937. That may be related.
Comment #2 by razvan.nitu1305 — 2022-10-28T10:32:55Z
The code does not crash but produces the following errors now: test.d(27): Error: uninitialized variable `components` cannot be returned from CTFE test.d(27): while evaluating `pragma(msg, colorA.components)` test.d(30): while evaluating `pragma(msg, colorA)`
Comment #3 by b2.temp — 2023-10-05T13:23:17Z
updated test case: ``` struct B15575 { union { ubyte[2] a; struct { ubyte b; ubyte c; } } this(ubyte b) { if (b) this.a[0] = b; else this.b = b; this.c = 0; } } void main() { auto a = B15575(7).a; // run-time OK pragma(msg, B15575(7).a); // CTFE NG } ```
Comment #4 by robert.schadek — 2024-12-13T18:46:36Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19091 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB