comp.lang.ada
 help / color / mirror / Atom feed
From: "Peter C. Chapin" <pcc482719@gmail.com>
Subject: Question about SPARK flow error.
Date: Sat, 19 Jun 2010 12:58:41 -0400
Date: 2010-06-19T12:58:41-04:00	[thread overview]
Message-ID: <4c1cf6a3$0$30803$4d3efbfe@news.sover.net> (raw)

My example isn't complete enough to compile, but I could work up a complete
example if necessary. Hopefully this will be accurate and complete enough...

Consider the following procedure:

procedure Add_Item
   (Item : in Some_Type; Index : out Index_Type) is

   Found : Boolean;
begin
   Found := False;

   -- Let's see if the Item already exists in the storage.
   for I in Index_Type range Index_Type'First .. Size loop
      if Storage(I) = Item then
         Index := I;     -- It does. Return the current position.
         Found := True;
         exit;
      end if;
   end loop;

   if not Found then
      -- The Item does not already exist. Install it in the next slot.
      Storage(Next_Index) := Item;
      Index := Next_Index;    -- Return where we installed it.

      -- Play around with Size and Next_Index to set them up for next time
      -- (not shown).
   end if;
end Add_Item;

SPARK gives me an error, "Flow Error 602 - The undefined initial value of
Index may be used in the derivation of Index." I understand this to mean that
there is a path through the procedure where Index (being an out parameter) is
never given a value. So SPARK might be thinking, "Okay... the loop might run
to completion without ever setting Index inside the inner conditional
statement. Then it might skip over the next conditional statement and thus
never set Index at all."

However, if I'm not mistaken that can't actually happen. If the conditional
statement inside the loop never runs, Found will still be False and the
second conditional will definitely execute. Index will receive a value either
inside the loop or later.

Am I missing something or is this reasoning outside of the abilities of
SPARK's flow analysis? Is there some other idiomatic way I should be handling
this sort of situation?

Thanks

Peter




             reply	other threads:[~2010-06-19 16:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-19 16:58 Peter C. Chapin [this message]
2010-06-19 18:03 ` Question about SPARK flow error Phil Thornley
2010-06-19 18:23   ` Gavino
2010-06-19 19:24     ` Phil Thornley
2010-06-19 18:14 ` Gavino
2010-06-19 19:29   ` Phil Thornley
2010-06-19 23:28 ` Phil Thornley
2010-06-20  0:37   ` Peter C. Chapin
2010-06-20  8:42     ` Phil Thornley
replies disabled

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