If a pragma Compile_Time_Error contains T'Size, then T'Size can be
computed as 0 instead of the correct value, causing missing or extra
error messages. The same applies to pragma Compile_Time_Warning.  This
patch fixes that bug.

Running these commands:

  gcc -c -g -O0 -gnata fillers-neq_error.ads
  gcc -c -g -O0 -gnata fillers-neq_error-main.adb

On the following sources:

package Fillers is

   Filler_Bytes : constant Long_Long_Integer := 1024 / 8;

   type Filler_Range is new Long_Long_Integer range 0 .. Filler_Bytes - 1;

   type Integer_8 is mod 2**8 with Size => 8;

   type Filler_Type is array (Filler_Range) of Integer_8
   with Pack;

end Fillers;
package Fillers.Neq_Error is

   pragma Compile_Time_Error
     (Filler_Type'Size /= 1024, "Filler_Type'Size /= 1024");

end Fillers.Neq_Error;
with Text_IO; use Text_IO;
procedure Fillers.Neq_Error.Main is
   X : constant Integer := Filler_Type'Size;
begin
   Put_Line (X'Img);
end Fillers.Neq_Error.Main;

Should execute silently.

Tested on x86_64-pc-linux-gnu, committed on trunk

2019-10-10  Bob Duff  <d...@adacore.com>

gcc/ada/

        * sem_prag.adb (Defer_Compile_Time_Warning_Error_To_BE): In
        addition to saving the pragma for further processing, copy the
        pragma into the main unit if necessary.
--- gcc/ada/sem_prag.adb
+++ gcc/ada/sem_prag.adb
@@ -32197,6 +32197,15 @@ package body Sem_Prag is
         (New_Val => CTWE_Entry'(Eloc  => Sloc (Arg1),
                                 Scope => Current_Scope,
                                 Prag  => N));
+
+      --  If the Boolean expression contains T'Size, and we're not in the main
+      --  unit being compiled, then we need to copy the pragma into the main
+      --  unit, because otherwise T'Size might never be computed, leaving it
+      --  as 0.
+
+      if not In_Extended_Main_Code_Unit (N) then
+         Insert_Library_Level_Action (New_Copy_Tree (N));
+      end if;
    end Defer_Compile_Time_Warning_Error_To_BE;
 
    ------------------------------------------

Reply via email to