Issue 18949 - Array literals don't work with betterc
Summary: Array literals don't work with betterc
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: betterC
Depends on:
Blocks:
 
Reported: 2018-06-06 00:55 UTC by Jonathan Marler
Modified: 2022-12-27 12:00 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 Jonathan Marler 2018-06-06 00:55:10 UTC
--- bug.d
void takeArray(const(int)[] array)
{
}
void bar()
{
    takeArray([1, 2, 3]);
}


> dmd -c -betterc bug.d

bug.d(6): Error: `TypeInfo` cannot be used with -betterC


It looks like passing an array literal to a function parameter creates a dependency on `TypeInfo`.  It think this has to do with how D is handling the array literal.  It might be trying to allocate it using the GC which is causing it to depend on GC.  You can make the example work if you change bar to the following:

void bar()
{
    static arr = [1, 2, 3];
    takeArray(arr);
}

However, I think both versions should work. Otherwise, a better error message should be in order, i.e.

bug.d(6): dynamic array literals are not supported in betterC.  you can fix this by assigning the array to a static variable instead.
Comment 1 anonymous4 2018-06-06 13:49:21 UTC
Technically it needs `in` attribute:
---
void takeArray(in int[] array)
{
}
void bar()
{
    takeArray([1, 2, 3]);
}
---
Comment 2 Nick Treleaven 2022-12-27 12:00:55 UTC
`array` needs to be `in` or `scope`. The error message has been improved:

arrlit.d(6): Error: expression `[1, 2, 3]` uses the GC and cannot be used with switch `-betterC`