This is an old regression present on all active branches for a quite unusual
setup where a generic package is declared and instantiated in a subunit, and
which comes from a thinko in the Has_Body.Find_Body routine of Sem_Elab.
Tested on x86-64/Linux, applied on all the active branches.
2026-03-23 Eric Botcazou <[email protected]>
PR ada/124607
* sem_elab.adb (Has_Body.Find_Body): Fix thinko.
2026-03-23 Eric Botcazou <[email protected]>
* gnat.dg/generic_inst20.ads, gnat.dg/generic_inst20.adb: New test.
* gnat.dg/generic_inst20-sub.adb: New helper.
--
Eric Botcazoudiff --git a/gcc/ada/sem_elab.adb b/gcc/ada/sem_elab.adb
index daabcd6a2ee..5e3823c3649 100644
--- a/gcc/ada/sem_elab.adb
+++ b/gcc/ada/sem_elab.adb
@@ -9890,7 +9890,7 @@ package body Sem_Elab is
-- The corresponding subunit was previously loaded
if Present (Lib_Unit) then
- return Lib_Unit;
+ return Proper_Body (Unit (Lib_Unit));
-- Otherwise attempt to load the corresponding subunit
package Generic_Inst20 is
pragma Elaborate_Body;
package Sub is
function F return Boolean;
end Sub;
end Generic_Inst20;
-- { dg-do compile }
package body Generic_Inst20 is
package body Sub is separate;
end Generic_Inst20;
-- { dg-excess-errors "cannot generate code" }
separate (Generic_Inst20)
package body Sub is
function F return Boolean is
begin
return True;
end F;
generic
package G is
end G;
package I is new G;
end Sub;