comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: A Gnother Gnasty bug
Date: Sat, 07 Apr 2012 13:09:11 +0200
Date: 2012-04-07T13:09:11+02:00	[thread overview]
Message-ID: <87mx6nvlwo.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 22193583.1528.1333759470339.JavaMail.geo-discussion-forums@vbdn7

sbelmont700@gmail.com writes:
>  new I'class'(F(arg=> 42)))));

I don't think that's legal.  You can have a class-wide access value but
not an allocator for a class-wide type because the allocator doesn't
know how much memory to allocate:

> 4.8(4): An initialized allocator is an allocator with a
> qualified_expression. [...]

Your allocator is initialized because it has a qualified_expression.

> 4.8(7/2): For the evaluation of an initialized allocator, the
> evaluation of the qualified_expression is performed first. An object
> of the designated type is created and the value of the
> qualified_expression is converted to the designated subtype and
> assigned to the object.

Now the allocator must create an object of the type designated by the
qualified_expression.

> 3.3(23): [...] A class-wide subtype is defined to have unknown
> discriminants, and is therefore an indefinite subtype. An indefinite
> subtype does not by itself provide enough information to create an
> object; an additional constraint or explicit initialization expression
> is necessary (see *note 3.3.1).

The type designatd by your qualified_expression is class-wide, it is
therefore impossible for the allocator to create the object.

Therefore I think the compiler should have rejected your program.  The
fact that the value in the qualified_expression is the result of a
function call fools the compiler.  Here is a smaller example to
demonstrate:

with Ada.Finalization;
procedure T is

   type B is new Ada.Finalization.Controlled with record
      D : Integer;
   end record;

   type Access_B is access B'Class;


   function F return B'Class is
   begin
      return B'(Ada.Finalization.Controlled with D => 42);
   end F;

   J : Access_B := new B'Class'(Ada.Finalization.Controlled with D => 42); -- Line 16
   K : Access_B := new B'Class'(F); -- Line 17: Not diagnosed

begin
   null;
end T;

gnatmake -gnatwa t
gcc-4.4 -c -gnatwa t.adb
t.adb:16:32: expected type "B'Class" defined at line 4
t.adb:16:32: found a composite type
gnatmake: "t.adb" compilation error

gnatmake -gnatwa t
gcc-4.6 -c -gnatwa t.adb
t.adb:16:32: expected type "B'Class" defined at line 4
t.adb:16:32: found a composite type

So, the compiler correctly rejects line 16 but not line 17, even though
both lines do exactly the same illegal thing.

The correct way to call the allocator is, of course:

   J : Access_B := new B'(Ada.Finalization.Controlled with D => 42);
   K : Access_B := new B'(F);

HTH

-- 
Ludovic Brenta.



  reply	other threads:[~2012-04-07 11:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-07  0:44 A Gnother Gnasty bug sbelmont700
2012-04-07 11:09 ` Ludovic Brenta [this message]
2012-04-07 12:48   ` sbelmont700
2012-04-07 13:55   ` Robert A Duff
2012-04-09 21:09     ` Adam Beneschan
2012-04-09 21:39       ` Robert A Duff
2012-04-07 11:54 ` Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox