Bug 7725 – Implicit function pointer cast

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-03-18T05:56:00Z
Last change time
2013-11-09T08:41:58Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-03-18T05:56:43Z
void foo1(void function(int) f) {} void foo2(void function(in int) f) {} void bar1(int) {} void bar2(in int) {} void main() { foo1(&bar1); // OK foo2(&bar2); // OK foo1(&bar2); // Err foo2(&bar1); // Err } DMD 2.059head gives: test.d(8): Error: function test.foo1 (void function(int) f) is not callable using argument types (void function(const(int))) test.d(8): Error: cannot implicitly convert expression (& bar2) of type void function(const(int)) to void function(int) test.d(9): Error: function test.foo2 (void function(const(int)) f) is not callable using argument types (void function(int)) test.d(9): Error: cannot implicitly convert expression (& bar1) of type void function(int) to void function(const(int)) I think void function(in int) is a subset of void function(int). So I think only one of the calls should to give an error, while the other seems better as safe implicit cast.
Comment #1 by andrej.mitrovich — 2013-10-10T06:51:00Z
*** Issue 11207 has been marked as a duplicate of this issue. ***
Comment #2 by andrej.mitrovich — 2013-10-10T06:51:09Z
Another test-case copied from http://d.puremagic.com/issues/show_bug.cgi?id=11207#c0: (In reply to comment #0) > class A {} > class B : A {} > B foo(const A) { return null; } > void main() { > A function(const A) f1 = &foo; // compiles > B function(A) f2 = &foo; // does not compile > B function(immutable A) f3 = &foo; // does not compile > B function(B) f4 = &foo; // does not compile > } > > test.d(6): Error: cannot implicitly convert expression (& foo) of type B > function(const(A) _param_0) to B function(A) > test.d(7): Error: cannot implicitly convert expression (& foo) of type B > function(const(A) _param_0) to B function(immutable(A)) > test.d(8): Error: cannot implicitly convert expression (& foo) of type B > function(const(A) _param_0) to B function(B) > > > These implicit conversions should be possible. Explicit casting is too > confusing and dangerous. If you make a mistake, you will end up with serious > runtime errors.
Comment #3 by yebblies — 2013-11-09T07:05:52Z
Oh look, this again. *** This issue has been marked as a duplicate of issue 3075 ***
Comment #4 by andrej.mitrovich — 2013-11-09T08:41:58Z
(In reply to comment #3) > Oh look, this again. > > *** This issue has been marked as a duplicate of issue 3075 *** We could however implement this as a safe feature for to!(): ----- import std.conv; void call(void function(int) f) {} void foo(in int) {} void main() { call(to!(void function(int))(&foo)); } -----