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

"Expected mutable reference &mut (), found mutable reference &mut ()" #117995

Open
GrigorenkoPV opened this issue Nov 16, 2023 · 1 comment
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@GrigorenkoPV
Copy link
Contributor

GrigorenkoPV commented Nov 16, 2023

Code

fn foo(t: &mut ()) -> &mut () {
    t
}

fn bar<T, E>(_: impl FnOnce(&mut T) -> E) {}

fn test() {
    bar(foo)
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:8:5
  |
8 |     bar(foo)
  |     ^^^ one type is more general than the other
  |
  = note: expected mutable reference `&mut ()`
             found mutable reference `&mut ()`
note: the lifetime requirement is introduced here
 --> src/lib.rs:5:40
  |
5 | fn bar<T, E>(_: impl FnOnce(&mut T) -> E) {}
  |                                        ^

For more information about this error, try `rustc --explain E0308`.

Desired output

Something that allows to distinguish the lifetimes.

I doubt this is a false compiler error, because the following compiles just fine:

fn foo(t: &mut ()) -> &mut () {
    t
}

fn bar<'a, T: 'a, E: 'a>(_: impl FnOnce(&'a mut T) -> E) {}

fn test() {
    bar(foo)
}

Rationale and extra context

No response

Other cases

The output above is from 1.75.0-beta.1.

This is what 1.62.1 gives me:

error[E0308]: mismatched types
 --> src/lib.rs:8:5
  |
8 |     bar(foo)
  |     ^^^ lifetime mismatch
  |
  = note: expected associated type `<for<'r> fn(&'r mut ()) -> &'r mut () {foo} as FnOnce<(&mut (),)>>::Output`
             found associated type `<for<'r> fn(&'r mut ()) -> &'r mut () {foo} as FnOnce<(&mut (),)>>::Output`
  = note: the required lifetime does not necessarily outlive the empty lifetime
note: the lifetime requirement is introduced here
 --> src/lib.rs:5:40
  |
5 | fn bar<T, E>(_: impl FnOnce(&mut T) -> E) {}
  |                                        ^

For more information about this error, try `rustc --explain E0308`.

Which is even worse.

cargo-bisect-rustc tells me the error message changed at some point during e3dfeea...900c354

Anything else?

Initially I was trying to write a function like RefMut::filter_map, but for Result instead of Option

Came up with this piece of code:

fn try_map<'a, T, U, E, F>(mut r: RefMut<'a, T>, f: F) -> Result<RefMut<'a, U>, E>
where
    T: ?Sized + 'a,
    U: ?Sized + 'a,
    F: FnOnce(&mut T) -> Result<&mut U, E>,
{
    let t: *mut T = r.deref_mut() as *mut T;
    let u: *mut U = f(unsafe { &mut *t })? as *mut U;
    Ok(RefMut::<'a, T>::map(r, |_| unsafe { &mut *u }))
}

& was trying to test that you cannot leak any 'a data in E. (That's where I was greeted with a similar error message saying it found Result<_, &mut ...> where Result<_, &mut ...> was expected.)

Anyways, is this implementation actually sound or am I missing something?

If it's not, is it even possible to do something like this in Rust at all?

If it is okay, I would actually love to try to get this into std. Any tip on what I should start with in that case? An "RFC" thing or a simple PR?

Thanks!

@GrigorenkoPV GrigorenkoPV 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 Nov 16, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 16, 2023
@fmease
Copy link
Member

fmease commented Nov 17, 2023

cc #112985

@fmease fmease added A-lifetimes Area: Lifetimes / regions D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 17, 2023
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 A-lifetimes Area: Lifetimes / regions D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants