phobos icon indicating copy to clipboard operation
phobos copied to clipboard

std.algorithm.iteration.each should be usable in @safe code

Open dlangBugzillaToGithub opened this issue 7 years ago • 1 comments

greensunny12 reported this on 2018-01-03T02:16:44Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=18160

Description

@safe unittest
{
    import std.algorithm.iteration : each;
    import std.range : lockstep;

    auto a = [ 1, 2, 3 ];
    auto b = [ 2, 3, 4 ];

    a.lockstep(b).each!((ref x, ref y) { ++x; ++y; });

    assert(a == [ 2, 3, 4 ]);
    assert(b == [ 3, 4, 5 ]);
}

dlangBugzillaToGithub avatar Jan 03 '18 02:01 dlangBugzillaToGithub

Similar to #10368, this is just fundamentally an issue of cases where opApply can't template the delegate since this would break type inference, i.e. (ref x, ref y) would have to provide a type then. each infers @safe and other attributes correctly if it iterates over a range and doesn't use the opApply interface

Error: `@safe` function `D main` cannot call `@system` function `t.main.each!(Lockstep!(int[], int[])).each`
/usr/include/dlang/dmd/std/range/package.d-mixin-6198(6199):        which calls `std.range.Lockstep!(int[], int[]).Lockstep.opApply`
/usr/include/dlang/dmd/std/range/package.d-mixin-6198(6209):        which wasn't inferred `@safe` because of:
/usr/include/dlang/dmd/std/range/package.d-mixin-6198(6209):        `@safe` function `opApply` cannot call `@system` `dg`

Inkrementator avatar Apr 03 '25 19:04 Inkrementator