From: Justin Squirek Date: Sat, 8 May 2021 04:28:55 +0000 (-0400) Subject: [Ada] Spurious initialization of element iterators in "for of" loops X-Git-Tag: basepoints/gcc-13~6246 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=160a6c31074afde0bfc835591a95653c5e65cc45;p=thirdparty%2Fgcc.git [Ada] Spurious initialization of element iterators in "for of" loops gcc/ada/ * exp_ch5.adb (Expand_Formal_Container_Element_Loop): Remove legacy expansion of element iterators, and use expansion form used by unconstrained element types in the general case. --- diff --git a/gcc/ada/exp_ch5.adb b/gcc/ada/exp_ch5.adb index 2cc8b64f083b..3e65084c65f6 100644 --- a/gcc/ada/exp_ch5.adb +++ b/gcc/ada/exp_ch5.adb @@ -4077,7 +4077,6 @@ package body Exp_Ch5 is Make_Defining_Identifier (Loc, Chars => New_External_Name (Chars (Element), 'C')); Elmt_Decl : Node_Id; - Elmt_Ref : Node_Id; Element_Op : constant Entity_Id := Get_Iterable_Type_Primitive (Container_Typ, Name_Element); @@ -4088,19 +4087,10 @@ package body Exp_Ch5 is begin -- For an element iterator, the Element aspect must be present, - -- (this is checked during analysis) and the expansion takes the form: + -- (this is checked during analysis). - -- Cursor : Cursor_Type := First (Container); - -- Elmt : Element_Type; - -- while Has_Element (Cursor, Container) loop - -- Elmt := Element (Container, Cursor); - -- - -- Cursor := Next (Container, Cursor); - -- end loop; - - -- However this expansion is not legal if the element is indefinite. - -- In that case we create a block to hold a variable declaration - -- initialized with a call to Element, and generate: + -- We create a block to hold a variable declaration initialized with + -- a call to Element, and generate: -- Cursor : Cursor_Type := First (Container); -- while Has_Element (Cursor, Container) loop @@ -4132,48 +4122,20 @@ package body Exp_Ch5 is Defining_Identifier => Element, Object_Definition => New_Occurrence_Of (Etype (Element_Op), Loc)); - if not Is_Constrained (Etype (Element_Op)) then - Set_Expression (Elmt_Decl, - Make_Function_Call (Loc, - Name => New_Occurrence_Of (Element_Op, Loc), - Parameter_Associations => New_List ( - Convert_To_Iterable_Type (Container, Loc), - New_Occurrence_Of (Cursor, Loc)))); - - Set_Statements (New_Loop, - New_List - (Make_Block_Statement (Loc, - Declarations => New_List (Elmt_Decl), - Handled_Statement_Sequence => - Make_Handled_Sequence_Of_Statements (Loc, - Statements => Stats)))); - - else - Elmt_Ref := - Make_Assignment_Statement (Loc, - Name => New_Occurrence_Of (Element, Loc), - Expression => - Make_Function_Call (Loc, - Name => New_Occurrence_Of (Element_Op, Loc), - Parameter_Associations => New_List ( - Convert_To_Iterable_Type (Container, Loc), - New_Occurrence_Of (Cursor, Loc)))); - - Prepend (Elmt_Ref, Stats); - - -- The element is assignable in the expanded code - - Set_Assignment_OK (Name (Elmt_Ref)); - - -- The loop is rewritten as a block, to hold the element declaration - - New_Loop := - Make_Block_Statement (Loc, - Declarations => New_List (Elmt_Decl), + Set_Expression (Elmt_Decl, + Make_Function_Call (Loc, + Name => New_Occurrence_Of (Element_Op, Loc), + Parameter_Associations => New_List ( + Convert_To_Iterable_Type (Container, Loc), + New_Occurrence_Of (Cursor, Loc)))); + + Set_Statements (New_Loop, + New_List + (Make_Block_Statement (Loc, + Declarations => New_List (Elmt_Decl), Handled_Statement_Sequence => Make_Handled_Sequence_Of_Statements (Loc, - Statements => New_List (New_Loop))); - end if; + Statements => Stats)))); -- The element is only modified in expanded code, so it appears as -- unassigned to the warning machinery. We must suppress this spurious