www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13364] New: Template instance isInstanceOf itself

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

          Issue ID: 13364
           Summary: Template instance isInstanceOf itself
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: acehreli yahoo.com

The following code passes both checks with dmd v2.067-devel-c6cf84f. I would
expect the second one to fail:

import std.traits;

struct Foo(T)
{}

void main()
{
    static assert(isInstanceOf!(Foo, Foo!int));        // OK
    static assert(isInstanceOf!(Foo!int, Foo!int));    // ?
}

After debugging this issue I think that this is actually a dmd bug because
variadic template argument deduction seems to be making an error. Here is the
implementation of isInstanceOf:

enum bool isInstanceOf(alias S, T) = is(T == S!Args, Args...);

When both S and T are Foo!int, then (T == S!Args, Args...) should fail.
(Actually, it could succeed only if Args... were resolved to be empty, which is
not the case.)

The following code demonstrates that Args is non-empty when the S and T
template arguments are both Foo!int.

struct Foo(T)
{}

bool dumpInformation(alias S, T, Args)()
{
    import std.string;

    pragma(msg, S);       // prints: Foo!int
    pragma(msg, T);       // prints: Foo!int
    pragma(msg, Args);    // prints: int

    return true;
}

// Copy of std.traits.isInstanceOf with the addition of dumpInformation()
enum bool myIsInstanceOf(alias S, T) =
    is(T == S!Args, Args...) &&
    dumpInformation!(S, T, Args)();

void main()
{
    static assert(myIsInstanceOf!(Foo!int, Foo!int));
}

Ali

--
Aug 22 2014