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:
Though it seems moderately reasonable for it to work.
Consider the following program:
I am compiling this with the
previewedition to get aliasingreshape, which is required to reproduce the issue in this form, though it's present more generally regardless of edition.Here, the compiler claims that
BandChave the same type. However, although I can callreshapeonB, I can't do the same forC, because "this type of array does not support aliasing". In general, it's clear thatBandCactually have different types. One way to check this is to examine the_instancefield, which reportsDefaultRectangularArrayforBbutArrayViewSliceArrforC.The issue turns out to be that for array and domain types,
.typeconstructs a runtime type, which boils down to invokingchpl__convertValueToRuntimeType. This boils down to invokingdom.createArrayon the domain of the array. However, slice types do not have their own unique domain implementations (likeDefaultRectangularDom), and instead use default domains. As a result,SliceArr.typeproduces a default rectangular array type. This results in weird cases where trying to create a second reference, while explicitly specifying a type, breaks:I suspect this is a bug, though I can see a world in which this is also a language design concern (what should
.typedo for types that weren't explicitlydmapped?). Probably the fix would be to construct custom domain classes for array slices that differ only in how theycreateArray(probably just halting). But then, the following would halt:Though it seems moderately reasonable for it to work.