[Ada] Fix a bug in the contract of formal ordered sets

Message ID 20220906071550.GA1280361@poulhies-Precision-5550
State New, archived
Headers
Series [Ada] Fix a bug in the contract of formal ordered sets |

Commit Message

Marc Poulhiès Sept. 6, 2022, 7:15 a.m. UTC
  There are 2 main issues in the postcondition of the function Replace
of the formal ordered sets, in the sub package Generic_Keys. One is
related to the fact that when the element is changed, the key is
also changed. The second one is due to the fact that the arrival of
the new element might modify the ordering of the set and thus
the Positions might be modified.
As a consequence, the postcondition might be false and thus fail
at runtime.

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

gcc/ada/

	* libgnat/a-cforse.ads (Replace): Fix the postcondition.
  

Patch

diff --git a/gcc/ada/libgnat/a-cforse.ads b/gcc/ada/libgnat/a-cforse.ads
--- a/gcc/ada/libgnat/a-cforse.ads
+++ b/gcc/ada/libgnat/a-cforse.ads
@@ -1598,7 +1598,7 @@  is
 
              --  Key now maps to New_Item
 
-             and Element (Container, Key) = New_Item
+             and Element (Container, Find (Container, Key)'Old) = New_Item
 
              --  New_Item is contained in Container
 
@@ -1622,8 +1622,9 @@  is
                     E_Right  => Elements (Container),
                     P_Left   => Positions (Container)'Old,
                     P_Right  => Positions (Container),
-                    Position => Find (Container, Key))
-             and Positions (Container) = Positions (Container)'Old;
+                    Position => Find (Container, Key)'Old)
+             and P.Keys_Included (Positions (Container),
+                                  Positions (Container)'Old);
 
       procedure Exclude (Container : in out Set; Key : Key_Type) with
         Global         => null,