This happens when the first reference to the structural instance is made
within an expression function, because the implicit instantiation is not
performed in an appropriate state by the semantic analyzer.

Tested on x86-64/Linux, applied on the mainline and 16 branch.


2026-10-31  Eric Botcazou  <[email protected]>

        * sem_ch12.adb (Analyze_Structural_Associations): Even in the local
        case, make sure that the implicit instantiation is performed in an
        appropriate state by the semantic analyzer.

-- 
Eric Botcazou
diff --git a/gcc/ada/sem_ch12.adb b/gcc/ada/sem_ch12.adb
index 35aa7c72018..58e117a91ab 100644
--- a/gcc/ada/sem_ch12.adb
+++ b/gcc/ada/sem_ch12.adb
@@ -6517,8 +6517,28 @@ package body Sem_Ch12 is
                   Inside_A_Generic := S_Inside_A_Generic;
                end;
 
+            --  If the scope is local, the instantiation must be fully analyzed
+            --  as well as expanded if the context is not a generic unit.
+
             else
-               Add_Local_Declaration (R, N, Scop => Scop);
+               declare
+                  S_Expander_Active : constant Boolean := Expander_Active;
+                  S_Full_Analysis   : constant Boolean := Full_Analysis;
+                  S_In_Spec_Expr    : constant Boolean := In_Spec_Expression;
+
+               begin
+                  Expander_Active :=
+                    Operating_Mode = Opt.Generate_Code
+                      and then not Inside_A_Generic;
+                  Full_Analysis := True;
+                  In_Spec_Expression := False;
+
+                  Add_Local_Declaration (R, N, Scop => Scop);
+
+                  Expander_Active := S_Expander_Active;
+                  Full_Analysis := S_Full_Analysis;
+                  In_Spec_Expression := S_In_Spec_Expr;
+               end;
             end if;
 
             Ent := Defining_Entity_Of_Instance (R);

Reply via email to