Compiling the following code using DMD 2.059 without -g results in a UTF exception (memory corruption?) after outputting ["a", "b",. Compiling with -g produces no exception but the output is incorrect (["a", "b", "0", "1", "2"] should be ["a", "b", "a0", "a1", "a2"]). Now, if you replace the iota line with with the commented out sequence line it works the other way. Without -g gives no exception (but correct output this time), with -g results in a UTF exception. It's a bit unsettling that adding debug info can change the behavior of the program.
import std.stdio, std.range, std.algorithm, std.string;
void main()
{
writeln(gen(["a", "b"]).take(5));
}
auto gen(string[] lst)
{
auto prefix = lst[0];
auto a = iota(10).map!(a=>format("%s%d", prefix, a))();
//auto a = sequence!"n+1"().map!(a=>format("%s%d", prefix, a))();
return chain(lst.save, a);
}
Similar code can also produce access violations as can be seen here: http://forum.dlang.org/post/[email protected]
I'm not sure how to go about reducing this. Sometimes I can get it to work (using a global fixed it in one case though wasn't suitable for my needs). Moving it out of its own function into main() also fixes it sometimes but not always.
Comment #1 by eco — 2012-07-27T00:58:21Z
I see my error. Prefix is local so can't be captured in the closure. Passing lst by ref and making it not an rvalue resolves this.
Comment #2 by hsteoh — 2012-10-16T22:13:37Z
According to TDPL (section 5.8.1) it should be valid to reference local variables from a closure. So this is a valid bug. I'll let you decide whether to reopen it; I already opened issue 8832 for probably the same bug. (Looks like in both cases it's the map delegate that's causing trouble.)
Comment #3 by eco — 2012-10-16T22:20:24Z
I've reopened so I'm reminded to check on this if issue 8832 gets fixed.
(In reply to comment #4)
> Tested with 2.060 on DPaste:
>
> 32 bits 64 bits.
> DMD PASS(1) FAIL
> GDC FAIL PASS(2)
> LDC FAIL FAIL
>
> (1) output: ["a", "b", "0", "1", "2"]
> (2) output: ["a", "b", "\x01\x000", "\x01\x001", "\x01\x002"])
>
> http://dpaste.dzfl.pl/fork/2cd77316
I'd just like to add that the PASS cases are still incorrect (obviously since the output differs). They do build though which is what someone reading this should take "PASS" to mean. I just tried with DMD git (2.061 alpha-ish at the moment) and it throws this error message now:
Error: function std.range.chain!(string[],MapResult!(__lambda2, Result)).chain.Result.save cannot get frame pointer to gen
So at least the compiler seems to be somewhat aware of a frame pointer problem now although it should be able to accept it by creating a closure for this bug to be fixed.
Comment #6 by eco — 2013-03-06T09:14:43Z
This is fixed in 2.062 (tested on Windows and Linux).
Comment #7 by andrej.mitrovich — 2013-03-06T09:16:55Z
Fixed but no associated pull -> worksforme.
Don't blame the messenger, that's how Walter wants it.