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

Add #[context()] for the function which has the return type impl Iterator<Item = xxx>, get error[E0562] #18

Closed
HuijingHei opened this issue Dec 11, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@HuijingHei
Copy link

When using

Please indicate the precise nightly build you are using; try rustc --version

# rustc --version
rustc 1.74.0 (79e9716c9 2023-11-13) (Fedora 1.74.0-1.fc39)

What I tried to do

Please include sample code or links to your project!

If add #[context()] for the function which has the return type impl Iterator<Item = xxx>, get error[E0562]: impl Trait only allowed in function and inherent method return types, not in closure return types.

use fn_error_context::context;

#[context("Scan all tmpfiles conf and save entries")]
fn empty_iterators() -> impl Iterator<Item = String> {
    std::iter::empty::<_>()
}

fn main() {   
    let _ = empty_iterators();   
}

What happened

Describe what happened in as much detail as possible.
For compiler ICEs, a good idea would be to include the output with RUST_BACKTRACE=1.

When run above code, get error:

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in closure return types
 --> src/main.rs:4:25
  |
4 | fn empty_iterators() -> impl Iterator<Item = String> {
  |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

What I expected

When remove #[context()] line, no error. But it should support.

@cgwalters
Copy link

I think the tricky thing here is that due to the use of the fn-error-context crate it's not immediately obvious (to someone starting work on a codebase that uses the crate without being familiar with its implementation) that the macro rewrote the function to use an inner closure.

Hmm, I wonder if it'd be possible to change that crate to use an inner fn instead of a closure...thinking about it briefly it seems likely.

I'm not a maintainer for this repository but I suspect there's not much immediately actionable for the Rust trait maintainers.

Ah, there's already an issue: andrewhickman/fn-error-context#7 so I think we should probably discuss there.

@HuijingHei
Copy link
Author

Thanks @cgwalters for the clarification, then I will close this issue and track in andrewhickman/fn-error-context#7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants