This patch fixes a bug where the compiler allowed mismatched brackets
and parentheses in aggregates in -gnatX mode, as in (...] and [...).

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

gcc/ada/

        * par-ch4.adb (P_Aggregate_Or_Paren_Expr): Require matching
        parens or brackets.
        * par.adb, par-tchk.adb (T_Right_Bracket): New procedure to give
        an error on missing ].
diff --git a/gcc/ada/par-ch4.adb b/gcc/ada/par-ch4.adb
--- a/gcc/ada/par-ch4.adb
+++ b/gcc/ada/par-ch4.adb
@@ -1391,6 +1391,9 @@ package body Ch4 is
          return Maybe;
       end Is_Quantified_Expression;
 
+      Start_Token : constant Token_Type := Token;
+      --  Used to prevent mismatches (...] and [...)
+
    --  Start of processing for P_Aggregate_Or_Paren_Expr
 
    begin
@@ -1697,23 +1700,26 @@ package body Ch4 is
          end if;
       end loop;
 
-      --  All component associations (positional and named) have been scanned
+      --  All component associations (positional and named) have been scanned.
+      --  Scan ] or ) based on Start_Token.
 
-      if Token = Tok_Right_Bracket and then Ada_Version >= Ada_2020 then
-         Set_Component_Associations (Aggregate_Node, Assoc_List);
-         Set_Is_Homogeneous_Aggregate (Aggregate_Node);
-         Scan;  --  past right bracket
+      case Start_Token is
+         when Tok_Left_Bracket =>
+            Set_Component_Associations (Aggregate_Node, Assoc_List);
+            Set_Is_Homogeneous_Aggregate (Aggregate_Node);
+            T_Right_Bracket;
 
-         if Token = Tok_Apostrophe then
-            Scan;
+            if Token = Tok_Apostrophe then
+               Scan;
 
-            if Token = Tok_Identifier then
-               return P_Reduction_Attribute_Reference (Aggregate_Node);
+               if Token = Tok_Identifier then
+                  return P_Reduction_Attribute_Reference (Aggregate_Node);
+               end if;
             end if;
-         end if;
-      else
-         T_Right_Paren;
-      end if;
+         when Tok_Left_Paren =>
+            T_Right_Paren;
+         when others => raise Program_Error;
+      end case;
 
       if Nkind (Aggregate_Node) /= N_Delta_Aggregate then
          Set_Expressions (Aggregate_Node, Expr_List);


diff --git a/gcc/ada/par-tchk.adb b/gcc/ada/par-tchk.adb
--- a/gcc/ada/par-tchk.adb
+++ b/gcc/ada/par-tchk.adb
@@ -402,6 +402,20 @@ package body Tchk is
       Check_Token (Tok_Record, AP);
    end T_Record;
 
+   ---------------------
+   -- T_Right_Bracket --
+   ---------------------
+
+   procedure T_Right_Bracket is
+   begin
+      if Token = Tok_Right_Bracket then
+         Scan;
+      else
+         Error_Msg_AP -- CODEFIX
+           ("|missing ""']'""");
+      end if;
+   end T_Right_Bracket;
+
    -------------------
    -- T_Right_Paren --
    -------------------


diff --git a/gcc/ada/par.adb b/gcc/ada/par.adb
--- a/gcc/ada/par.adb
+++ b/gcc/ada/par.adb
@@ -1212,6 +1212,7 @@ function Par (Configuration_Pragmas : Boolean) return List_Id is
       procedure T_Private;
       procedure T_Range;
       procedure T_Record;
+      procedure T_Right_Bracket;
       procedure T_Right_Paren;
       procedure T_Semicolon;
       procedure T_Then;


Reply via email to