The old gcc version gives me the warning “is not modified, could be declared constant,” but 15.2.1 from February does not

The old gcc version gives me the warning “is not modified, could be declared constant,” but 15.2.1 from February does not.

with Ada.Text_IO; use Ada.Text_IO;

procedure Swap_Test is

procedure Swap (A, B: in out Integer) is
Tmp : Integer := A;   -– should warn me "is not modified, could be declared constant"
begin
A := B;
B := Tmp;
end Swap;

A : Integer := 10;
B : Integer := 20;

begin

Swap (A, B);

Put_Line ("A: " & A’Image);
Put_Line ("B: " & B’Image);

end Swap_Test;
$ /usr/bin/gnatmake -f -gnatwa swap_test.adb
x86_64-linux-gnu-gcc-9 -c -gnatwa swap_test.adb
swap_test.adb:7:07: warning: “Tmp” is not modified, could be declared constant
x86_64-linux-gnu-gnatbind-9 -x swap_test.ali
x86_64-linux-gnu-gnatlink-9 swap_test.ali

but

$ \~/opt/gcc-15-20260221/bin/gnatmake -f -gnatwa swap_test.adb
gcc -c -gnatwa swap_test.adb
gnatbind -x swap_test.ali
gnatlink swap_test.ali

Refer to GNAT Refererence Manual 2.196 Pragma Unmodified:

For the variable case, warnings are never given for unreferenced variables whose name contains one of the substrings DISCARD, DUMMY, IGNORE, JUNK, UNUSE, TMP, TEMP in any casing. Such names are typically to be used in cases where such warnings are expected. Thus it is never necessary to use pragma Unmodified for such variables, though it is harmless to do so.

You’re using a variable name Tmp so no warnings.
We could discuss if your variable meets the ‘unreferenced’ qualifier (perhaps it should say ‘unmodified’), but it’s really not worth anyone’s time.
This is one of the silly things GNAT does quietly, but even then you gave the variable a sloppy name (happens to all of us).
It should be called A_Copy, which not only represents the purpose of the variable (unlike Tmp), but also saves you from GNAT footguns.

Check it out here:

I also recommend using code blocks to format your posts next time because this one here is hard to read.

3 Likes

Welcome to the forum @stanrifkin!

Adding to the above, and using Compiler Explorer, this behaviour changed from v12 → v13. It is still present in v12. It also seems that v13 generates more efficient code, so the tmp var may end up being hidden/unused/optimized out and therefore GNAT does not even warn about it.

Best regards,
Fer

(also, I have edited your post to make it more readable :slight_smile: )

I reported this, and it is not a bug:

That came from an unexpected direction. Thank you.