D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20546 - Сast nested array from static to dynamic.
Summary: Сast nested array from static to dynamic.
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: x86 Windows
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-01-30 18:49 UTC by andrei007
Modified: 2020-01-30 19:34 UTC (History)
1 user (show)

See Also:


Attachments
Сast nested array from static to dynamic. (535 bytes, text/plain)
2020-01-30 18:49 UTC, andrei007
Details

Note You need to log in before you can comment on or make changes to this issue.
Description andrei007 2020-01-30 18:49:23 UTC
Created attachment 1774 [details]
Сast nested array from static to dynamic.

Version: v2.090.0
Comment 1 moonlightsentinel 2020-01-30 19:34:12 UTC
The cast from int[2][2] to int[][] is invalid - you cannot slice across multiple dimensions.

import std.stdio;

void main(string[] args)
{
    int[2][2] e = [[1,2],[3,4]];
    int[][] c = cast(int[][]) e;

    writeln(c.length);    // 1
    writeln(c[0].length); // 8589934593
    
    int[2][] valid = e; // no cast needed
    writeln(valid);     // [[1, 2], [3, 4]]
}

The length of a static array is only known at compile time while the length of a dynamic array is a runtime value. Hence c's length is set to some garbage from the stack.