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 when infered lifetime of return value doesn't match parameter in closure #102400

Closed
lukas-code opened this issue Sep 28, 2022 · 2 comments
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. 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

@lukas-code
Copy link
Member

Given the following code: Playground

fn find_by_key<T, K>(_get_key: impl FnMut(&T) -> K) {
    
}

fn get_key((key, _) : &(String, u8)) -> &str {
    key
}
    
fn main() {
    find_by_key(get_key);
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
  --> src/main.rs:10:5
   |
10 |     find_by_key(get_key);
   |     ^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected trait `for<'r> <for<'r> fn(&'r (String, u8)) -> &'r str {get_key} as FnOnce<(&'r (String, u8),)>>`
              found trait `for<'r> <for<'r> fn(&'r (String, u8)) -> &'r str {get_key} as FnOnce<(&'r (String, u8),)>>`
note: the lifetime requirement is introduced here
  --> src/main.rs:1:50
   |
1  | fn find_by_key<T, K>(_get_key: impl FnMut(&T) -> K) {
   |                                                  ^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

This diagnostic shows the lifetimes of the function item, but omits the return lifetime on the function trait.
(It also shows the wrong fn trait.)

Ideally the output should only show the lifetimes on the fn traits and suggest restricting the lifetime of the parameter:

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
  --> src/main.rs:10:5
   |
10 |     find_by_key(get_key);
   |     ^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected trait `for<'a, 'r> FnMut(&'r (String, u8)) -> &'a str`
              found trait `for<'r> FnMut(&'r (String, u8)) -> &'r str`
note: the lifetime requirement `'a` is introduced here
  --> src/main.rs:1:50
   |
1  | fn find_by_key<T, K>(_get_key: impl FnMut(&T) -> K) {
   |                                                  ^
help: Try restricting the lifetime of the argument to `'a`:
   |
1  | fn find_by_key<'a, T: 'a, K>(_get_key: impl FnMut(&'a T) -> K) {
   |                ++   ++++                           ++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
@lukas-code lukas-code 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 Sep 28, 2022
@lukas-code
Copy link
Member Author

lukas-code commented Sep 28, 2022

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

@rustbot rustbot added 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 Sep 28, 2022
@lukas-code
Copy link
Member Author

triage: closing this in favor of #112985.

@lukas-code lukas-code closed this as not planned Won't fix, can't repro, duplicate, stale Sep 3, 2024
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. 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

2 participants