Bug 2454 – typeof(object) is incorrectly evaluated

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-11-14T14:03:00Z
Last change time
2015-06-09T01:20:25Z
Keywords
wrong-code
Assigned to
nobody
Creator
2korden

Comments

Comment #0 by 2korden — 2008-11-14T14:03:32Z
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 by shro8822 — 2008-11-14T17:00:42Z
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 by 2korden — 2008-11-14T18:50:03Z
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 by 2korden — 2008-11-14T19:42:51Z
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 by shro8822 — 2008-11-14T19:51:32Z
What version? version: 1026 // what codepad.org uses float double int
Comment #5 by 2korden — 2008-11-14T19:54:20Z
> Version: 2.020
Comment #6 by clugdbug — 2010-07-28T13:21:29Z
Fixed DMD2.023 (failed in 2.022).