Skip to content

Lifetime suggestion hint interacts weirdly with closures #17811

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

Closed
ftxqxd opened this issue Oct 6, 2014 · 7 comments
Closed

Lifetime suggestion hint interacts weirdly with closures #17811

ftxqxd opened this issue Oct 6, 2014 · 7 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

@ftxqxd
Copy link
Contributor

ftxqxd commented Oct 6, 2014

fn main() {
    let closure = |x: &uint| -> &uint { x };
}

Produces:

<anon>:2:41: 2:42 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
<anon>:2     let closure = |x: &uint| -> &uint { x };
                                                 ^
<anon>:1:1: 3:2 note: consider using an explicit lifetime parameter as shown: fn main<'a>()
<anon>:1 fn main() {
<anon>:2     let closure = |x: &uint| -> &uint { x };
<anon>:3 }
error: aborting due to previous error

This is suggesting to put a lifetime on the main function, which certainly is not going to solve the problem here. The real solution is to give closure an explicit type declaration: let closure: <'a>|&'a uint| -> &'a uint = ...;.

Incidentally, is lifetime elision supposed to work on closures, too? Evidently it does not.

@kmcallister kmcallister added A-diagnostics Area: Messages for errors, warnings, and lints I-wrong labels Oct 6, 2014
@bombless
Copy link
Contributor

fn main() {
    let closure = |x: &u32| -> &u32 { x };
}

Same issue today.
Seems relatively easy to fix, I'll work on it.

@bombless
Copy link
Contributor

bombless commented May 1, 2015

fn give_suggestion(&self, same_regions: &[SameRegions]) {

Given the name give_suggestion here which sounds too general, I think we'd better stop giving advice for this case, but I'm not sure.

In real coding I may try to make a helper function to retrieve the context by reference or even use a macro if I'm in a good mood (which I know might not really be a Rust way). Of course, better to avoid a closure here since lifetimes work better in real context rather than being used as arguments.

@bombless
Copy link
Contributor

bombless commented May 1, 2015

I think it's also a valid option that we refactor the error_reporting.rs here to use proper method names, as this "give_suggestion" is not the only method name that looks misleading here.

@bombless
Copy link
Contributor

bombless commented May 2, 2015

maybe related: #25005

@Manishearth
Copy link
Member

#25005 is probably not related. ast_map's parenting is conceptually broken, but the current rustc code basically relies on this broken code (It uses get_parent to mean "get the parent item or fn")

@steveklabnik
Copy link
Member

Triage: no change.

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

I don't see any errors after changing uint to u32 today; closing.

lnicola pushed a commit to lnicola/rust that referenced this issue Aug 13, 2024
fix: Panic while rendering function type hint with impl trait arg

Fixes rust-lang#17811
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

6 participants