https://gcc.gnu.org/g:4954f55c959aea62a8c7c0282804752a2a107178
commit r17-6588-g4954f55c959aea62a8c7c0282804752a2a107178 Author: Eric Botcazou <[email protected]> Date: Mon Nov 23 10:02:47 2026 +0100 Ada: Fix missing error on illegal use of access-to-subprogram parameter More precisely, illegal use of access parameter with access-to-subprogram type as a named access-to-subprogram value. Such access parameters have an accessibility level "deeper than that of any master" per RM 3.10.2(13.1) and all of them "have this same level" so, in particular, they cannot be reused as named access-to-subprogram values. This is enforced in the case of an assignment statement, but not in other cases. gcc/ada PR ada/115666 * sem_ch5.adb (Analyze_Assignment): In Ada 2005 and later, do not add an implicit conversion when the RHS is of an anonymous access- to-subprogram type here... * sem_res.adb (Resolve): ...but here instead. (Valid_Conversion): Fix reference to RM rule in error message. gcc/testsuite/ * gnat.dg/access13.ads, gnat.dg/access13.adb: New test. Diff: --- gcc/ada/sem_ch5.adb | 10 +++------- gcc/ada/sem_res.adb | 32 ++++++++++++++++++++++++-------- gcc/testsuite/gnat.dg/access13.adb | 22 ++++++++++++++++++++++ gcc/testsuite/gnat.dg/access13.ads | 15 +++++++++++++++ 4 files changed, 64 insertions(+), 15 deletions(-) diff --git a/gcc/ada/sem_ch5.adb b/gcc/ada/sem_ch5.adb index c54ad85cf44b..c60512a4be55 100644 --- a/gcc/ada/sem_ch5.adb +++ b/gcc/ada/sem_ch5.adb @@ -892,15 +892,12 @@ package body Sem_Ch5 is end if; end if; - -- Ada 2005 (AI-385): When the lhs type is an anonymous access type, - -- apply an implicit conversion of the rhs to that type to force - -- appropriate static and run-time accessibility checks. This applies - -- as well to anonymous access-to-subprogram types that are component - -- subtypes or formal parameters. + -- Ada 2005 (AI95-0385): When the LHS is of an anonymous access type, + -- apply an implicit conversion of the RHS to that type in order to + -- force appropriate static and dynamic accessibility checks. if Ada_Version >= Ada_2005 and then Is_Access_Type (T1) then if Is_Local_Anonymous_Access (T1) - or else Ekind (T2) = E_Anonymous_Access_Subprogram_Type -- Handle assignment to an Ada 2012 stand-alone object -- of an anonymous access type. @@ -908,7 +905,6 @@ package body Sem_Ch5 is or else (Ekind (T1) = E_Anonymous_Access_Type and then Nkind (Associated_Node_For_Itype (T1)) = N_Object_Declaration) - then Rewrite (Rhs, Convert_To (T1, Relocate_Node (Rhs))); Analyze_And_Resolve (Rhs, T1); diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb index f927d8791e54..033ee88b8b1f 100644 --- a/gcc/ada/sem_res.adb +++ b/gcc/ada/sem_res.adb @@ -3553,6 +3553,22 @@ package body Sem_Res is Mark_Use_Clauses (Original_Node (N)); end if; + -- Ada 2005 (AI95-0230): Apply an (implicit) conversion to an + -- expression of an anonymous access-to-subprogram type that occurs + -- in the context of a named access-to-subprogram type. This ensures + -- proper legality checking in terms of allowed conversions. + + if Ada_Version >= Ada_2005 + and then Ekind (Ctx_Type) in E_Access_Subprogram_Type + | E_Access_Protected_Subprogram_Type + and then + Ekind (Etype (N)) in E_Anonymous_Access_Subprogram_Type + | E_Anonymous_Access_Protected_Subprogram_Type + then + Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N))); + Analyze_And_Resolve (N, Ctx_Type); + end if; + -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an -- expression of an anonymous access type that occurs in the context -- of a named general access type, except when the expression is that @@ -14415,24 +14431,24 @@ package body Sem_Res is -- branch in the case of a remote access to subprogram type, which -- is internally represented as an E_Record_Type. - and then Is_Access_Type (Opnd_Type) + and then Is_Access_Subprogram_Type (Opnd_Type) then - if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type + if Ekind (Opnd_Type) in E_Anonymous_Access_Subprogram_Type + | E_Anonymous_Access_Protected_Subprogram_Type and then Is_Entity_Name (Operand) and then Ekind (Entity (Operand)) = E_In_Parameter - and then - (Nkind (Parent (N)) /= N_Assignment_Statement - or else not Is_Entity_Name (Name (Parent (N))) - or else not Is_Return_Object (Entity (Name (Parent (N))))) + and then not + (Nkind (Parent (N)) = N_Assignment_Statement + and then Is_Entity_Name (Name (Parent (N))) + and then Is_Return_Object (Entity (Name (Parent (N))))) then Report_Error_N ("illegal attempt to store anonymous access to subprogram", Operand, Report_Errs); Report_Error_N ("\value has deeper accessibility than any master " - & "(RM 3.10.2 (13))", + & "(RM 3.10.2(13.1))", Operand, Report_Errs); - Error_Msg_NE ("\use named access type for& instead of access parameter", Operand, Entity (Operand)); diff --git a/gcc/testsuite/gnat.dg/access13.adb b/gcc/testsuite/gnat.dg/access13.adb new file mode 100644 index 000000000000..7775c2ad981b --- /dev/null +++ b/gcc/testsuite/gnat.dg/access13.adb @@ -0,0 +1,22 @@ +-- { dg-do compile } + +package body Access13 is + + procedure Set_R (A : access procedure (B : Boolean)) is + begin + Global_R := (A => A); -- { dg-error "illegal|deeper|use named" } + end; + + procedure Attach_Local_To_Global is + Cond : Boolean; + + procedure Proc (B : Boolean) is + begin + Cond := B; + end; + + begin + Set_R (Proc'Access); + end; + +end Access13; diff --git a/gcc/testsuite/gnat.dg/access13.ads b/gcc/testsuite/gnat.dg/access13.ads new file mode 100644 index 000000000000..d2b60f2ccd7c --- /dev/null +++ b/gcc/testsuite/gnat.dg/access13.ads @@ -0,0 +1,15 @@ +package Access13 is + + type Acc is access procedure (B : Boolean); + + type Rec is record + A : Acc; + end record; + + Global_R : Rec; + + procedure Set_R (A : access procedure (B : Boolean)); + + procedure Attach_Local_To_Global; + +end Access13;
