[Ada] Fix analysis of Relaxed_Initialization for bodies-as-specs
authorPiotr Trojanek <trojanek@adacore.com>
Wed, 1 Apr 2020 09:43:39 +0000 (11:43 +0200)
committerPierre-Marie de Rodat <derodat@adacore.com>
Mon, 15 Jun 2020 08:04:36 +0000 (04:04 -0400)
2020-06-15  Piotr Trojanek  <trojanek@adacore.com>

gcc/ada/

* sem_ch13.adb (Analyze_Aspect_Relaxed_Initialization): Fix
dealing with scopes on subprogram bodies that act as specs.
* sem_util.adb (Has_Relaxed_Initialization): Fix trivial
mistake.

gcc/ada/sem_ch13.adb
gcc/ada/sem_util.adb

index 3bdc39a..19a0780 100644 (file)
@@ -2203,6 +2203,10 @@ package body Sem_Ch13 is
                --  Items that appear in the relaxed initialization aspect
                --  expression of a subprogram; for detecting duplicates.
 
+               Restore_Scope : Boolean;
+               --  Will be set to True if we need to restore the scope table
+               --  after analyzing the aspect expression.
+
             --  Start of processing for Analyze_Aspect_Relaxed_Initialization
 
             begin
@@ -2231,17 +2235,23 @@ package body Sem_Ch13 is
                elsif Is_Subprogram (E) then
                   if Present (Expr) then
 
-                     --  Subprogram and its formal parameters must be visible
-                     --  when analyzing the aspect expression.
-
-                     pragma Assert (not In_Open_Scopes (E));
+                     --  If we analyze subprogram body that acts as its own
+                     --  spec, then the subprogram itself and its formals are
+                     --  already installed; otherwise, we need to install them,
+                     --  as they must be visible when analyzing the aspect
+                     --  expression.
 
-                     Push_Scope (E);
-
-                     if Is_Generic_Subprogram (E) then
-                        Install_Generic_Formals (E);
+                     if In_Open_Scopes (E) then
+                        Restore_Scope := False;
                      else
-                        Install_Formals (E);
+                        Restore_Scope := True;
+                        Push_Scope (E);
+
+                        if Is_Generic_Subprogram (E) then
+                           Install_Generic_Formals (E);
+                        else
+                           Install_Formals (E);
+                        end if;
                      end if;
 
                      --  Aspect expression is either an aggregate with list of
@@ -2281,7 +2291,9 @@ package body Sem_Ch13 is
                         Analyze_Relaxed_Parameter (E, Expr, Seen);
                      end if;
 
-                     End_Scope;
+                     if Restore_Scope then
+                        End_Scope;
+                     end if;
                   else
                      Error_Msg_N ("missing expression for aspect %", N);
                   end if;
index 31e03fd..a3dbaaf 100644 (file)
@@ -12511,7 +12511,8 @@ package body Sem_Util is
 
                if Has_Aspect (Subp_Id, Aspect_Relaxed_Initialization) then
                   Aspect_Expr :=
-                    Find_Value_Of_Aspect (E, Aspect_Relaxed_Initialization);
+                    Find_Value_Of_Aspect
+                      (Subp_Id, Aspect_Relaxed_Initialization);
 
                   --  Aspect expression is either an aggregate, e.g.:
                   --