D - [BUG] narrowing formal parameter lists
- Manfred Nowak (30/30) Mar 21 2004 dmc compiles the following and the resulting executable runs okay:
- Walter (11/41) Mar 21 2004 The reason this "works" in C but fails in D is because the default funct...
- Manfred Nowak (6/7) Mar 23 2004 Thx. I was on the wrong foot because I got quite early in my investigati...
dmc compiles the following and the resulting executable runs okay:
unsigned int (*fp)(unsigned int);
unsigned int f(int num, int num2){ return num;}
void main(void){
fp=(unsigned int(*)(unsigned int))f;
printf("%u\n",fp(2));
}
dmd compiles but resulting executable outputs garbage and then(!) throws
AV on:
uint function(uint) fp;
uint f(uint num , uint num2){ return num;}
void main(){
fp=cast(uint function(uint)) &f;
printf("%u\n", fp(2));
}
output:
4202694
Error: Access Violation
This is a bug, because either one is true:
- dmd should have denied the compilation, with an error message, because
it cannot cast it, or
- dmd can cast it, but did not generate the correct code
This raises the question, whether such casts should be allowed at all. and
if they are allowed, what are the precise semantics of such casts?
I.e. I believe that in dmc in the example above the actual parameter is
assigned to the first formal parameter, whereas in dmd it is assigned to
the last formal parameter.
What will happen, when the formal parameters of the cast and the casted
function are not identical?
So long!
Mar 21 2004
The reason this "works" in C but fails in D is because the default function
calling conventions are different, and it "working" in C is an artifact of
C's default calling convention. The reason the cast does not issue an error
message is because a cast *assumes* the user knows better than the compiler
what he is doing.
In general, you're going to have to have precise and detailed knowledge of
the various function calling conventions to make such code work, and the
result will still have non-portable implementation dependencies on it. But
it isn't a bug in the compiler.
"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:c3lq72$11gk$2 digitaldaemon.com...
dmc compiles the following and the resulting executable runs okay:
unsigned int (*fp)(unsigned int);
unsigned int f(int num, int num2){ return num;}
void main(void){
fp=(unsigned int(*)(unsigned int))f;
printf("%u\n",fp(2));
}
dmd compiles but resulting executable outputs garbage and then(!) throws
AV on:
uint function(uint) fp;
uint f(uint num , uint num2){ return num;}
void main(){
fp=cast(uint function(uint)) &f;
printf("%u\n", fp(2));
}
output:
4202694
Error: Access Violation
This is a bug, because either one is true:
- dmd should have denied the compilation, with an error message, because
it cannot cast it, or
- dmd can cast it, but did not generate the correct code
This raises the question, whether such casts should be allowed at all. and
if they are allowed, what are the precise semantics of such casts?
I.e. I believe that in dmc in the example above the actual parameter is
assigned to the first formal parameter, whereas in dmd it is assigned to
the last formal parameter.
What will happen, when the formal parameters of the cast and the casted
function are not identical?
So long!
Mar 21 2004
Walter wrote: [...]But it isn't a bug in the compiler.Thx. I was on the wrong foot because I got quite early in my investigation of D a `cannot cast' error message for bit to byte, which is still in existence since 24.01.04. So long!
Mar 23 2004








Manfred Nowak <svv1999 hotmail.com>