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

Unable to pass hook to panic::set_hook as a variable or without specifying type #84296

Open
wooster0 opened this issue Apr 18, 2021 · 0 comments
Labels
A-lifetimes Area: Lifetimes / regions C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@wooster0
Copy link
Contributor

I tried this code:

use std::panic;

fn main() {
    let new_hook = Box::new(|_| {});
    panic::set_hook(new_hook);
}

I expected it to compile successfully.

Instead, this happened:

error[E0308]: mismatched types
 --> src/main.rs:5:21
  |
5 |     panic::set_hook(new_hook);
  |                     ^^^^^^^^ one type is more general than the other
  |
  = note: expected type `FnOnce<(&PanicInfo<'_>,)>`
             found type `FnOnce<(&PanicInfo<'_>,)>`
note: this closure does not fulfill the lifetime requirements
 --> src/main.rs:4:29
  |
4 |     let new_hook = Box::new(|_| {});
  |                             ^^^^^^

error: aborting due to previous error

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

To learn more, run the command again with --verbose.

If I however not assign the Box to a variable and instead directly pass it to panic::set_hook, it compiles:

use std::panic;

fn main() {
    panic::set_hook(Box::new(|_| {}));
}

I believe it should be allowed to assign it to a variable beforehand.

That, or if you explicitly specify the type, it compiles too:

use std::panic;

type Hook = Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>;

fn main() {
    let new_hook: Hook = Box::new(|_| {});
    panic::set_hook(new_hook);
}

I believe it should be able to infer the type on its own.

Meta

rustc --version --verbose:

rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: x86_64-unknown-linux-gnu
release: 1.51.0
LLVM version: 11.0.1

Note that on beta and nightly the error is different.

@wooster0 wooster0 added the C-bug Category: This is a bug. label Apr 18, 2021
@fmease fmease added A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage-legacy C-bug Category: This is a bug. labels Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants