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

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

Closed
mcy opened this issue Oct 20, 2021 · 0 comments · Fixed by #95663
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mcy
Copy link
Contributor

mcy commented Oct 20, 2021

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).

@mcy mcy added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 20, 2021
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 5, 2022
…re, r=compiler-errors

diagnostics: give a special note for unsafe fn / Fn/FnOnce/FnMut

Fixes rust-lang#90073
@bors bors closed this as completed in bec8dbd Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant