防止在 GNAT 中使用 Ada 202x

Prevent Ada 202x Use in GNAT

GNAT 允许以下代码,因为 Random(Generator, First, Last) 是在运行时实现的,但它不是 Ada 2012 的一部分。我是否可以导致它生成编译错误,因为它不应该可用?

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;

这是我的 gpr 文件:

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;

在我看来,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.

但这在 GNAT Community Edition 2021 中不起作用(我猜在 GCC 11 中也不行)。

您可以创建一个自定义 GNAT 运行-time 并删除此子程序或用 aspect Implementation_Defined 标记它以使限制生效。