3

GNAT allows the following code due to Random(Generator, First, Last) being implemented in the runtime, but it's not part of Ada 2012. Can I cause this to generate a compile error since it shouldn't be available?

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;

procedure Main is
   package Positive_Random is new Ada.Numerics.Discrete_Random
     (Result_Subtype => Positive);
   Generator : Positive_Random.Generator;

   -- This should fail, since function isn't part of Ada 2012.
   Value : Positive := Positive_Random.Random (Generator, 1, 10);
begin
    Put_Line (Value'Image);
end Main;

This is my gpr file:

project Default is

   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Main use ("main.adb");

   package Compiler is
      for Switches ("ada") use ("-gnat12");
   end Compiler;

end Default;
pyj
  • 1,489
  • 11
  • 19
  • When I'm trying to compile the example, it nicely fails to compile with information that Random has too many values. Which means: works as expected. :) Are you sure you don't add somewhere `-gnatX` switch? Like an environment variable, etc. – thindil Aug 01 '21 at 05:57
  • That's the error I get on some systems in Alire-Index's build checks but I'm not seeing that locall.y – pyj Aug 01 '21 at 12:43
  • 1
    My compiler doesn't even have that version of `Random` in the runtime (package gnat-9 in Ubuntu 20.04, so FSF GNAT I guess). I know that, taking a look at the specification for Ada.Numerics.Discrete_Random (`a-nudira.ads`). The extensions provided by Ada 2005 or 2012 are marked with `pragma Ada_05` and `pragma Ada_2012` respectively and those indications are used by the compiler to raise errors or not depending on the requested version. I guess yours is GNAT Community Edition, which doesn't appear to support different language versions (see https://www.adacore.com/gnatpro/comparison) – Gneuromante Aug 01 '21 at 15:20

1 Answers1

2

In my point of view, the standard way to do this is to add a global restriction:

pragma Restrictions (No_Implementation_Identifiers);

No_Implementation_Identifiers

There are no usage names that denote declarations with implementation-defined identifiers that occur within language-defined packages or instances of language-defined generic packages.

But this doesn't work in GNAT Community Edition 2021 (nor in GCC 11, I guess).

You can create a custom GNAT run-time and delete this subprogram or mark it with aspect Implementation_Defined to make the restriction work.

Maxim Reznik
  • 1,216
  • 9
  • 12