Skip to content

Commit

Permalink
Fix PR ada/113893
Browse files Browse the repository at this point in the history
The finalization of objects dynamically allocated through an anonymous access
type is deferred to the enclosing library unit in the current implementation
and a warning is given on each of them.

However this cannot be done if the designated type is local, because this
would generate dangling references to the local finalization routine, so
the finalization needs to be dropped in this case and the warning adjusted.

Tested on x86-64/Linux, applied on all active branches.

2024-02-26  Eric Botcazou  <ebotcazou@adacore.com>

	PR ada/113893
	* exp_ch7.adb (Build_Anonymous_Master): Do not build the master
	for a local designated type.
	* exp_util.adb (Build_Allocate_Deallocate_Proc): Force Needs_Fin
	to false if no finalization master is attached to an access type
	and assert that it is anonymous in this case.
	* sem_res.adb (Resolve_Allocator): Mention that the object might
	not be finalized at all in the warning given when the type is an
	anonymous access-to-controlled type.

2024-02-26  Eric Botcazou  <ebotcazou@adacore.com>

	* gnat.dg/access10.adb: New test.
  • Loading branch information
Eric Botcazou authored and ouuleilei-bot committed Feb 26, 2024
1 parent c2d62cd commit b943853
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
13 changes: 13 additions & 0 deletions gcc/ada/exp_ch7.adb
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ package body Exp_Ch7 is
Desig_Typ : Entity_Id;
FM_Id : Entity_Id;
Priv_View : Entity_Id;
Scop : Entity_Id;
Unit_Decl : Node_Id;
Unit_Id : Entity_Id;

Expand Down Expand Up @@ -789,6 +790,18 @@ package body Exp_Ch7 is
Desig_Typ := Priv_View;
end if;

-- For a designated type not declared at library level, we cannot create
-- a finalization collection attached to an outer unit since this would
-- generate dangling references to the dynamic scope through access-to-
-- procedure values designating the local Finalize_Address primitive.

Scop := Enclosing_Dynamic_Scope (Desig_Typ);
if Scop /= Standard_Standard
and then Scope_Depth (Scop) > Scope_Depth (Unit_Id)
then
return;
end if;

-- Determine whether the current semantic unit already has an anonymous
-- master which services the designated type.

Expand Down
15 changes: 10 additions & 5 deletions gcc/ada/exp_util.adb
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,16 @@ package body Exp_Util is
Needs_Finalization (Desig_Typ)
and then not No_Heap_Finalization (Ptr_Typ);

-- The allocation/deallocation of a controlled object must be associated
-- with an attachment to/detachment from a finalization master, but the
-- implementation cannot guarantee this property for every anonymous
-- access tyoe, see Build_Anonymous_Collection.

if Needs_Fin and then No (Finalization_Master (Ptr_Typ)) then
pragma Assert (Ekind (Ptr_Typ) = E_Anonymous_Access_Type);
Needs_Fin := False;
end if;

if Needs_Fin then

-- Do nothing if the access type may never allocate / deallocate
Expand All @@ -946,11 +956,6 @@ package body Exp_Util is
return;
end if;

-- The allocation / deallocation of a controlled object must be
-- chained on / detached from a finalization master.

pragma Assert (Present (Finalization_Master (Ptr_Typ)));

-- The only other kind of allocation / deallocation supported by this
-- routine is on / from a subpool.

Expand Down
14 changes: 7 additions & 7 deletions gcc/ada/sem_res.adb
Original file line number Diff line number Diff line change
Expand Up @@ -5735,19 +5735,19 @@ package body Sem_Res is
Set_Is_Dynamic_Coextension (N, False);
Set_Is_Static_Coextension (N, False);

-- Anonymous access-to-controlled objects are not finalized on
-- time because this involves run-time ownership and currently
-- this property is not available. In rare cases the object may
-- not be finalized at all. Warn on potential issues involving
-- anonymous access-to-controlled objects.
-- Objects allocated through anonymous access types are not
-- finalized on time because this involves run-time ownership
-- and currently this property is not available. In rare cases
-- the object might not be finalized at all. Warn on potential
-- issues involving anonymous access-to-controlled types.

if Ekind (Typ) = E_Anonymous_Access_Type
and then Is_Controlled_Active (Desig_T)
then
Error_Msg_N
("??object designated by anonymous access object might "
("??object designated by anonymous access value might "
& "not be finalized until its enclosing library unit "
& "goes out of scope", N);
& "goes out of scope, or not be finalized at all", N);
Error_Msg_N ("\use named access type instead", N);
end if;
end if;
Expand Down

0 comments on commit b943853

Please sign in to comment.