comp.lang.ada
 help / color / mirror / Atom feed
* Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
@ 2014-05-19 10:10 mockturtle
  2014-05-19 13:49 ` Simon Wright
  0 siblings, 1 reply; 11+ messages in thread
From: mockturtle @ 2014-05-19 10:10 UTC (permalink / raw)


Dear all,
I am stuck with an error that could be a compiler bug and I hope you could give me some help.  I am currently using GNAT 20140331 on 64 bit Linux.

A brief introduction about what I am trying to do: I want to implement a "symbol table" that maps identifiers (implemented as bounded strings) into "symbol descriptors" (implemented as records with a discriminant that identifies the symbol type).  

I define the "identifier name" type in a package that is with-ed by the package that defines the symbol table.  The symbol table is just a Indefinite_Hash_Map (indefinite since the descriptor is indefinite).   When I try to compile the body of the symbol table package I get in the .ads the error 

    instantiation error at a-cihama.adb:1043
    invalid constraint: type has no discriminant

I was able to replicate the error with a small set of files.  At the end of this message you will find three packages: Ginger (that defines the identifier type), Foo (that with-s Ginger) and Foo_2 (that is Foo merged together with Ginger, so that it is self-sufficient).

If I try to compile foo.adb, I get the error above; if I try to compile foo-2.adb, I get no error.  This makes me suspect that (i) I tripped over some subtlety of the language or (ii) this is a bug.

Any help?

Thank you.

Riccardo

---------

--------------------
--   GINGER.ADS   --
--------------------

with Ada.Strings.Bounded;

package ginger is
   type Name_Type is private;

   function To_String(X:Name_Type) return String;

private
   package Names is
     new Ada.Strings.Bounded.Generic_Bounded_Length (10);

   type Name_Type is new Names.Bounded_String;

   function To_String (X : Name_Type) return String
   is (Names.To_String (Names.Bounded_String (X)));
end ginger;

-----------------
--   FOO.ADS   --
-----------------


with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;
with Ginger;

package foo is
   type My_Map is tagged private;

   type Name_Class is (First, Second);

   type Descriptor (Class : Name_Class) is
      record
         case Class is
            when First =>
               null;

            when Second =>
               X : Float;
         end case;
      end record;

   procedure Zizi (X: My_Map);

private
   function Hash (Key : Ginger.Name_Type) return Ada.Containers.Hash_Type;

   function Equal (Left, Right : Ginger.Name_Type) return Boolean;

   package Maps is
     new Ada.Containers.Indefinite_Hashed_Maps
       (Key_Type        => Ginger.Name_Type,
        Element_Type    => Descriptor,
        Hash            => Hash,
        Equivalent_Keys => Equal);

   type My_Map is  new Maps.Map with null record;
end foo;

-----------------
--   FOO.ADB   --
-----------------

package body foo is

   ----------
   -- Zizi --
   ----------

   procedure Zizi (X: My_Map) is
   begin
      null;
   end Zizi;
   
   function Hash (Key : Ginger.Name_Type) return Ada.Containers.Hash_Type
   is
   begin
      return Ada.Strings.Hash (Ginger.To_String (Key));
   end Hash;

   function Equal (Left, Right : Ginger.Name_Type) return Boolean 
   is
   begin
      
      return Ginger.To_String (Left) = Ginger.To_String (Right);
   end Equal;
        


end foo;

-------------------
--   FOO_2.ADS   --
-------------------


with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;
with Ada.Strings.Bounded;


package Foo_2 is
   type My_Map is tagged private;

   type Name_Class is (First, Second);

   type Descriptor (Class : Name_Class) is
      record
         case Class is
            when First =>
               null;

            when Second =>
               X : Float;
         end case;
      end record;

   procedure Zizi (X: My_Map);

private
   package Names is
     new Ada.Strings.Bounded.Generic_Bounded_Length (10);

   type Name_Type is new Names.Bounded_String;

   function To_String (X : Name_Type) return String
   is (Names.To_String (Names.Bounded_String (X)));

   function Hash (Key : Name_Type) return Ada.Containers.Hash_Type
   is (Ada.Strings.Hash (To_String (Key)));

   function Equal (Left, Right : Name_Type) return Boolean
   is (Left = Right);

   package Maps is
     new Ada.Containers.Indefinite_Hashed_Maps
       (Key_Type        => Name_Type,
        Element_Type    => Descriptor,
        Hash            => Hash,
        Equivalent_Keys => Equal);

   type My_Map is tagged
      record
         M : Maps.Map;
      end record;
end Foo_2;


-------------------
--   FOO_2.ADB   --
-------------------

package body Foo_2 is

   ----------
   -- Zizi --
   ----------

   procedure Zizi (X: My_Map) is
   begin
      null;
   end Zizi;

end Foo_2;


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

* Re: Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-19 10:10 Strange compile-time error with Ada.Containers.Indefinite_Hashed_Maps mockturtle
@ 2014-05-19 13:49 ` Simon Wright
  2014-05-19 16:00   ` mockturtle
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Wright @ 2014-05-19 13:49 UTC (permalink / raw)


mockturtle <framefritti@gmail.com> writes:

> I define the "identifier name" type in a package that is with-ed by
> the package that defines the symbol table.  The symbol table is just a
> Indefinite_Hash_Map (indefinite since the descriptor is indefinite).
> When I try to compile the body of the symbol table package I get in
> the .ads the error
>
>     instantiation error at a-cihama.adb:1043
>     invalid constraint: type has no discriminant

All of GNAT GPL 2013, FSF GCC 4.8.1 and FSF GCC 4.9.0 are happy with
your code.

a-cihama.adb (Ada.Containers.Indefinite_Hashed_Maps) line 1043 is in
Read_Node:

   function Read_Node
     (Stream : not null access Root_Stream_Type'Class) return Node_Access
   is
      Node : Node_Access := new Node_Type;

   begin
      begin
         Node.Key := new Key_Type'(Key_Type'Input (Stream)); <<<<<<

and I don't see why GNAT thinks there (should be?) a discriminant there.

Is there a reason why you don't make Name_Type just String? You're
forced to use indefinite hashed maps, so the stored Key is assumed to be
indefinite and will be allocated. (I tried this and the compiler was
happy).

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

* Re: Strange compile-time error with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-19 13:49 ` Simon Wright
@ 2014-05-19 16:00   ` mockturtle
  2014-05-19 16:19     ` G.B.
                       ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: mockturtle @ 2014-05-19 16:00 UTC (permalink / raw)


