]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix spurious range check for out mode scalars in class-wide preconditions
authorDenis Mazzucato <mazzucato@adacore.com>
Mon, 22 Dec 2025 12:44:41 +0000 (13:44 +0100)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Mon, 25 May 2026 08:28:05 +0000 (10:28 +0200)
This patch fixes a spurious range check that was generated for out mode scalars
after a class-wide precondition call. This issue was not present in static
preconditions, but only in dynamic ones. This patch refactors the two helper
call builders into the same to ensure the same processing of actuals.

gcc/ada/ChangeLog:

* exp_ch6.adb (Build_Helper_Call): Refactor code to handle both static and
dynamic precondition calls in the same way.

gcc/ada/exp_ch6.adb

index e769d3936f70a1700b33564854817f926413b815..70b5a66ed13db0431e6fe9b67a02dc9668fbd62e 100644 (file)
@@ -8290,19 +8290,14 @@ package body Exp_Ch6 is
    procedure Install_Class_Preconditions_Check (Call_Node : Node_Id) is
       Loc : constant Source_Ptr := Sloc (Call_Node);
 
-      function Build_Dynamic_Check_Helper_Call return Node_Id;
-      --  Build call to the helper runtime function of the nearest ancestor
-      --  of the target subprogram that dynamically evaluates the merged
-      --  or-else preconditions.
-
       function Build_Error_Message (Subp_Id : Entity_Id) return Node_Id;
       --  Build message associated with the class-wide precondition of Subp_Id
       --  indicating the call that caused it.
 
-      function Build_Static_Check_Helper_Call return Node_Id;
+      function Build_Helper_Call (Dynamic : Boolean) return Node_Id;
       --  Build call to the helper runtime function of the nearest ancestor
-      --  of the target subprogram that dynamically evaluates the merged
-      --  or-else preconditions.
+      --  of the target subprogram that statically or dynamically (depending on
+      --  the Dynamic flag) evaluates the merged or-else preconditions.
 
       function Class_Preconditions_Subprogram
         (Spec_Id : Entity_Id;
@@ -8315,39 +8310,6 @@ package body Exp_Ch6 is
       --  preconditions; return Empty when not available (which means that no
       --  preconditions check is required).
 
-      -------------------------------------
-      -- Build_Dynamic_Check_Helper_Call --
-      -------------------------------------
-
-      function Build_Dynamic_Check_Helper_Call return Node_Id is
-         Spec_Id   : constant Entity_Id := Entity (Name (Call_Node));
-         CW_Subp   : constant Entity_Id :=
-                       Class_Preconditions_Subprogram (Spec_Id,
-                         Dynamic => True);
-         Helper_Id : constant Entity_Id :=
-                       Dynamic_Call_Helper (CW_Subp);
-         Actuals   : constant List_Id := New_List;
-         A         : Node_Id   := First_Actual (Call_Node);
-
-      begin
-         while Present (A) loop
-
-            --  Ensure that the evaluation of the actuals will not produce
-            --  side effects.
-
-            Remove_Side_Effects (A);
-
-            Append_To (Actuals, New_Copy_Tree (A));
-
-            Next_Actual (A);
-         end loop;
-
-         return
-           Make_Function_Call (Loc,
-             Name => New_Occurrence_Of (Helper_Id, Loc),
-             Parameter_Associations => Actuals);
-      end Build_Dynamic_Check_Helper_Call;
-
       -------------------------
       -- Build_Error_Message --
       -------------------------
@@ -8429,11 +8391,11 @@ package body Exp_Ch6 is
          return Make_String_Literal (Loc, Name_Buffer (1 .. Name_Len));
       end Build_Error_Message;
 
-      ------------------------------------
-      -- Build_Static_Check_Helper_Call --
-      ------------------------------------
+      -----------------------
+      -- Build_Helper_Call --
+      -----------------------
 
-      function Build_Static_Check_Helper_Call return Node_Id is
+      function Build_Helper_Call (Dynamic : Boolean) return Node_Id is
          Actuals   : constant List_Id := New_List;
          A         : Node_Id;
          Helper_Id : Entity_Id;
@@ -8451,11 +8413,14 @@ package body Exp_Ch6 is
          --  Common case
 
          else
-            CW_Subp := Class_Preconditions_Subprogram (Spec_Id,
-                         Dynamic => False);
+            CW_Subp := Class_Preconditions_Subprogram (Spec_Id, Dynamic);
          end if;
 
-         Helper_Id := Static_Call_Helper (CW_Subp);
+         if Dynamic then
+            Helper_Id := Dynamic_Call_Helper (CW_Subp);
+         else
+            Helper_Id := Static_Call_Helper (CW_Subp);
+         end if;
 
          F := First_Formal (Helper_Id);
          A := First_Actual (Call_Node);
@@ -8484,7 +8449,7 @@ package body Exp_Ch6 is
            Make_Function_Call (Loc,
              Name => New_Occurrence_Of (Helper_Id, Loc),
              Parameter_Associations => Actuals);
-      end Build_Static_Check_Helper_Call;
+      end Build_Helper_Call;
 
       ------------------------------------
       -- Class_Preconditions_Subprogram --
@@ -8627,11 +8592,7 @@ package body Exp_Ch6 is
 
       --  Build and install the check
 
-      if Dynamic_Check then
-         Cond := Build_Dynamic_Check_Helper_Call;
-      else
-         Cond := Build_Static_Check_Helper_Call;
-      end if;
+      Cond := Build_Helper_Call (Dynamic_Check);
 
       if Exception_Locations_Suppressed then
          Fail :=