D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2608 - int[] literal too polysemous
Summary: int[] literal too polysemous
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-01-23 16:22 UTC by Andrei Alexandrescu
Modified: 2015-06-09 01:20 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 Andrei Alexandrescu 2009-01-23 16:22:45 UTC
The following code fails:

void next(T)(ref T[] a) { assert(a.length); a = a[1 .. $]; }

void main()
{
    auto a = [ 1, 2, 3 ];
    a.next;
    assert(a == [ 2, 3 ]);
}

If one replaces "auto" with "int[]", the code compiles and runs as expected. It looks like auto with literals tries too hard to keep it around as an rvalue.

This seems to be related to issue 2606.
Comment 1 Ary Borenszweig 2009-01-23 17:47:05 UTC
The problem is that an array literal has a static array type by default. Wouldn't it be more user-friendly for it to be a dynamic array type?
Comment 2 Andrei Alexandrescu 2009-01-23 20:03:18 UTC
(In reply to comment #1)
> The problem is that an array literal has a static array type by default.
> Wouldn't it be more user-friendly for it to be a dynamic array type?

Oh, you are right. Walter did change the default type of literals to dynamic-length, but apparently only for strings.
Comment 3 Koroskin Denis 2009-01-24 00:53:33 UTC
Could you please post generated error message, too, so that other don't have to guess? :)

...
a.next; // Error: cast(int[])a is not an lvalue
...
Comment 4 bearophile_hugs 2010-10-29 09:47:02 UTC
That code works with dmd 2.050.