D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20734 - Array literals as arguments to scope slice parameters should be stack-allocated
Summary: Array literals as arguments to scope slice parameters should be stack-allocated
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: pull
: 11657 (view as issue list)
Depends on:
Blocks:
 
Reported: 2020-04-13 14:36 UTC by kinke
Modified: 2022-06-07 17:25 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description kinke 2020-04-13 14:36:31 UTC
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.
Comment 1 Dlang Bot 2020-04-14 12:23:40 UTC
@kinke created dlang/dmd pull request #11039 "Fix issue 20734 - Allocate array literals as arguments to scope slice parameters on the stack" fixing this issue:

- Fix issue 20734 - Allocate array literals as arguments to scope slice parameters on the stack

https://github.com/dlang/dmd/pull/11039
Comment 2 Dlang Bot 2020-04-15 18:31:22 UTC
dlang/dmd pull request #11039 "Fix issue 20734 - Allocate array literals as arguments to scope slice parameters on the stack" was merged into master:

- f551f1b810d798a9ea9222e7e228ede32a90b6c2 by Martin Kinkelin:
  Fix issue 20734 - Allocate array literals as arguments to scope slice parameters on the stack

https://github.com/dlang/dmd/pull/11039
Comment 3 Mathias LANG 2020-05-10 06:40:59 UTC
*** Issue 11657 has been marked as a duplicate of this issue. ***