[Ada] Fix the check of the 'Old prefix

Message ID 20220107162726.GA948994@adacore.com
State Committed
Commit 9b573d421a457852da09253f966246fcc0e16a27
Headers
Series [Ada] Fix the check of the 'Old prefix |

Commit Message

Pierre-Marie de Rodat Jan. 7, 2022, 4:27 p.m. UTC
  The check did miss the part of Ada 2022 RM 6.1.1 (27/5) that the use of
an entity declared within the postcondition expression is allowed if
it's declared within the prefix itself.

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

gcc/ada/

	* sem_attr.adb (Check_Reference): Fix condition.
  

Patch

diff --git a/gcc/ada/sem_attr.adb b/gcc/ada/sem_attr.adb
--- a/gcc/ada/sem_attr.adb
+++ b/gcc/ada/sem_attr.adb
@@ -5151,11 +5151,14 @@  package body Sem_Attr is
                --  Entities mentioned within the prefix of attribute 'Old must
                --  be global to the related postcondition. If this is not the
                --  case, then the scope of the local entity is nested within
-               --  that of the subprogram.
+               --  that of the subprogram. Moreover, we need to know whether
+               --  Entity (Nod) occurs in the tree rooted at the prefix to
+               --  ensure the entity is not declared within then prefix itself.
 
                elsif Is_Entity_Name (Nod)
                  and then Present (Entity (Nod))
                  and then Scope_Within (Scope (Entity (Nod)), Subp_Id)
+                 and then not In_Subtree (Entity (Nod), P)
                then
                   Error_Attr
                     ("prefix of attribute % cannot reference local entities",