On Monday, May 19, 2014 3:49:45 PM UTC+2, Simon Wright wrote:
> mockturtle <framefritti@gmail.com> writes:
> 
> 
> 
> > When I try to compile the body of the symbol table package I get in
> > the .ads the error
> 
> >     instantiation error at a-cihama.adb:1043
> >     invalid constraint: type has no discriminant
> 
> 
> 
> All of GNAT GPL 2013, FSF GCC 4.8.1 and FSF GCC 4.9.0 are happy with
> your code.
> 

Strange...  

I tried my code on my PC at work; I have the same version of GNAT installed also on my PCs at home.  I'll try there.

> 
> 
> a-cihama.adb (Ada.Containers.Indefinite_Hashed_Maps) line 1043 is in
> Read_Node:
> 
> 
>    function Read_Node
>      (Stream : not null access Root_Stream_Type'Class) return Node_Access
>    is
>       Node : Node_Access := new Node_Type;
>    begin
>       begin
>          Node.Key := new Key_Type'(Key_Type'Input (Stream)); <<<<<<
> 
> and I don't see why GNAT thinks there (should be?) a discriminant there.
> 

I do not see it either... Nevertheless, I just c'n'p-ed the error message. 

This makes me think: could it be some problem with my installation?  Maybe some funny interaction with an older version?

[[ 

My setup is a bit peculiar: I have GNAT installed under /usr/gnat which is actually a symbolic link to /usr/gnat-2014-05-15.  When I install a new version of GNAT, I create a new directory under /usr and move the link of /usr/gnat to the new version.  In this way, I can keep the old version(s) (if something goes wrong I can go back to the old setup), while avoiding (I hope) mix-up between versions.  

]] 

> 
> 
> Is there a reason why you don't make Name_Type just String? You're
> forced to use indefinite hashed maps, so the stored Key is assumed to be
> indefinite and will be allocated. (I tried this and the compiler was
> happy).

Yes, there are few reasons for using a bounded string.  Basically, in other parts of the code it is more convenient (but not strictly necessary) to have a definite type for Type_Name.  Currently I solved the problem by making a "home-brew" version of Indefinite_Hashed_Maps by storing the access to the descriptor.   The change were just minimal.  The curiosity, however, remains.

Riccardo


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

