www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20734] New: Array literals as arguments to scope slice

https://issues.dlang.org/show_bug.cgi?id=20734

          Issue ID: 20734
           Summary: Array literals as arguments to scope slice parameters
                    should be stack-allocated
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: kinke gmx.net

This currently fails to compile:

void foo() nothrow  safe  nogc
{
    int[5] a = [ 1, 2, 3, 4, 5 ];
    _d_assert_fail!("==")(a, [ 1, 2, 3, 4, 5 ]); // fails: non-nogc
}

// void _d_assert_fail!("==")(ref const(int[5]) a, scope const(int[]) b)
void _d_assert_fail(string comp, A, B)(auto ref const scope A a, auto ref const
scope B b)
{}

The problem is that the array literal is needlessly allocated by the GC,
although _d_assert_fail is not going to escape any reference to its data, and a
stack allocation would do just fine, making this work for -betterC as well as
eliminating one hurdle towards compiling the Phobos unittests with
-checkaction=context.

--
Apr 13 2020