This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug ada/34504] New: Legal program runs incorrectly, dynamic dispatching with interfaces, RM 3.9.2(5/2, 20/2), 3.9.4(1/2)
- From: "ludovic at ludovic-brenta dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 17 Dec 2007 09:19:05 -0000
- Subject: [Bug ada/34504] New: Legal program runs incorrectly, dynamic dispatching with interfaces, RM 3.9.2(5/2, 20/2), 3.9.4(1/2)
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
-- Newsgroups: comp.lang.ada
-- From: "Peter C. Chapin" <pcha...@sover.net>
-- Date: Thu, 13 Dec 2007 07:20:46 -0500
-- Subject: Q about finalization and interfaces.
--
-- I'm trying to understand the interaction between controlled types,
-- interfaces, and class-wide dispatching. Accordingly I wrote the
-- following program, which I will present in sections. First the package
-- specification:
with Ada.Finalization;
package Check_Package is
type B is interface;
procedure Do_Stuff( Thing : in B ) is abstract;
type D is new Ada.Finalization.Controlled and B with
record
X : Integer := 0;
end record;
overriding procedure Do_Stuff( Thing : in D );
overriding procedure Finalize( Thing : in out D );
end Check_Package;
-- The idea is that I want to build a derivation class rooted on the B
-- interface with some of the types in that class (such as D) being
-- controlled. The corresponding body prints out a few messages to help
-- track what is happening. Note that I use the X component as a kind of
-- object ID number.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
package body Check_Package is
procedure Do_Stuff( Thing : in D ) is
begin
Put("Do_Stuff( Thing : in D ) => ");
Put(Thing.X);
New_Line;
end Do_Stuff;
procedure Finalize( Thing : in out D ) is
begin
Put("Finalize( Thing : in out D ) => ");
Put(Thing.X);
New_Line;
end Finalize;
end Check_Package;
-- Now the test program that exercises the above code looks like this:
with Check_Package; use Check_Package;
procedure Check is
Object : D;
Some_Thing : B'Class := Object;
begin
Object.X := -1;
Do_Stuff(Some_Thing);
end Check;
-- Using GNAT GPL 2007 I get the following output:
--
-- Finalize( Thing : in out D ) => 0
-- Finalize( Thing : in out D ) => -1
--
-- My interpretation is that Object is copied (with the default ID value
-- of zero) when Some_Thing is initialized; Object's ID is then modified.
-- Both Object and its copy are finalized when the program ends. But...
-- what happened to my call to Do_Stuff?? I expected that call to dispatch
-- on Some_Thing and invoke the Do_Stuff for type D. What am I
-- misunderstanding?
--
-- Thanks in advance!
--
-- Peter
--
Summary: Legal program runs incorrectly, dynamic dispatching with
interfaces, RM 3.9.2(5/2, 20/2), 3.9.4(1/2)
Product: gcc
Version: 4.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: ada
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: ludovic at ludovic-brenta dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34504