* Re: Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-19 16:00   ` mockturtle
@ 2014-05-19 16:19     ` G.B.
  2014-05-19 16:35     ` Simon Wright
  2014-05-19 21:27     ` J-P. Rosen
  2 siblings, 0 replies; 11+ messages in thread
From: G.B. @ 2014-05-19 16:19 UTC (permalink / raw)


On 19.05.14 18:00, mockturtle wrote:
> This makes me think: could it be some problem with my installation?

Same error here (GNAT GPL 2014).

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

* Re: Strange compile-time error with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-19 16:00   ` mockturtle
  2014-05-19 16:19     ` G.B.
@ 2014-05-19 16:35     ` Simon Wright
  2014-05-19 21:27     ` J-P. Rosen
  2 siblings, 0 replies; 11+ messages in thread
From: Simon Wright @ 2014-05-19 16:35 UTC (permalink / raw)


mockturtle <framefritti@gmail.com> writes:

> On Monday, May 19, 2014 3:49:45 PM UTC+2, Simon Wright wrote:
>> mockturtle <framefritti@gmail.com> writes:
>> 
>> 
>> 
>> > When I try to compile the body of the symbol table package I get in
>> > the .ads the error
>> 
>> >     instantiation error at a-cihama.adb:1043
>> >     invalid constraint: type has no discriminant
>> 
>> 
>> 
>> All of GNAT GPL 2013, FSF GCC 4.8.1 and FSF GCC 4.9.0 are happy with
>> your code.
>> 
>
> Strange...  
>
> I tried my code on my PC at work; I have the same version of GNAT
> installed also on my PCs at home.  I'll try there.

I should have said, I get the same error as you do with GNAT GPL 2014 on
Mac OS X.


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

* Re: Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-19 16:00   ` mockturtle
  2014-05-19 16:19     ` G.B.
  2014-05-19 16:35     ` Simon Wright
@ 2014-05-19 21:27     ` J-P. Rosen
  2014-05-20  6:30       ` Simon Wright
  2014-05-20 20:47       ` björn lundin
  2 siblings, 2 replies; 11+ messages in thread
From: J-P. Rosen @ 2014-05-19 21:27 UTC (permalink / raw)


Le 19/05/2014 18:00, mockturtle a écrit :
> My setup is a bit peculiar: I have GNAT installed under /usr/gnat
> which is actually a symbolic link to /usr/gnat-2014-05-15.  When I
> install a new version of GNAT, I create a new directory under /usr
> and move the link of /usr/gnat to the new version.  In this way, I
> can keep the old version(s) (if something goes wrong I can go back to
> the old setup), while avoiding (I hope) mix-up between versions.

Well, I do that all the time (I have many versions of Gnat installed -
all thosed used by my clients, + at least the current and the previous
GPL). Never had any problem, a symbolic link is so convenient.

(of course, managing several versions of Gnat under Windows is a bit
more challenging...)
-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-19 21:27     ` J-P. Rosen
@ 2014-05-20  6:30       ` Simon Wright
  2014-05-20 20:47       ` björn lundin
  1 sibling, 0 replies; 11+ messages in thread
From: Simon Wright @ 2014-05-20  6:30 UTC (permalink / raw)


"J-P. Rosen" <rosen@adalog.fr> writes:

> Le 19/05/2014 18:00, mockturtle a écrit :
>> My setup is a bit peculiar: I have GNAT installed under /usr/gnat
>> which is actually a symbolic link to /usr/gnat-2014-05-15.  When I
>> install a new version of GNAT, I create a new directory under /usr
>> and move the link of /usr/gnat to the new version.  In this way, I
>> can keep the old version(s) (if something goes wrong I can go back to
>> the old setup), while avoiding (I hope) mix-up between versions.
>
> Well, I do that all the time (I have many versions of Gnat installed -
> all thosed used by my clients, + at least the current and the previous
> GPL). Never had any problem, a symbolic link is so convenient.
>
> (of course, managing several versions of Gnat under Windows is a bit
> more challenging...)

I wrote gnatef[1] many years ago when you needed to set several
environment variables to get things to work properly (PATH, of course,
C_INCLUDE_PATH, GCC_EXEC_PREFIX, possibly others). I still use it
(setting GNAT_PREFIX to the location of the version I want to use,
e.g. /opt/gcc-4.9.0) but in fact modern GNATs use the location of
gnatmake (or, I expect, gprbuild) to set things up: so you can either
set PATH or say e.g. /opt/gnat-gpl-2014/bin/gnatmake -P foo. On Unixes,
at any rate.

[1] http://gnuada.sourceforge.net/pmwiki.php/Packages/Gnatfe

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

* Re: Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-19 21:27     ` J-P. Rosen
  2014-05-20  6:30       ` Simon Wright
@ 2014-05-20 20:47       ` björn lundin
  2014-05-21  5:04         ` J-P. Rosen
  1 sibling, 1 reply; 11+ messages in thread
From: björn lundin @ 2014-05-20 20:47 UTC (permalink / raw)


Den måndagen den 19:e maj 2014 kl. 23:27:07 UTC+2 skrev J-P. Rosen:
> (of course, managing several versions of Gnat under Windows > >is a bit more challenging...)

Not really, just change the PATH
But then again I just use gnat as cli tool, so managing
PATHS are simple
--
/Björn

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

* Re: Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-20 20:47       ` björn lundin
@ 2014-05-21  5:04         ` J-P. Rosen
  2014-05-25 18:28           ` björn lundin
  0 siblings, 1 reply; 11+ messages in thread
