Advanced Array Aggregates only possible with integer type?

Hello

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

This looks quite likely to be a compiler error.

From GNAT’s Ada.Streams,

   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!

$ gnatpp --pipe gast.adb
gast.adb:1:1: Error formatting node (CompilationUnit). Keeping the initial input selection unchanged
null template: <IteratedAssoc gast.adb:10:21-10:52>
1 Like

GCC PR117328 submitted.

3 Likes

Thank you very much! This is the second array aggregate compiler error I have encountered…

“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.

3 Likes

Comment by GCC developer (who is investigating): “Strange.”

Bug has been fixed: gcc.gnu.org Git - gcc.git/commit

2 Likes