1341 – typeof(X) accepted, even if X is a type

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1341 - typeof(X) accepted, even if X is a type
Summary: typeof(X) accepted, even if X is a type
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 normal
Assignee: Walter Bright
URL:
Keywords: accepts-invalid, spec
: 1777 (view as issue list)
Depends on:
Blocks: 1777
Show dependency tree / graph
Reported: 2007年07月14日 16:52 UTC by Don
Modified: 2014年02月16日 15:22 UTC (History)
2 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2007年07月14日 16:52:03 UTC
According to the spec (in declaration.html), the argument to typeof() must be an expression. This is not currently enforced by DMD. In fact, it can be any type, except for builtin types which fail with a misleading error message.
alias int foo;
typeof(foo) a; // ok
typeof(int) b; // FAILS: found ')' when expecting '.' following 'int'
typeof(typeof(foo)) c; // ok, even though the argument is obviously a type.
I think that the compiler's behaviour generally makes more sense than the spec (and I'm finding it to be useful behaviour). Enforcing the rule in the spec would mean the compiler would need to determine if 'foo' is a type name before issuing a syntax error --> violation of independence of syntax and semantic passes.
So I suggest that the spec be changed to allow typeof(Type) to be legal, and to adjust the compiler to accept built-in types.
eg, make typeof(int) synonymous with int.
Comment 1 Stewart Gordon 2007年10月21日 10:58:03 UTC
According to the spec, typeof(typeof(foo)) shouldn't compile. As you've probably noticed already, the syntax is
Typeof:
 typeof ( Expression )
and this is parseable only as a type and not as an expression.
I can see that there might be some generic programming use in defining typeof(X) == X if X is already a type. The only problem is that it isn't documented. But if what's in the brackets is already not parseable as an Expression, there's no benefit to writing it this way. As such, it's probably reasonable to disallow it as it would mean yet another ambiguity in the grammar.
Comment 2 Walter Bright 2008年07月02日 01:41:18 UTC
I agree with Stewart, neither should compile.
Comment 3 Don 2008年07月02日 02:10:45 UTC
So the resolution would be that:
typeof(X)
would generate an error late in the semantic pass if X turns out to be a type, rather than an expression?
Comment 4 Walter Bright 2008年07月02日 02:44:22 UTC
Yes.
Comment 5 Don 2008年07月03日 08:03:39 UTC
One thing worries me about this...
in D1.0, a common metaprogramming idiom is "is(typeof(xxx))"
For example, this compiles in D1.0:
------
static assert(!is(typeof(nonexistent)));
------
Lots of code (including BLADE, for example) will fail if that stops working. Please make sure that doesn't get broken in fixing this bug!
Comment 6 Stewart Gordon 2008年07月03日 09:03:40 UTC
How do you work that out?
Comment 7 Don 2008年07月03日 09:46:30 UTC
Changed the description of this bug in view of the discussion above.
The fear I mentioned above is probably irrational.
Comment 8 Don 2008年07月09日 07:19:41 UTC
Fixed DMD1.032
Comment 9 Don 2008年07月09日 07:20:34 UTC
*** Bug 1777 has been marked as a duplicate of this bug. ***
Comment 10 Walter Bright 2008年07月09日 22:39:55 UTC
Fixed dmd 1.032 and 2.016


AltStyle によって変換されたページ (->オリジナル) /