comp.lang.ada
 help / color / mirror / Atom feed
* Adjust primitive not called on defaulted nonlimited controlled parameter, bug or feature ?
@ 2022-08-17 20:11 David SAUVAGE
  2022-08-17 22:49 ` Jere
  2022-08-18  0:11 ` Jeffrey R.Carter
  0 siblings, 2 replies; 5+ messages in thread
From: David SAUVAGE @ 2022-08-17 20:11 UTC (permalink / raw)


In the code extract below [2] Adjust primitive is not called on defaulted nonlimited controlled parameter Set.
A reproducer is available on gitlab [1]

Seems like a bug, any feedbacks ?

[1] 
reproducer
https://gitlab.com/adalabs/reproducers/-/tree/main/adjust-not-called-on-defaulted-nonlimited-controlled-parameter

[2]
     1	with Ada.Exceptions,
     2	     Ada.Text_IO;
     3	
     4	with GNAT.OS_Lib;
     5	
     6	procedure Reproducer.Main is
     7	
     8	   --
     9	   --  snippet of reproducer.ads
    10	   --  ...
    11	   --     type Translate_Set is private;
    12	   --     Null_Set : constant Translate_Set;
    13	   --  private
    14	   --     type Translate_Set is new Ada.Finalization.Controlled with record
    15	   --        Ref_Count : Integer_Access;
    16	   --        Set       : Boolean_Access;
    17	   --     end record;
    18	   --     Null_Set : constant Translate_Set := (Ada.Finalization.Controlled with null, null);
    19	   --   ...
    20	   --
    21	
    22	   procedure Process (Set : Translate_Set := Null_Set)
    23	   is
    24	      Content : constant String := Parse (Filename          => "Process",
    25	                                          Translations      => Set);
    26	   begin
    27	      Ada.Text_IO.Put_Line (Content);
    28	   end Process;
    29	begin
    30	   Process;
    31	   --  Ok, Initialize (Set) is not called because default value Null_Set is specified to Set (7.6 10/2).
    32	   --  However Adjust (Set) is not called (7.6 17.8/3).
    33	   --  Is it a feature or a bug ?
    34	
    35	exception
    36	   when E : others =>
    37	      Ada.Text_IO.Put_Line ("(FF) Adjust was not called on the nonlimited controlled object Set, when parameter defaulted to Null_Set");
    38	      Ada.Text_IO.Put_Line ("(FF) " & Ada.Exceptions.Exception_Information (E));
    39	      GNAT.OS_Lib.OS_Exit (255);
    40	end Reproducer.Main;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Adjust primitive not called on defaulted nonlimited controlled parameter, bug or feature ?
  2022-08-17 20:11 Adjust primitive not called on defaulted nonlimited controlled parameter, bug or feature ? David SAUVAGE
@ 2022-08-17 22:49 ` Jere
  2022-08-18  0:11 ` Jeffrey R.Carter
  1 sibling, 0 replies; 5+ messages in thread
From: Jere @ 2022-08-17 22:49 UTC (permalink / raw)


On Wednesday, August 17, 2022 at 4:11:46 PM UTC-4, david....@adalabs.com wrote:
> In the code extract below [2] Adjust primitive is not called on defaulted nonlimited controlled parameter Set. 
> A reproducer is available on gitlab [1] 
> 
> Seems like a bug, any feedbacks ? 
> 
> [1] 
> reproducer 
> https://gitlab.com/adalabs/reproducers/-/tree/main/adjust-not-called-on-defaulted-nonlimited-controlled-parameter 
> 
> [2] 
> 1 with Ada.Exceptions, 
> 2 Ada.Text_IO; 
> 3 
> 4 with GNAT.OS_Lib; 
> 5 
> 6 procedure Reproducer.Main is 
> 7 
> 8 -- 
> 9 -- snippet of reproducer.ads 
> 10 -- ... 
> 11 -- type Translate_Set is private; 
> 12 -- Null_Set : constant Translate_Set; 
> 13 -- private 
> 14 -- type Translate_Set is new Ada.Finalization.Controlled with record 
> 15 -- Ref_Count : Integer_Access; 
> 16 -- Set : Boolean_Access; 
> 17 -- end record; 
> 18 -- Null_Set : constant Translate_Set := (Ada.Finalization.Controlled with null, null); 
> 19 -- ... 
> 20 -- 
> 21 
> 22 procedure Process (Set : Translate_Set := Null_Set) 
> 23 is 
> 24 Content : constant String := Parse (Filename => "Process", 
> 25 Translations => Set); 
> 26 begin 
> 27 Ada.Text_IO.Put_Line (Content); 
> 28 end Process; 
> 29 begin 
> 30 Process; 
> 31 -- Ok, Initialize (Set) is not called because default value Null_Set is specified to Set (7.6 10/2). 
> 32 -- However Adjust (Set) is not called (7.6 17.8/3). 
> 33 -- Is it a feature or a bug ? 
> 34 
> 35 exception 
> 36 when E : others => 
> 37 Ada.Text_IO.Put_Line ("(FF) Adjust was not called on the nonlimited controlled object Set, when parameter defaulted to Null_Set"); 
> 38 Ada.Text_IO.Put_Line ("(FF) " & Ada.Exceptions.Exception_Information (E)); 
> 39 GNAT.OS_Lib.OS_Exit (255); 
> 40 end Reproducer.Main;

