Can I use Advanced Array Aggregates only with Integer types? The code below does not work because the index types are different. Is it possible to fix this somehow?
with Ada.Streams;
with Interfaces;
procedure Test is
use Ada.Streams;
type Bytes is array (Positive range <>) of Interfaces.Unsigned_8;
X : Bytes := [16#ff#, 16#00#, 16#10#, 16#01#];
Y : Stream_Element_Array := [for Byte of X => Stream_Element (Byte)];
begin
null;
end Test;
Console output
gcc -c -gnat2022 test.adb
test.adb:7:32: error: value not in range of type "Standard.Integer"
test.adb:7:32: error: static expression fails Constraint_Check
gnatmake: "test.adb" compilation error
type Stream_Element_Offset is new Long_Long_Integer;
type Stream_Element_Array is
array (Stream_Element_Offset range <>) of aliased Stream_Element;
Changing your code slightly to correspond,
pragma Ada_2022;
with Ada.Streams;
with Interfaces;
procedure Test is
use Ada.Streams;
type Bytes is array (Positive range <>) of Interfaces.Unsigned_8;
X : Bytes := [16#ff#, 16#00#, 16#10#, 16#01#];
type Integers is array (Stream_Element_Offset range <>) of Integer;
-- type Integers is array (Integer range <>) of Integer;
Z : Integers := [for Byte of X => Integer (Byte)];
begin
null;
end Test;
gives exactly the same error. However, replacing the definition of Integers by the commented-out version, the code compiles OK. So it’s something to do with that Long_Long_Integer.
In either case, gnatpp (and gnatformat, as implemented in VSCode/ALS) crash out on this code!
“gcc version 15.0.0 20240727 (experimental)” has the same error. Looks a bit like a new feature, which needs a complex implementation, has thrown up a lot of challenges; and people are only just starting to trip over problems.