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

Unhelpful diagnostic for sort_by_key lifetime issue #92039

Open
tornewuff opened this issue Dec 17, 2021 · 2 comments
Open

Unhelpful diagnostic for sort_by_key lifetime issue #92039

tornewuff opened this issue Dec 17, 2021 · 2 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) 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. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tornewuff
Copy link

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=132fc68312d40c6ed0d878b1d5bdb711

fn parse_filename(file: &std::fs::DirEntry) -> &str {
    todo!()
}

fn foo(mut files: Vec<std::fs::DirEntry>) {
    files.sort_by_key(parse_filename);
}

The current output is:

error[E0308]: mismatched types
 --> src/lib.rs:6:11
  |
6 |     files.sort_by_key(parse_filename);
  |           ^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected associated type `<for<'r> fn(&'r DirEntry) -> &'r str {parse_filename} as FnOnce<(&DirEntry,)>>::Output`
             found associated type `<for<'r> fn(&'r DirEntry) -> &'r str {parse_filename} as FnOnce<(&DirEntry,)>>::Output`

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

Ideally the output should explain the lifetime issue somehow rather than saying "one type is more general than the other" for two identical types. I understand that the reason why this code doesn't work is as described in https://stackoverflow.com/questions/47121985/why-cant-i-use-a-key-function-that-returns-a-reference-when-sorting-a-vector-wi but this error output wasn't informative :)

@tornewuff tornewuff 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 Dec 17, 2021
@BGR360
Copy link
Contributor

BGR360 commented Dec 21, 2021

@rustbot label +D-confusing +D-terse +A-lifetimes +A-associated-items

@rustbot rustbot added A-associated-items Area: Associated items (types, constants & functions) A-lifetimes Area: Lifetimes / regions D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Dec 21, 2021
@panstromek
Copy link
Contributor

I get a different error but similarly unhelpful


error[E0308]: mismatched types
   --> crates\rust-common\src\label.rs:703:9
    |
703 |         pattern.sort_by_key(sort_key);
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
    |
    = note: expected tuple `(_, _, &Node)`
               found tuple `(_, _, &Node)`
note: the lifetime requirement is introduced here
   --> C:\Users\panstromek\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\alloc\src\slice.rs:307:25
    |
307 |         F: FnMut(&T) -> K,
    |                         ^

code

        fn sort_key(pat: &(Node, Point)) -> (i64, i64, &Node) {
            todo!()
        }
        pattern.sort_by_key(sort_key);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) 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. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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

4 participants