Since Translate_Set is a "by-reference type" (see section 6.2 of the RM), there isn't an 
assignment actually made is my guess.  The default parameter notation looks like assignment, but
I would hazard a guess that it doesn't mean an actual assignment is required.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Adjust primitive not called on defaulted nonlimited controlled parameter, bug or feature ?
  2022-08-17 20:11 Adjust primitive not called on defaulted nonlimited controlled parameter, bug or feature ? David SAUVAGE
  2022-08-17 22:49 ` Jere
@ 2022-08-18  0:11 ` Jeffrey R.Carter
  2022-08-18  1:23   ` Randy Brukardt
  1 sibling, 1 reply; 5+ messages in thread
From: Jeffrey R.Carter @ 2022-08-18  0:11 UTC (permalink / raw)


On 2022-08-17 22:11, David SAUVAGE wrote:
> In the code extract below [2] Adjust primitive is not called on defaulted nonlimited controlled parameter Set.
> A reproducer is available on gitlab [1]
> 
> Seems like a bug, any feedbacks ?

Adjust is called on assignment. Your code does no assignments.

-- 
Jeff Carter
"The time has come to act, and act fast. I'm leaving."
Blazing Saddles
36

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Adjust primitive not called on defaulted nonlimited controlled parameter, bug or feature ?
  2022-08-18  0:11 ` Jeffrey R.Carter
@ 2022-08-18  1:23   ` Randy Brukardt
  2022-08-18  4:04     ` David SAUVAGE
  0 siblings, 1 reply; 5+ messages in thread
From: Randy Brukardt @ 2022-08-18  1:23 UTC (permalink / raw)


"Jeffrey R.Carter" <spam.jrcarter.not@spam.acm.org.not> wrote in message 
news:tdk03o$kljt$1@dont-email.me...
> On 2022-08-17 22:11, David SAUVAGE wrote:
>> In the code extract below [2] Adjust primitive is not called on defaulted 
>> nonlimited controlled parameter Set.
>> A reproducer is available on gitlab [1]
>>
>> Seems like a bug, any feedbacks ?
>
> Adjust is called on assignment. Your code does no assignments.

Right. A default parameter that is an object (as in this case) is simply 
passed to the subprogram, so there is no assignment when that happens, and 
thus no Adjust. If the default had been an aggregate or some more complex 
expression, then there would have been a new, temporary object. But new 
objects can be built-in-place, and if so, there is no assignment and no 
adjust (of the whole object, individual components are assigned). 
Build-in-place is required in some contexts, and since compilers have to 
implement it for those contexts, and it is cheaper in most contexts, it is 
likely to be used on almost all new objects (especially temporaries).

It is tricky to determine precisely when non-limited controlled objects are 
Adjusted; the main rule of thumb is that any object that is Initialized or 
Adjusted will be Finalized, but one should not count on any particular 
number or order of Adjusts or Initializes.

Anyway, the point is that one should never expect an Adjust to be associated 
with parameter passing; it could be in a few cases, but even then it could 
be omitted with different compiler settings (or a different compiler).

                    Randy.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Adjust primitive not called on defaulted nonlimited controlled parameter, bug or feature ?
  2022-08-18  1:23   ` Randy Brukardt
@ 2022-08-18  4:04     ` David SAUVAGE
  0 siblings, 0 replies; 5+ messages in thread
From: David SAUVAGE @ 2022-08-18  4:04 UTC (permalink / raw)



> Adjust is called on assignment. Your code does no assignments.
> Right. A default parameter that is an object (as in this case) is simply 
> passed to the subprogram, so there is no assignment when that happens, and 
> thus no Adjust. 

Thanks Jere, Jeffrey and Randy.

reproducer updated accordingly
https://gitlab.com/adalabs/reproducers/-/raw/main/adjust-not-called-on-defaulted-nonlimited-controlled-parameter/sources/reproducer-main.adb

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-08-18  4:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-17 20:11 Adjust primitive not called on defaulted nonlimited controlled parameter, bug or feature ? David SAUVAGE
2022-08-17 22:49 ` Jere
2022-08-18  0:11 ` Jeffrey R.Carter
2022-08-18  1:23   ` Randy Brukardt
2022-08-18  4:04     ` David SAUVAGE

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