E8015
E8015 — Asafeextern function that returns a value of a type with an invariant, or takes a&mutor a*mutto such a value. C code does not keep the invariant, and a call of asafefunction needs nounsafe. So C code could return or change such a value with no mark in the program. Removesafe, so that each call is in anunsafeblock.
What the compiler reports
Error[E8015] at {{.*}}safe_extern_functions_that_return_or_change_a_value_of_a_type_with_an_invariant.vx:19:3: the `safe` extern function `mint` returns a value of `Token`, and `Token` has an invariant. C code does not keep the invariant. Remove `safe`, so that each call is in an `unsafe` block
Error[E8015] at {{.*}}safe_extern_functions_that_return_or_change_a_value_of_a_type_with_an_invariant.vx:20:3: the `safe` extern function `mint_option` returns a value of `Option<Token>`, and `Token` has an invariant.
Error[E8015] at {{.*}}safe_extern_functions_that_return_or_change_a_value_of_a_type_with_an_invariant.vx:21:3: the `safe` extern function `bump` takes `t : &mut Token`, so C code can change a value of `Token`, and `Token` has an invariant.
Error[E8015] at {{.*}}safe_extern_functions_that_return_or_change_a_value_of_a_type_with_an_invariant.vx:22:3: the `safe` extern function `bump_through_a_pointer` takes `t : *mut Token`, so C code can change a value of `Token`, and `Token` has an invariant.
The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.
A program that triggers it
//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// `Token` has an invariant, and C code does not keep the invariant. So a `safe` extern function
// cannot return a `Token`, or a value that holds a `Token`, such as an `Option<Token>`. A `safe`
// extern function also cannot take a `&mut Token` or a `*mut Token`, because C code could change
// the `Token`. Without `safe`, each call needs an `unsafe` block. A `&Token` parameter and a
// `*mut Token` result are not errors.
import core::option;
import tests::modules::a_token_with_an_invariant;
extern "C" {
safe fn mint() -> Token;
safe fn mint_option() -> Option<Token>;
safe fn bump(t : &mut Token) -> void;
safe fn bump_through_a_pointer(t : *mut Token) -> void;
safe fn number(t : &Token) -> i64;
safe fn place() -> *mut Token;
fn mint_unchecked() -> Token;
}
fn main() -> i32 {
return 0;
}
From tests/frontend/fail/formal_verification/safe_extern_functions_that_return_or_change_a_value_of_a_type_with_an_invariant.vx, which asserts this diagnostic on every commit.
Related
Contracts and verificationThe chapter covering the rule this code enforces.
All diagnosticsEvery code the compiler can emit, grouped by stage.
E8014Code outside the module of a type with an invariant changes a field of the type. Only the module (the
.vx file) that declares the type can write a field, take a &mut to a field, call a method that takes &mut self on a field, move a field out of the value, or make a value of the type with a struct literal or an mlir! block. Other code calls a function of the module to change a value of the type. Then the invariant holds for each value of the type outside its module.