What is wrong in this short code?

Hello,

Assume his code:

pragma Ada_2022;
with Ada.Text_IO;
procedure test1 is
   hist1    : constant array(1..4) of Natural := [10,20,30,40];
   x : constant float := float(hist1'Reduce("+",0));
begin
   Ada.Text_IO.put_line("x: " & x'Image);
end test1;

When I compile, I get this message:

test1.adb:5:37: error: expected type universal integer
test1.adb:5:37: error: found type “Standard.Integer”

reinert

Your snippet seems to compile with gnat 14.x but not 13.x. godbolt

3 Likes

I’m pretty sure this is an implementation bug: Universal_Integer is a virtual/conceptual type (from which any integral type is derived/subtyped) that you cannot declare.

As an aged Ada user I would say, wrong are:

  • pragma Ada_2022
  • Square brackets
  • X’Reduce

This is not Ada and against the core principles of original Ada design.

However, try:

x : constant float := float(hist1'Reduce("+",Natural'(0)));

That might help the compiler some signal in this noise… :grinning:

See Reduction Expressions (ARM 2022 4-5-10)

A’Reduce("+",0) is in Ada2022.

But I guess that by « it is not Ada », you mean « not original Ada ». Where should we put the bar between using strictly Ada83… and using new features.

1 Like

Ada 95/2005. After that the train left the station…

:sunglasses:

1 Like

Answering technical questions clearly without complaining about decade old technical decisions prevents confusion like this.

2 Likes

Surely, the issue is also resolved with Integer'(0) instead of 0. I deduce the compiler claims it doesn’t want an (implicit) Standard.Integer… but is happy with an explicit Integer.

@pyj I don’t mean to complain about decade old decisions. I appreciate the quality of the Ada83 language design, and the inclusion of high level features like generics (C# and Java fail to include in the first versions !). However I think that there is a room for enhancement. Afterward, deciding if a new feature is worth been added can be subjective. Here we have more likely a bug of the implementation (gnat probably) which is different than a language design issue. But I have to admit that a 2022 feature is probably less well implemented than an earlier one.

@F-Loyer my comment wasn’t targeted at you at all, I quoted because I could see how someone might have gotten confused. Ada needs better tutorials and docs as demonstrated by difficulties of people trying to examine it. It’s disheartening to see people asking technical questions here and then the responses being increasingly more opining about the solution, than at accurately communicating it.

3 Likes

No, that’s the-other-way-'round: type-hinting to the compiler that the Universal_Integer that corresponds-with/-to and is literal 0 is, in fact, Integer. (Absent shadowing/scope, Standard.Integer.)

I think I get what you mean.

But it compiles like it. I was quite surprised since the error is about the compiler has found a Standard.Integer… (error: found type “Standard.Integer”) and with an explicit Integer, there is nothing wrong. Perhaps the error messages are exchanged.

But it’s a bug in the compiler, which has been fixed! as @jcmoyer noted upthread. Upgrade to GCC 14!

2 Likes

I use linux (debian). How can I upgrade?

Use Alire. It’s a package manager for GNAT and can download the correct GNAT version for you. Alire can also download cross compiler and Ada libraries. Alire is the way to go even if you don’t have a bug.

Then I get

  1. gnat_native=13.2.1

as the newest version.

reinert

Are you able to grab version 14 via alr toolchain --select ?

You can also add this to your project’s alire.toml:

[[depends-on]]
gnat = ">=14"

That should force it to find and install version 14 when compiling your package via alr build

Nope, version 14 does not show up via toolchain, and

[[depends-on]]
gnat = ">=14"

in alire.toml just gives

gprconfig: can't find a native toolchain for language 'ada'
gprconfig: can't find a native toolchain for language 'ada'
test2.gpr:2:09: no compiler for language "Ada", cannot compile "test2_config.ads"
gprbuild: *** compilation phase failed
error: Command ["gprbuild", "-s", "-j0", "-p", "-P", "/home/reinert/test2/test2.gpr"] exited with code 4
error: Compilation failed.

reinert

What Alire version are you using and did you updated the index?

Best regards,
Fer

alire 1.2.1-1+b1

Yes, I did issue the commands:

alr index --update-all
alr toolchain --select

several times :slight_smile:

Getting “Already up to date.”

I run Debian at home, I’ll try your example and report my settings when I get back today.