www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21768] New: typeid(Expression) doesn't properly resolve

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

          Issue ID: 21768
           Summary: typeid(Expression) doesn't properly resolve opIndex
                    overload
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: trivial
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: blackbirdcry806 gmail.com

When using the typeid(Expression) statement where "Expression" is a membership
access of a struct that is returned by a opIndex oveload that takes only one
parameter, the compiler resolve it as an array access instead of a opIndex
call.
Adding parenthesis around the expression solve the problem but looks
inconsistent.

godbolt link: https://godbolt.org/z/djjxqvKr8

sample code (same as above):

struct Vec2
{
    int x, y;
}

class Entity
{

}

struct Tile
{
    Entity e;
}

class World
{
    auto opIndex(Vec2 i)
    {
        return Tile();
    }

    auto opIndex(int i, int j)
    {
        return Tile();
    }

    auto opIndex(int i)
    {
        return Tile();
    }
}

void main()
{
    auto w = new World();

    // auto id = typeid(w[Vec2(0, 0)].e); // compile error here
    // auto id4 = typeid(w[0].e); // error as well

    // works fine
    auto id2 = typeid((w[Vec2(0, 0)].e));
    auto id3 = typeid(w[0, 0].e);
}

--
Mar 25