[Issue 8229] string literals are not zero-terminated during CTFE

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jun 13 01:42:33 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8229



--- Comment #3 from Don <clugdbug at yahoo.com.au> 2012-06-13 01:44:42 PDT ---
(In reply to comment #2)
> (In reply to comment #1)
> > This behaviour is intentional. Pointer operations are strictly checked in CTFE.
> > It's the same as doing 
> > 
> > int n = 0;
> > char c = ""[n];
> > 
> > which generates an array bounds error at runtime.
> > 
> 
> I think that would be stretching it too far. It is more like:
> 
> auto s = ['\0'];
> auto q = s[0..0];
> char c = *q.ptr;

That's an interesting interpretation. It can't be true for D1, where string
literals are fixed length arrays, but it could work for D2.

In D1 it's more like:
struct S
{
  static char[3] s = ['a', 'b', 'c'];
  static char terminator = '\0';
}
And every mention of it in the spec dates from D1.

> > Is the terminating null character still in the spec? A long time ago it was in
> > there, but now I can only find two references to it in the current spec (in
> > 'arrays' and in 'interfacing to C'), and they both relate to printf. 
> > 
> > The most detailed is in 'interface to C', which states:
> > "string literals, when they are not part of an initializer to a larger data
> > structure, have a '\0' character helpfully stored after the end of them."
> > 
> > which is pretty weird. These funky semantics would be difficult to implement in
> > CTFE,
> 
> I guess this is from D1 times, when string literals were static arrays, and
> doesn't apply anymore.

Could be. So the few parts of the spec that mention it are horribly
out-of-date.
Though it also applies to assigning to fixed length arrays.

immutable(char)[3] s = "abc";
// Does this have a trailing zero?

> > and I doubt they are desirable. Here's an example:
> > 
> > const(char)[] foo(char[] s) { return "abc" ~ s; }
> > 
> > immutable bar = foo("xyz"); // becomes a string literal when it leaves CTFE
> > 
> 
> Well, this is not specified afaics.

Hmm, maybe it isn't. The spec says almost nothing about the whole thing. What I
do know is that there is a lot of existing code that relies on this behaviour
(especially, "abc" ~ "def" having a trailing zero).
Pretty much the only thing the spec says is that you can use string literals
with printf.

Does TDPL mention it?

The spec definitely needs to be improved.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list