Bug 4891 – Assignment from non-pure function to pure function pointer compiles when it shouldn't
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-09-18T16:07:00Z
Last change time
2011-06-06T19:33:40Z
Assigned to
nobody
Creator
issues.dlang
Comments
Comment #0 by issues.dlang — 2010-09-18T16:07:33Z
Take this program
struct S
{
public:
this(int function(int) pure func)
{
_func = func;
}
@property int function(int) pure func()
{
return func;
}
private:
int function(int) pure _func;
}
int add1(int num)
{
return num + 1;
}
void main()
{
auto s = S(&add1);
}
The construction of s should not compile because add1() is not a pure function. Granted, it could be pure if I declared it as such, but I didn't. And the compiler isn't figuring out that add1() could be pure and letting it through because of that because if I add a writeln() call to add1(), it still compiles.
Comment #1 by issues.dlang — 2010-09-18T17:15:25Z
Oh, there's an error in my example - func return func instead of _func, resulting infinite recursion and therefore a segfault. I could have removed that function for the bug report anyway.
Comment #2 by bearophile_hugs — 2010-09-19T18:04:25Z
See bug 3833
Comment #3 by yebblies — 2011-06-06T19:33:40Z
This is a dupe of 3797, the purity of a function pointer (among other things) is not checked when performing implicit conversions.
With dmd pull 88 you get the following error from dmd:
testx.d(26): Error: constructor testx.S.this (int function(int) pure func) is no
t callable using argument types (int function(int num))
testx.d(26): Error: cannot implicitly convert expression (& add1) of type int fu
nction(int num) to int function(int) pure
*** This issue has been marked as a duplicate of issue 3797 ***