Thread overview
[Issue 9563] New: (2.062) typeof(T[0]) no longer works with array T's
Feb 22, 2013
Marco Leise
February 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9563

           Summary: (2.062) typeof(T[0]) no longer works with array T's
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: siegelords_abode@yahoo.com


--- Comment #0 from siegelords_abode@yahoo.com 2013-02-21 19:08:04 PST ---
Both invocations of ElemTypeOf used to work fine with DMD 2.061 but with DMD 2.062 only the class one works. Either none of them should work, or preferably both should work as they had since D1:

template ElemTypeOf( T )
{
    alias typeof(T[0]) ElemTypeOf;
}

class A
{
    int opIndex(size_t i)
    {
        return 0;
    }
}

void main()
{
    ElemTypeOf!(A) a;
    ElemTypeOf!(int[]) b;

    static assert(is(typeof(a) == int));
    static assert(is(typeof(b) == int));
}

With dmd 2.062 the second invocation now gives the error:

test.d(3): Error: argument int[][0LU] to typeof is not an expression
test.d(3): Error: argument int[][0LU] to typeof is not an expression
test.d(17): Error: template instance test.ElemTypeOf!(int[]) error
instantiating
test.d(20): Error: static assert  (is(_error_ == int)) is false

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9563


Marco Leise <Marco.Leise@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Marco.Leise@gmx.de


--- Comment #1 from Marco Leise <Marco.Leise@gmx.de> 2013-02-21 21:03:11 PST ---
You made me smile. I recently needed ElementType! in my code for arrays. I had almost written a simple template, but remembered that Phobos had it already. And I thought "I better use the Phobos one, that's sure to still work in the future. Not that it's likely that typeof(T[0]) breaks anytime soon... but still." Sometimes you have these gut feelings you can't even explain to co-workers.

Sorry for the noise.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9563


monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra@gmail.com


--- Comment #2 from monarchdodra@gmail.com 2013-02-21 22:39:48 PST ---
(In reply to comment #0)
> Both invocations of ElemTypeOf used to work fine with DMD 2.061 but with DMD 2.062 only the class one works. Either none of them should work, or preferably both should work as they had since D1:

What do you mean "only the class one works"? I'm getting a failure for both asserts.

Furthermore, I expect a failure. Your ElemTypeOf simply declares a static array of size 0 of your type, it is not actually calling opIndex. Then combining it with calling "typeof" on something that is already a type, makes this dobly wrong code.

This worked in 2.060, but was fixed starting in 2.061.

Try something like this:

template ElemTypeOf( T )
{
    static private T t = T.init;
    alias typeof(t[0]) ElemTypeOf;
}

Or better yet, std.range's ElementType or ElementEncodingType.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9563



--- Comment #3 from siegelords_abode@yahoo.com 2013-02-22 07:35:52 PST ---
> What do you mean "only the class one works"? I'm getting a failure for both asserts.

I am not with DMD 2.062. This code compiles without any error and prints "int":

template ElemTypeOf( T )
{
    alias typeof(T[0]) ElemTypeOf;
}

class A
{
    int opIndex(size_t i)
    {
        return 0;
    }
}

void main()
{
    ElemTypeOf!(A) a;

    pragma(msg, typeof(a).stringof);
    static assert(is(typeof(a) == int));
}

> This worked in 2.060, but was fixed starting in 2.061.
> 
> Try something like this:
> 
> template ElemTypeOf( T )
> {
>     static private T t = T.init;
>     alias typeof(t[0]) ElemTypeOf;
> }

I guess that works (with some simplification of course), although the existence of a workaround does not negate the existence of the regression.

> Or better yet, std.range's ElementType or ElementEncodingType.

Not using Phobos.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9563



--- Comment #4 from monarchdodra@gmail.com 2013-02-22 07:54:25 PST ---
(In reply to comment #3)
> > What do you mean "only the class one works"? I'm getting a failure for both asserts.
> 
> I am not with DMD 2.062. This code compiles without any error and prints "int":
> 
> > This worked in 2.060, but was fixed starting in 2.061.

My bad, I must have botched the test. That does indeed work in 2.061, so was broken in 2.062.

> > Try something like this:
> > 
> > template ElemTypeOf( T )
> > {
> >     static private T t = T.init;
> >     alias typeof(t[0]) ElemTypeOf;
> > }
> 
> I guess that works (with some simplification of course), although the existence of a workaround does not negate the existence of the regression.

Well, as I said, I think this is not a matter of regression and workaround, but rather wrong code finally getting rejected, and new and correct code being deployed to replace it.

In any case, I'll let one of the more compiler knowledgeable guys (Kenji? Walter) give you definitive answer.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9563


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy@yahoo.com
         Resolution|                            |INVALID


--- Comment #5 from Steven Schveighoffer <schveiguy@yahoo.com> 2013-02-22 08:21:07 PST ---
This is indeed a bug that was fixed.

T[0] is applying index to the type, not an instance of the type.  I wouldn't
expect typeof(T[0]) to work at all, it's like saying typeof(int) (which doesn't
compile), typeof converts an expression into its type.

You should be able to do typeof(T.init[0]).

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