Skip to content

Rustc could be pointing out that the only thing preventing a function from being FnOnce is that it is unsafe #90073

Closed
@mcy

Description

@mcy

Given the following code: https://godbolt.org/z/zK79rccxf

use std::mem::MaybeUninit;
use std::pin::Pin;

unsafe fn f(x: Pin<&mut MaybeUninit<i32>>) {
    x.map_unchecked_mut(MaybeUninit::assume_init_mut);
}

The current output is:

error[E0277]: expected a `FnOnce<(&mut MaybeUninit<i32>,)>` closure, found `for<'r> unsafe fn(&'r mut MaybeUninit<_>) -> &'r mut _ {MaybeUninit::<_>::assume_init_mut}`
 --> <source>:5:25
  |
5 |     x.map_unchecked_mut(MaybeUninit::assume_init_mut);
  |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnOnce<(&mut MaybeUninit<i32>,)>` closure, found `for<'r> unsafe fn(&'r mut MaybeUninit<_>) -> &'r mut _ {MaybeUninit::<_>::assume_init_mut}`
  |
  = help: the trait `for<'r> FnOnce<(&'r mut MaybeUninit<i32>,)>` is not implemented for `for<'r> unsafe fn(&'r mut MaybeUninit<_>) -> &'r mut _ {MaybeUninit::<_>::assume_init_mut}`

error: aborting due to previous error

This error is a bit difficult, since it does not give the user the essential information: assume_init_mut is unsafe, and therefore cannot be turned into a Fn* trait. Ideally, the compiler would point this out, and suggest a rewrite into a closure like |x| x.assume_init_mut() (plus or minus an unsafe {} block as context requires).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions