www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20664] New: Compiler generates string with bad pointer

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

          Issue ID: 20664
           Summary: Compiler generates string with bad pointer
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andy.pj.hanson gmail.com

```
import std.stdio : writeln;

void main() {
        immutable Wrapper* a = getFieldName!S;
        immutable Wrapper* b = getFieldName!(immutable S);
        writeln(a, " ", b);
        writeln(a.value.ptr, " ", b.value.ptr);
        writeln(a.value.length, " ", b.value.length);
        writeln(a.value, " ", b.value);
}

struct S {
        int a;
}

template getFieldName(T) {
        immutable Wrapper* getFieldName = getFieldNameInner!(T.tupleof);
}

template getFieldNameInner(fields...) {
        immutable Wrapper w = Wrapper(fields[0].stringof);
        immutable Wrapper* getFieldNameInner = &w;
}

struct Wrapper {
        immutable string value;
}
```

This code should print two arbitrary pointer values, then 1, then "a".
If the variable `b` is removed, it does.
When `b` is present, `a.value.ptr` is different and identical to `b.value.ptr`,
and it segfaults when trying to print `a.value`.

The issue doesn't occur if `b` uses just `getFieldName!S` instead of
`getFieldName!(immutable S)` or it uses some other struct instead.
Somehow, passing both `S` and `immutable S` to the template causes `a` and `b`
to share the same `Wrapper*`,
but the string it contains has an invalid pointer.

--
Mar 10