The implementation of the build-in-place return protocol for functions whose result type is an unconstrained array type generates dangling references to local bounds built on the stack for the result as soon as these bounds are not static. The reason is that the implementation treats the return object, either explicitly present in the source or synthesized by the compiler, as a regular constrained object until very late in the game, although it needs to be ultimately rewritten as the renaming of the dereference of an allocator with unconstrained designated type in order for the bounds to be part of the allocation.
Recently a partial fix was implemented for the case where the result is an aggregate, by preventing the return object from being expanded after it has been analyzed. However, it does not work for the general case of extended return statements, because the statements therein are still analyzed with the constrained version of the return object so, after it is changed into the unconstrained renaming, this yields (sub)type mismatches. Therefore this change goes the other way around: it rolls back the partial fix and instead performs the transformation of the return object into the unconstrained renaming during the expansion of its declaration, in other words before statements referencing it, if any, are analyzed, thus ensuring that they see the final version of the object. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * exp_aggr.adb (Expand_Array_Aggregate): Remove obsolete code. Delay the expansion of aggregates initializing return objects of build-in-place functions. * exp_ch3.ads (Ensure_Activation_Chain_And_Master): Delete. * exp_ch3.adb (Ensure_Activation_Chain_And_Master): Fold back to... (Expand_N_Object_Declaration): ...here. Perform the expansion of return objects of build-in-place functions here instead of... * exp_ch6.ads (Is_Build_In_Place_Return_Object): Declare. * exp_ch6.adb (Expand_N_Extended_Return_Statement): ...here. (Is_Build_In_Place_Result_Type): Alphabetize. (Is_Build_In_Place_Return_Object): New predicate. * exp_ch7.adb (Enclosing_Function): Delete. (Process_Object_Declaration): Tidy up handling of return objects. * sem_ch3.adb (Analyze_Object_Declaration): Do not decorate and freeze the actual type if it is the same as the nominal type. * sem_ch6.adb: Remove use and with clauses for Exp_Ch3. (Analyze_Function_Return): Analyze again all return objects. (Create_Extra_Formals): Do not force the definition of an Itype if the subprogram is a compilation unit.
patch.diff.gz
Description: application/gzip