[Issue 9186] New: Manifest constant can violate const correctness restrictions when empty

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Dec 19 06:49:47 PST 2012


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

           Summary: Manifest constant can violate const correctness
                    restrictions when empty
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: monarchdodra at gmail.com


--- Comment #0 from monarchdodra at gmail.com 2012-12-19 06:49:46 PST ---
The basic use case is having:
foo(const(char)[]);
and trying to pass a
immutable(char)[] to it. Which is illegal.

The problem here is that if you define a manifest constant that is empty, and
try to pass it to foo, the code will compile:

//----
void main()
{
    void foo(const(char)[][]);
    alias Type = string[];
    static assert(is(typeof(Type.init) == Type));
    static if (is(typeof(foo(Type.init)))) //Enter here !!!
    {
        Type Rbla() @property;
        Type Lbla;
        enum Type Ebla1 = [[]];
        enum Type Ebla2 = ["hello"];
        foo(Type.init); //OK!
        foo(Rbla);
        foo(Lbla);
        foo(Ebla1); //OK!
        foo(Ebla2);
    }
}
//----
Error: function dmd2.main.foo (const(char)[][]) is not callable using argument
types (string[])
Error: cannot implicitly convert expression (Rbla()) of type string[] to
const(char)[][]
Error: function dmd2.main.foo (const(char)[][]) is not callable using argument
types (string[])
Error: cannot implicitly convert expression (Lbla) of type string[] to
const(char)[][]
//----

This may not seem like a "huge" problem in and out of itself. The *REAL*
problem lies in the static if:
"static if (is(typeof(foo(Type.init))))"
This code is explicitly written to find out if we can pass a variable of type
Type to foo. This is especially true for array types, which are the ones
vulnerable to this problem.

I'm marking as "Major", because phobos is vulnerable to the bug.

Found inside "put":
//----
else static if (is(typeof(r.put((E[]).init))))
{
    r((&e)[0..1]);
}
//----
src\phobos\std\range.d(590): Error: cannot implicitly convert expression (&
e[0u..1u]) of type string[] to const(char)[][]
//----

Current workaround is to use an lvalueof type function.

-- 
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