Thread overview
[Issue 19769] CTFE format(): str[index] is used before initialized
Dec 05, 2019
berni44
Dec 05, 2019
berni44
Dec 17, 2022
Iain Buclaw
December 05, 2019
https://issues.dlang.org/show_bug.cgi?id=19769

berni44 <bugzilla@d-ecke.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@d-ecke.de

--- Comment #1 from berni44 <bugzilla@d-ecke.de> ---
The problem here is, that asCapitalized returns a struct (ToCapitalizerImpl, which is a range), but the overload of formatValueImpl for structs (instead of ranges or the more expected strings) is choosen for printing. This wrong formatValueImpl tries to print all members of the struct, including the uninitialized ones.

--
December 05, 2019
https://issues.dlang.org/show_bug.cgi?id=19769

--- Comment #2 from berni44 <bugzilla@d-ecke.de> ---
My former comment is a little bit misleading. It's the correct formatValueImpl, which is called, but inside this function the static if, checking for an InputRange, is not executed. This is, because after the cast to const, we have a `const(ToCapitalizerImpl)` which is not anymore recoginzed as an InputRange (i.e. isInputRange fails).

The reason is, that empty, front and popFront would need to be const for a const object, but they cannot be const, because they change the object.

IMHO, this is an invalid bug. The result of "test".asCapitalized is an
InputRange and not a string as probably was intended. So, the correct call (if
that cast(const) is really needed) would be:

import std.uni;
import std.format;
import std.array;
static immutable x = format("%s", cast(const)"test".asCapitalized.array);

Having said this: The errormessage is confusing. Maybe, the struct part of formatValueImpl should check if a member is initialized, before trying to access it... Therefore I leave this open.

New test example:

```
import std.format;

struct Foo
{
    int a = void;
}

static x = format("%s", Foo());
```

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=19769

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--