Skip to content

[Bug] .type on slices and rank-view arrays incorrectly reports a "normal" rectangular array #28277

@DanilaFe

Description

@DanilaFe

Consider the following program:

var A = [0,1,2,3,4,5,6,7,8,9,10];
var B = A[1..6];
ref C = A[1..6];

compilerWarning("B.type == C.type is " + (B.type == C.type) : string); // prints true
ref Bref = reshape(B, 0..2, 0..1); // works fine
ref Cref = reshape(C, 0..2, 0..1); // error: This array type does not support alias-based reshaping

I am compiling this with the preview edition to get aliasing reshape, which is required to reproduce the issue in this form, though it's present more generally regardless of edition.

Here, the compiler claims that B and C have the same type. However, although I can call reshape on B, I can't do the same for C, because "this type of array does not support aliasing". In general, it's clear that B and C actually have different types. One way to check this is to examine the _instance field, which reports DefaultRectangularArray for B but ArrayViewSliceArr for C.

The issue turns out to be that for array and domain types, .type constructs a runtime type, which boils down to invoking chpl__convertValueToRuntimeType. This boils down to invoking dom.createArray on the domain of the array. However, slice types do not have their own unique domain implementations (like DefaultRectangularDom), and instead use default domains. As a result, SliceArr.type produces a default rectangular array type. This results in weird cases where trying to create a second reference, while explicitly specifying a type, breaks:

var x = 42;
ref y = x;
ref z: y.type = y;

z = 1;
writeln(x);

var X = [0,1,2,3,4,5,6,7,8,9,10];
ref Y = X[1..5];
ref Z: Y.type = Y; // error: creating a reference to an incompatible type
Z[0] = 99;
writeln(X);

I suspect this is a bug, though I can see a world in which this is also a language design concern (what should .type do for types that weren't explicitly dmapped?). Probably the fix would be to construct custom domain classes for array slices that differ only in how they createArray (probably just halting). But then, the following would halt:

var B: A[1..5].type;

Though it seems moderately reasonable for it to work.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions