D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2454 - typeof(object) is incorrectly evaluated
Summary: typeof(object) is incorrectly evaluated
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 major
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2008-11-14 14:03 UTC by Koroskin Denis
Modified: 2015-06-09 01:20 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Koroskin Denis 2008-11-14 14:03:32 UTC
struct B
{
    float i;
    double j;
    int k;

    void test()
    {
        foreach (m; this.tupleof) {
            writefln(typeof(m).stringof);
        }
    }
}

void main()
{
    B b;
    b.test();
}
Comment 1 BCS 2008-11-14 17:00:42 UTC
If a struct has a member that is a struct, I would expect that this would work. As for non struct members, the logical thing to do would be to make that work anyway to avoid corner cases.

Or are you saying the result of the tupleof is not what it should be?
Comment 2 Koroskin Denis 2008-11-14 18:50:03 UTC
My bad, I forgot attaching the expected and actual result. Here they are:

Expected output:
float
double
int

Actual output:
float
float
float

The tuple is ok, but foreach body "instantiated" incorrectly.
Comment 3 Koroskin Denis 2008-11-14 19:42:51 UTC
I believe this is relevant:

import std.stdio;

void foo(T)(T t)
{
    assert(is(typeof(t) == T));
    writeln(T.stringof);
    writefln(typeof(t).stringof);
}

struct A
{
    int i;

    void test()
    {
        foreach (m; this.tupleof) {
            foo(m); // line 17
        }
    }
}

void main()
{
    foo(new A());
}

Expected output:
A*
A*

Actual output:
A*
int

Looks like result of typeof(t) is evaluated just once at compile time and then reused at every template instantiation. Commenting line 17 hides the issue.
Comment 4 BCS 2008-11-14 19:51:32 UTC
What version? 

version: 1026  // what codepad.org uses
float
double
int
Comment 5 Koroskin Denis 2008-11-14 19:54:20 UTC
> Version: 2.020
Comment 6 Don 2010-07-28 13:21:29 UTC
Fixed DMD2.023 (failed in 2.022).