[Ada] Spurious warning about premature use of selected component

Message ID 20210503092945.GA77515@adacore.com
State New
Headers show
Series
  • [Ada] Spurious warning about premature use of selected component
Related show

Commit Message

Pierre-Marie de Rodat May 3, 2021, 9:29 a.m.
Compiler emits a warning on a selected component that is an actual in a
function call, when the enclosing object has no explicit initialization
and no discriminants, but the component type itself has partial
initialization.  Such a warning should be suppressed.

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

gcc/ada/

	* sem_warn.adb (Check_References): Do not emit warning on a
	selected component when enclosing type has no discriminant and
	type of component has partial initialization.

Patch

diff --git a/gcc/ada/sem_warn.adb b/gcc/ada/sem_warn.adb
--- a/gcc/ada/sem_warn.adb
+++ b/gcc/ada/sem_warn.adb
@@ -1523,6 +1523,17 @@  package body Sem_Warn is
                         --  uninitialized component to get a better message.
 
                      elsif Nkind (Parent (UR)) = N_Selected_Component then
+                        --  Suppress possibly superfluous warning if component
+                        --  is known to exist and is partially initialized.
+
+                        if not Has_Discriminants (Etype (E1))
+                          and then
+                            Is_Partially_Initialized_Type
+                              (Etype (Parent (UR)), False)
+                        then
+                           goto Continue;
+                        end if;
+
                         Error_Msg_Node_2 := Selector_Name (Parent (UR));
 
                         if not Comes_From_Source (Parent (UR)) then