This patch modifies the processing of SPARK annotations Initializes and Initial_Condition to perform the resolution of the related expressions at the end of the enclosing package visible declarations.
------------ -- Source -- ------------ -- init_cond.ads package Init_Cond with SPARK_Mode, Initial_Condition => Vis_Var -- OK and Vis_Func -- OK and Vis_Nested.Var -- OK and Vis_Nested.Func -- OK and Priv_Var -- Error and Priv_Func -- Error and Priv_Nested.Var -- Error and Priv_Nested.Func -- Error is Vis_Var : Boolean := False; function Vis_Func return Boolean; package Vis_Nested is Var : Boolean := True; function Func return Boolean; end Vis_Nested; private Priv_Var : Boolean := False; function Priv_Func return Boolean; package Priv_Nested is Var : Boolean := True; function Func return Boolean; end Priv_Nested; end Init_Cond; ---------------------------- -- Compilation and output -- ---------------------------- $ gcc -c init_cond.ads init_cond.ads:8:16: "Priv_Var" is undefined init_cond.ads:9:16: "Priv_Func" is undefined init_cond.ads:10:16: "Priv_Nested" is undefined (more references follow) Tested on x86_64-pc-linux-gnu, committed on trunk 2017-10-14 Hristian Kirtchev <kirtc...@adacore.com> * sem_ch3.adb (Analyze_Declarations): Analyze the contract of an enclosing package at the end of the visible declarations. * sem_prag.adb (Analyze_Initialization_Item): Suppress the analysis of an initialization item which is undefined due to some illegality.
Index: sem_ch3.adb =================================================================== --- sem_ch3.adb (revision 253753) +++ sem_ch3.adb (working copy) @@ -2820,25 +2820,11 @@ -- Analyze the contracts of packages and their bodies - if Nkind (Context) = N_Package_Specification then + if Nkind (Context) = N_Package_Specification + and then L = Visible_Declarations (Context) + then + Analyze_Package_Contract (Defining_Entity (Context)); - -- When a package has private declarations, its contract must be - -- analyzed at the end of the said declarations. This way both the - -- analysis and freeze actions are properly synchronized in case - -- of private type use within the contract. - - if L = Private_Declarations (Context) then - Analyze_Package_Contract (Defining_Entity (Context)); - - -- Otherwise the contract is analyzed at the end of the visible - -- declarations. - - elsif L = Visible_Declarations (Context) - and then No (Private_Declarations (Context)) - then - Analyze_Package_Contract (Defining_Entity (Context)); - end if; - elsif Nkind (Context) = N_Package_Body then Analyze_Package_Body_Contract (Defining_Entity (Context)); end if; Index: sem_prag.adb =================================================================== --- sem_prag.adb (revision 253753) +++ sem_prag.adb (working copy) @@ -2818,10 +2818,16 @@ E_Constant, E_Variable) then + -- When the initialization item is undefined, it appears as + -- Any_Id. Do not continue with the analysis of the item. + + if Item_Id = Any_Id then + null; + -- The state or variable must be declared in the visible -- declarations of the package (SPARK RM 7.1.5(7)). - if not Contains (States_And_Objs, Item_Id) then + elsif not Contains (States_And_Objs, Item_Id) then Error_Msg_Name_1 := Chars (Pack_Id); SPARK_Msg_NE ("initialization item & must appear in the visible "