Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to annotate functions without body with contracts #3325

Open
celinval opened this issue Jul 4, 2024 · 2 comments
Open

Allow users to annotate functions without body with contracts #3325

celinval opened this issue Jul 4, 2024 · 2 comments
Labels
[C] Feature / Enhancement A new feature request or enhancement to an existing feature. [E] User Experience An UX enhancement for an existing feature. Including deprecation of an existing one. Z-Contracts Issue related to code contracts

Comments

@celinval
Copy link
Contributor

celinval commented Jul 4, 2024

Requested feature: Allow users to annotate external "C" functions and intrinsics with contracts.
Use case: Adding safety contracts to std intrinsics and extern "C" functions.
Link to relevant documentation (Rust reference, Nomicon, RFC):

Test case:

    /// The size of the referenced value in bytes.
    ///
    /// The stabilized version of this intrinsic is [`crate::mem::size_of_val`].
    #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
    #[rustc_nounwind]
    #[requires(matches!(
        <T as Pointee>::Metadata::map_dyn(crate::ptr::metadata(_val)::metadata(), |dyn_meta| {
    ub_checks::can_dereference(dyn_meta)}), None | Some(true)))]
    pub fn size_of_val<T: ?Sized>(_val: *const T) -> usize;

Fails with the following compilation error:

error: expected curly braces
    --> /verify-std/library/core/src/intrinsics.rs:1070:59
     |
1070 |     pub fn size_of_val<T: ?Sized>(_val: *const T) -> usize;

@celinval celinval added the [C] Feature / Enhancement A new feature request or enhancement to an existing feature. label Jul 4, 2024
@celinval celinval added this to the Function Contracts milestone Jul 4, 2024
@celinval celinval added the [E] User Experience An UX enhancement for an existing feature. Including deprecation of an existing one. label Jul 4, 2024
@celinval
Copy link
Contributor Author

celinval commented Jul 4, 2024

In order to do this, I believe we will need to apply the same logic as stubbing external functions where we replace the method calls instead of replacing the function body.

@pnkfelix
Copy link
Contributor

(this is probably obvious from context, but the test case in the description needs to be wrapped in extern "rust-intrinsic" { ... })

@tautschnig tautschnig added the Z-Contracts Issue related to code contracts label Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C] Feature / Enhancement A new feature request or enhancement to an existing feature. [E] User Experience An UX enhancement for an existing feature. Including deprecation of an existing one. Z-Contracts Issue related to code contracts
Projects
None yet
Development

No branches or pull requests

3 participants