The following code doesn't crash. However, it does print an unexpected value.
It should print 90, but instead it prints 4202678.
The bug can be worked around by referencing 'obj' directly.
struct SS{ int a, b, c; }
int delegate() addss(SS obj){
with(obj) return {
return a+b+c;
};
}
void main(){
SS obj = {15, 30, 45};
auto l = addss(obj);
writeln(l()); // Prints unexpected value
}
Comment #1 by salihdb — 2012-08-07T20:56:08Z
This is not a bug it's a feature,
there's solution:
void main() {
struct SS {
int a, b, c;
}
int delegate () addss(SS obj){
return {
with(obj) return a+b+c;
};
}
addss(SS(7, 10, 60))().writeln("(total numbers 77)");
}
Comment #2 by timon.gehr — 2012-08-08T02:05:30Z
(In reply to comment #1)
> This is not a bug it's a feature,
This can lead to memory corruption in @safe code, therefore it is a bug.
> there's solution:
>
This is a workaround.
Comment #3 by clugdbug — 2012-08-15T06:36:20Z
(In reply to comment #1)
> This is not a bug it's a feature,
No, it's a wrong-code bug. This one has been on my to-do list for years now.