[Issue 19769] CTFE format(): str[index] is used before initialized

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Dec 5 16:20:06 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=19769

--- Comment #2 from berni44 <bugzilla at 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());
```

--


More information about the Digitalmars-d-bugs mailing list