https://gcc.gnu.org/g:53133d07901afa45bd1cb3cdcf7f5d4ed2418dd7
commit r15-5241-g53133d07901afa45bd1cb3cdcf7f5d4ed2418dd7 Author: Eric Botcazou <ebotca...@adacore.com> Date: Mon Oct 28 11:19:10 2024 +0100 ada: Fix internal error on misplaced iterated component association This happens for example in the others choice of a 1-dimensional array: A : array (1 .. Length) of Integer := (others => for Each in 1 .. Length => Each); or when it is used without parentheses for the array component of a record. gcc/ada/ChangeLog: PR ada/112524 PR ada/113781 * par-ch4.adb (P_Primary) <Tok_For>: Give an error about missing parentheses in the (purported) iterated component case too. (P_Unparen_Cond_Expr_Etc): Likewise. * sem.adb (Analyze): Raise PE on N_Iterated_Component_Association. * sem_util.ads (Diagnose_Iterated_Component_Association): Delete. * sem_util.adb (Diagnose_Iterated_Component_Association): Likewise. Diff: --- gcc/ada/par-ch4.adb | 22 ++++++++++++++++++---- gcc/ada/sem.adb | 10 +--------- gcc/ada/sem_util.adb | 41 ----------------------------------------- gcc/ada/sem_util.ads | 4 ---- 4 files changed, 19 insertions(+), 58 deletions(-) diff --git a/gcc/ada/par-ch4.adb b/gcc/ada/par-ch4.adb index e76b0d8bea67..c66f53675044 100644 --- a/gcc/ada/par-ch4.adb +++ b/gcc/ada/par-ch4.adb @@ -3083,7 +3083,7 @@ package body Ch4 is return P_Identifier; end if; - -- For [all | some] indicates a quantified expression + -- Quantified expression or iterated component association when Tok_For => if Token_Is_At_Start_Of_Line then @@ -3103,9 +3103,18 @@ package body Ch4 is ("quantified expression must be parenthesized", Sloc (Node1)); end if; + + -- If no quantifier keyword, this is an iterated component + -- in an aggregate or an ill-formed quantified expression. + else Restore_Scan_State (Scan_State); -- To FOR Node1 := P_Iterated_Component_Association; + + if not (Lparen and then Token = Tok_Right_Paren) then + Error_Msg + ("construct must be parenthesized", Sloc (Node1)); + end if; end if; return Node1; @@ -3983,12 +3992,17 @@ package body Ch4 is ("quantified expression must be parenthesized!", Result); end if; - else - -- If no quantifier keyword, this is an iterated component in - -- an aggregate. + -- If no quantifier keyword, this is an iterated component in + -- an aggregate or an ill-formed quantified expression. + else Restore_Scan_State (Scan_State); Result := P_Iterated_Component_Association; + + if not (Lparen and then Token = Tok_Right_Paren) then + Error_Msg_N + ("construct must be parenthesized!", Result); + end if; end if; -- Declare expression diff --git a/gcc/ada/sem.adb b/gcc/ada/sem.adb index c6d65ea713a1..d93e177c765e 100644 --- a/gcc/ada/sem.adb +++ b/gcc/ada/sem.adb @@ -680,15 +680,6 @@ package body Sem is => null; - -- A quantified expression with a missing "all" or "some" qualifier - -- looks identical to an iterated component association. By language - -- definition, the latter must be present within array aggregates. If - -- this is not the case, then the iterated component association is - -- really an illegal quantified expression. Diagnose this scenario. - - when N_Iterated_Component_Association => - Diagnose_Iterated_Component_Association (N); - when N_Iterated_Element_Association => null; -- May require a more precise error if misplaced. @@ -742,6 +733,7 @@ package body Sem is | N_Function_Specification | N_Generic_Association | N_Index_Or_Discriminant_Constraint + | N_Iterated_Component_Association | N_Iteration_Scheme | N_Mod_Clause | N_Modular_Type_Definition diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb index b1b3891dd65b..a7869397bfcd 100644 --- a/gcc/ada/sem_util.adb +++ b/gcc/ada/sem_util.adb @@ -7167,47 +7167,6 @@ package body Sem_Util is end if; end Designate_Same_Unit; - --------------------------------------------- - -- Diagnose_Iterated_Component_Association -- - --------------------------------------------- - - procedure Diagnose_Iterated_Component_Association (N : Node_Id) is - Def_Id : constant Entity_Id := Defining_Identifier (N); - Aggr : Node_Id; - - begin - -- Determine whether the iterated component association appears within - -- an aggregate. If this is the case, raise Program_Error because the - -- iterated component association cannot be left in the tree as is and - -- must always be processed by the related aggregate. - - Aggr := N; - while Present (Aggr) loop - if Nkind (Aggr) = N_Aggregate then - raise Program_Error; - - -- Prevent the search from going too far - - elsif Is_Body_Or_Package_Declaration (Aggr) then - exit; - end if; - - Aggr := Parent (Aggr); - end loop; - - -- At this point it is known that the iterated component association is - -- not within an aggregate. This is really a quantified expression with - -- a missing "all" or "some" quantifier. - - Error_Msg_N ("missing quantifier", Def_Id); - - -- Rewrite the iterated component association as True to prevent any - -- cascaded errors. - - Rewrite (N, New_Occurrence_Of (Standard_True, Sloc (N))); - Analyze (N); - end Diagnose_Iterated_Component_Association; - ------------------------ -- Discriminated_Size -- ------------------------ diff --git a/gcc/ada/sem_util.ads b/gcc/ada/sem_util.ads index 2f1d2574d37a..2b9ba5f494cd 100644 --- a/gcc/ada/sem_util.ads +++ b/gcc/ada/sem_util.ads @@ -705,10 +705,6 @@ package Sem_Util is -- these names is supposed to be a selected component name, an expanded -- name, a defining program unit name or an identifier. - procedure Diagnose_Iterated_Component_Association (N : Node_Id); - -- Emit an error if iterated component association N is actually an illegal - -- quantified expression lacking a quantifier. - function Discriminated_Size (Comp : Entity_Id) return Boolean; -- If a component size is not static then a warning will be emitted -- in Ravenscar or other restricted contexts. When a component is non-