Summary: Local instantiation does not save context properly
           Product: D
           Version: D2
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD

--- Comment #0 from Andrei Alexandrescu <> 2011-02-22 
06:10:45 PST ---
The following program compiles but produces an incorrect result. It prints:

[false, false, false, false, true]

although it should print

[true, true, false, false, false]

When fixing this bug, care must be given to avoiding conservative dynamic
allocation of the frames in all cases. This would consistently ruin performance
of most of std.algorithm. Instead, the compiler should detect only the cases
when context must be saved and only act on those cases. Saving should happen
inside non-static struct Result, ideally without dynamic allocation (although
I'm not sure how that would be done in the general case).

Alternatively, the compiler could reject the example as written and require the
user to save the context manually, in a TBD manner. Don, Walter, let's talk
about this, it's important.

After fixing, this example should work if the template map2 is removed and is used instead.

import std.algorithm, std.array, std.stdio;

template map2(alias fun)
    auto map2(R)(R r)
        struct Result
            R _input;

            this(R input)
                _input = input;

            @property bool empty()
                return !_input.length;

            void popFront()

            @property auto ref front()
                return fun(_input.front);

        return Result(r);

auto fun(int[] a)
    auto y = 42;
    auto m = map2!((x) { return x == y; })(a);
    return m;

void main()
    auto a = [ 1, 2, 3, 4, 5 ];
    auto m = fun(a);

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to