Bug 1373 – typeof(func).stringof fails when func has parameters.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-07-25T04:39:00Z
Last change time
2014-02-16T15:22:35Z
Assigned to
nobody
Creator
clugdbug
Comments
Comment #0 by clugdbug — 2007-07-25T04:39:17Z
If func is a function (not a function pointer) with zero arguments, it works correctly (I think). But if it is a function with >0 arguments, then it complains that it cannot evaluate it.
Curiously, it generates the correct result, as evidenced by pragma(msg) below, before giving the error.
--------------
double func0() { return 0; }
int func2(int a, int b) { return 2; }
void main()
{
static assert(typeof(func0).stringof=="(double())()"); // OK
pragma(msg, typeof(func2).stringof);
// bug.d(8): Error: expected 2 arguments, not 0
// (int(int a, int b))()
static assert(typeof(func2).stringof=="(int(int a, int b))()");
}
Comment #1 by someanon — 2007-08-31T17:55:15Z
[email protected] wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=1373
>
> Summary: typeof(func).stringof fails when func has parameters.
> Product: D
> Version: 1.019
> Platform: PC
> OS/Version: Windows
> Status: NEW
> Severity: normal
> Priority: P2
> Component: DMD
> AssignedTo: [email protected]
> ReportedBy: [email protected]
>
>
> If func is a function (not a function pointer) with zero arguments, it works
> correctly (I think). But if it is a function with >0 arguments, then it
> complains that it cannot evaluate it.
>
> Curiously, it generates the correct result, as evidenced by pragma(msg) below,
> before giving the error.
>
> --------------
> double func0() { return 0; }
> int func2(int a, int b) { return 2; }
>
> void main()
> {
> static assert(typeof(func0).stringof=="(double())()"); // OK
> pragma(msg, typeof(func2).stringof);
> // bug.d(8): Error: expected 2 arguments, not 0
> // (int(int a, int b))()
> static assert(typeof(func2).stringof=="(int(int a, int b))()");
> }
>
>
I think a related issue is
void func(int) {}
const text = func.stringof;
which doesn't compile under 2.003, error message:
"func_stringof.func(int) does not match parameter types ()".
However, it works if the int parameter is removed.
Comment #2 by smjg — 2007-10-21T11:08:50Z
Please don't blindly quote the entire message when replying. It's bad enough on any newsgroup, and when it clutters up Bugzilla, it's even worse.
This issue is puzzling me. Surely typeof(func0) should be double, since as an expression, func0 is equivalent to func0(). And typeof(func2) probably shouldn't compile.
Comment #3 by clugdbug — 2007-10-23T10:49:16Z
(In reply to comment #2)
> Please don't blindly quote the entire message when replying. It's bad enough
> on any newsgroup, and when it clutters up Bugzilla, it's even worse.
>
> This issue is puzzling me. Surely typeof(func0) should be double, since as an
> expression, func0 is equivalent to func0(). And typeof(func2) probably
> shouldn't compile.
Function types _are_ wierd. And not well documented.
What is func2 ? It's a symbol of "function type". The only thing you do with it it takes its address. Since in the spec, it's allowable in & UnaryExpression, it must be an expression, but I can't see it listed there.
Definitely related to issue #1341.
Comment #4 by htvennik — 2010-04-26T06:20:08Z
This issue bugged me with DMD 2.043 on Mac.
I noticed something peculiar which is demonstrated by this test case:
-----
void foo(int i) { }
enum string FOO_TYPE = typeof(foo).stringof;
pragma(msg, "Type of foo is: " ~ FOO_TYPE);
-----
Compiling that will print:
-----
stringof_error.d(2): Error: expected 1 function arguments, not 0
Type of foo is: (void(int i))()
-----
So actually, the .stringof is correctly evaluated, but still some unnecessary check for function arguments makes the compiler generate an error.