From: J-P. Rosen @ 2014-05-21  5:04 UTC (permalink / raw)


Le 20/05/2014 22:47, björn lundin a écrit :
> Den måndagen den 19:e maj 2014 kl. 23:27:07 UTC+2 skrev J-P. Rosen:
>> (of course, managing several versions of Gnat under Windows > >is a bit more challenging...)
> 
> Not really, just change the PATH
> But then again I just use gnat as cli tool, so managing
> PATHS are simple

... and change some variables in the registry, and use the special
program that I wrote (available on Adalog's site) to notify all open
windows that some registry variables have changed. Doable, but it took
me some time to figure out...


-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-21  5:04         ` J-P. Rosen
@ 2014-05-25 18:28           ` björn lundin
  2014-05-26  8:53             ` J-P. Rosen
  0 siblings, 1 reply; 11+ messages in thread
From: björn lundin @ 2014-05-25 18:28 UTC (permalink / raw)


Den onsdagen den 21:e maj 2014 kl. 07:04:46 UTC+2 skrev J-P. Rosen:
> > But then again I just use gnat as cli tool, so managing
> > PATHS are simple
> 
> ... and change some variables in the registry, 

? What variables?

> and use the special
> program that I wrote (available on Adalog's site) to notify all open
> windows that some registry variables have changed. Doable, but it took
> me some time to figure out...

Hmm, perhaps open windows need that.
But usually, I just start a new command prompt,
and issue 

set PATH=\path\to\the\gnat\I\like\to\use;%PATH%

and then it works. That is it.
Of course other open windows lives in their own environment
so they do not see the path change.
But a .bat file that lets me pick a gnat version,
and then a suitable set command as above, is pretty simple.
And for cli, at least I find it to be enough.

--
/Björn

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

* Re: Strange compile-time error  with Ada.Containers.Indefinite_Hashed_Maps
  2014-05-25 18:28           ` björn lundin
@ 2014-05-26  8:53             ` J-P. Rosen
  0 siblings, 0 replies; 11+ messages in thread
From: J-P. Rosen @ 2014-05-26  8:53 UTC (permalink / raw)


Le 25/05/2014 20:28, björn lundin a écrit :

>> ... and change some variables in the registry,
> ? What variables?
PATH, and some variables that I used to account for a bug workaround in
older Gnat versions (the latter is obsolete now).

>> and use the special program that I wrote (available on Adalog's
>> site) to notify all open windows that some registry variables have
>> changed. Doable, but it took me some time to figure out...
> 
> Hmm, perhaps open windows need that. But usually, I just start a new
> command prompt, and issue
> 
> set PATH=\path\to\the\gnat\I\like\to\use;%PATH%
> 
Different use case. Here you change your Gnat only for the current
terminal. I want it changed for all future sessions, including when I
start GPS by clicking on a .gpr file.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

end of thread, other threads:[~2014-05-26  8:53 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-19 10:10 Strange compile-time error with Ada.Containers.Indefinite_Hashed_Maps mockturtle
2014-05-19 13:49 ` Simon Wright
2014-05-19 16:00   ` mockturtle
2014-05-19 16:19     ` G.B.
2014-05-19 16:35     ` Simon Wright
2014-05-19 21:27     ` J-P. Rosen
2014-05-20  6:30       ` Simon Wright
2014-05-20 20:47       ` björn lundin
2014-05-21  5:04         ` J-P. Rosen
2014-05-25 18:28           ` björn lundin
2014-05-26  8:53             ` J-P. Rosen

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