rust/tests/ui/contracts/disallow-contract-annotation-on-non-fn.rs
Celina G. Val 804cce47d9 Refactor contract builtin macro + error handling
Instead of parsing the different components of a function signature,
eagerly look for either the `where` keyword or the function body.

- Also address feedback to use `From` instead of `TryFrom` in cranelift
  contract and ubcheck codegen.
2025-02-03 13:55:15 -08:00

53 lines
1.5 KiB
Rust

//! Checks for compilation errors related to adding contracts to non-function items.
#![feature(rustc_contracts)]
#![allow(dead_code)]
#[core::contracts::requires(true)]
//~^ ERROR contract annotations can only be used on functions
struct Dummy(usize);
#[core::contracts::ensures(|v| v == 100)]
//~^ ERROR contract annotations can only be used on functions
const MAX_VAL: usize = 100;
// FIXME: Improve the error message here. The macro thinks this is a function.
#[core::contracts::ensures(|v| v == 100)]
//~^ ERROR contract annotations is only supported in functions with bodies
type NewDummy = fn(usize) -> Dummy;
#[core::contracts::ensures(|v| v == 100)]
//~^ ERROR contract annotations is only supported in functions with bodies
const NEW_DUMMY_FN : fn(usize) -> Dummy = || { Dummy(0) };
#[core::contracts::requires(true)]
//~^ ERROR contract annotations can only be used on functions
impl Dummy {
// This should work
#[core::contracts::ensures(|ret| ret.0 == v)]
fn new(v: usize) -> Dummy {
Dummy(v)
}
}
#[core::contracts::ensures(|dummy| dummy.0 > 0)]
//~^ ERROR contract annotations can only be used on functions
impl From<usize> for Dummy {
// This should work.
#[core::contracts::ensures(|ret| ret.0 == v)]
fn from(value: usize) -> Self {
Dummy::new(value)
}
}
/// You should not be able to annotate a trait either.
#[core::contracts::requires(true)]
//~^ ERROR contract annotations can only be used on functions
pub trait DummyBuilder {
fn build() -> Dummy;
}
fn main() {
}