E8015

E8015 — A safe extern function that returns a value of a type with an invariant, or takes a &mut or a *mut to such a value. C code does not keep the invariant, and a call of a safe function needs no unsafe. So C code could return or change such a value with no mark in the program. Remove safe, so that each call is in an unsafe block.

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