【问题标题】:SPARK instantiation error w.r.t. volatile typeSPARK 实例化错误 w.r.t.挥发性类型
【发布时间】:2020-11-06 18:01:25
【问题描述】:

我有一个大致如下的数据结构(我无法分享完整的来源,但可以根据要求提供更多信息):

generic
    type Item_Type is private;
package Util.Pool is
    type Pool is limited new Ada.Finalization.Limited_Controlled with private;

    procedure Get_Available (From: in out Pool; Available: out Natural);
    overriding procedure Finalize (Object: in out Pool);
private
    type Item_Array is array (Positive range <>) of Item_Type;
    type Item_Array_Access is access all Item_Array;

    Null_Item_Array: constant Item_Array_Access := null;

    protected type Protected_Pool is
        function Get_Available return Natural;
    private
        Available: Natural := 0;
        Items: Item_Array_Access := Null_Item_Array;
    end Protected_Pool;

    type Pool is limited new Ada.Finalization.Limited_Controlled with record
        List: Protected_Pool;
    end record;
end Util.Pool;

完整代码编译时没有错误和警告,但 SPARK 证明步骤失败并显示以下内容:

gnatprove -PX:\Path\To\project.gpr -j0 --mode=flow --ide-progress-bar -u main.adb
Phase 1 of 2: generation of Global contracts ...
main.adb:11:05: instantiation error at util-pool.ads:34
main.adb:11:05: effectively volatile type "Protected_Pool" must be declared at library level (SPARK RM 7.1.3(3))
main.adb:11:05: instantiation error at util-pool.ads:45
main.adb:11:05: component "List" of non-volatile type "Pool" cannot be volatile
gnatprove: error during generation of Global contracts

我已阅读 SPARK 手册的corresponding parts,但我不明白如何根据它们修复我的代码。 TIA。

【问题讨论】:

    标签: data-structures types ada spark-ada


    【解决方案1】:

    看起来好像是在 Main 中实例化泛型。这不是“在图书馆级别”。

    实例化为库级别的包,应该会更好。这需要进入一个文件(在这种情况下)my_util_pool.ads:

    with Util.Pool;
    package My_Util_Pool is new Util.Pool (Integer);
    

    main.adb 现在开始

    with My_Util_Pool;
    with ...;
    procedure Main is
       ...
    

    【讨论】:

    • 当在 Main 过程之外实例化时,编译突然失败并出现 main.adb:5:08: keyword "body" expected here [see file name] main.adb:20:10: missing "end Test_Pool;" ,我做错了什么?
    • 为什么Protected_Pool 类型实际上是易变的?
    • @debater,因为它是:the SPARK reference manual 7.1.2 的第 5 段。至于理由:在 PO 中保存的值可能随时发生变化,不受特定线程的控制(在这里猜测,但似乎很可能)
    • @AretsPaeglis,对不起,我之前没有看到您的评论:很难说,您从未向我们展示过您的 main.adb,无论是原始的还是修改后的。希望你自己修好了。
    猜你喜欢
    • 2019-09-16
    • 2014-03-16
    • 2023-02-10
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多