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

one type is more general than the other: note: expected reference &_ found reference &_ (note: the lifetime requirement is introduced here: [arrow points at nothing]) #137258

Open
nabijaczleweli opened this issue Feb 19, 2025 · 4 comments
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

@nabijaczleweli
Copy link
Contributor

nabijaczleweli commented Feb 19, 2025

Code

fn main() {
    let outputs_rgb: &mut [[u8; 12]] = &mut [];
    fn gaming(_: &[u8; 12]) -> &[u8] {
        unimplemented!()
    }
    outputs_rgb.sort_unstable_by_key(gaming);
}

Current output

error[E0308]: mismatched types
    --> test.rs:6:5
     |
6    |     outputs_rgb.sort_unstable_by_key(gaming);
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
     |
     = note: expected reference `&_`
                found reference `&_`
note: the lifetime requirement is introduced here
    --> D:\Users\nabijaczleweli\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib/rustlib/src/rust\library\core\src\slice\mod.rs:3029:25
     |
3029 |         F: FnMut(&T) -> K,
     |                         ^

error: aborting due to 1 previous error

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

Desired output

idk. literally anything else i think lol

Rust Version

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: x86_64-pc-windows-gnu
release: 1.84.1
LLVM version: 19.1.5
@nabijaczleweli nabijaczleweli 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 Feb 19, 2025
@nabijaczleweli
Copy link
Contributor Author

On rustc 1.64.0-nightly (46b8c23f3 2022-07-01) I get

error[E0308]: mismatched types
 --> test.rs:6:5
  |
6 |     outputs_rgb.sort_unstable_by_key(gaming);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected trait `for<'r> <for<'r> fn(&'r [u8; 12]) -> &'r [u8] {gaming} as FnOnce<(&'r [u8; 12],)>>`
             found trait `for<'r> <for<'r> fn(&'r [u8; 12]) -> &'r [u8] {gaming} as FnOnce<(&'r [u8; 12],)>>`
note: the lifetime requirement is introduced here

which is... not worse?

@nabijaczleweli
Copy link
Contributor Author

And on rustc 1.87.0-nightly (827a0d638 2025-02-18) I get

error[E0308]: mismatched types
 --> test.rs:6:5
  |
6 |     outputs_rgb.sort_unstable_by_key(gaming);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected reference `&_`
             found reference `&_`
note: the lifetime requirement is introduced here
 --> /rustc/827a0d638dabc9a22c56f9c37a557568f86ac76c\library\core\src\slice\mod.rs:3071:25

which is worse (observant readers may note that that path does not exist).

@nabijaczleweli
Copy link
Contributor Author

nabijaczleweli commented Feb 19, 2025

Compare error from equivalent lambda:

outputs_rgb.par_sort_unstable_by_key(|rgb| &rgb[0..3]);
error: lifetime may not live long enough
   --> src\main.rs:287:60
    |
287 |                 outputs_rgb.par_sort_unstable_by_key(|rgb| &rgb[0..3] );
    |                                                       ---- ^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
    |                                                       |  |
    |                                                       |  return type of closure is &'2 [u8]
    |                                                       has type `&'1 [u8; 12]`

(acceptable)

@dagit
Copy link

dagit commented Mar 19, 2025

I found a fairly minimal example to reproduce this:

#![allow(missing_docs)]

struct B {}
struct C<R> {
    r: R,
}

fn make_c(b: &B) -> C<&B> {
    C { r: b }
}

fn apply<P, S>(p: P)
where
    P: Fn(&B) -> S,
{
    p(&B {});
}

fn main() {
    apply(make_c);
}

On 1.85.1 that produces this output:

$ cargo check
    Checking type-error v0.1.0 (/home/dagit/local-data/tmp/type-error)
error[E0308]: mismatched types
  --> src/main.rs:20:5
   |
20 |     apply(make_c);
   |     ^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected struct `C<&B>`
              found struct `C<&B>`
note: the lifetime requirement is introduced here
  --> src/main.rs:14:18
   |
14 |     P: Fn(&B) -> S,
   |                  ^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `type-error` (bin "type-error") due to 1 previous error

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

No branches or pull requests

2 participants