Search code examples
adaada2012

Prevent Ada 202x Use in GNAT


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;

Solution

  • 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.