[Issue 5641] New: Local instantiation does not save context properly

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 22 06:13:28 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5641

           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
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrei at metalanguage.com


--- Comment #0 from Andrei Alexandrescu <andrei at metalanguage.com> 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
std.algorithm.map 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()
            {
                _input.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);
    writeln(m);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list