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

impl Trait cannot be used as return type #7

Open
quark-zju opened this issue Jun 13, 2023 · 1 comment
Open

impl Trait cannot be used as return type #7

quark-zju opened this issue Jun 13, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@quark-zju
Copy link

Example:

#[context("failed to parse config at `{}`", path.as_ref().display())]
pub fn parse_config(path: impl AsRef<Path>) -> anyhow::Result<impl std::fmt::Display> {
    let text = read_to_string(path.as_ref())?;
    Ok(text.parse()?)
}

0.2.1 expands it to:

pub fn parse_config(path: impl AsRef<Path>) -> anyhow::Result<impl std::fmt::Display> {
    let force_fn_once = ::core::iter::empty::<()>();
    (|| -> anyhow::Result<impl std::fmt::Display> {
        ::core::mem::drop(force_fn_once);
        {
            let text = read_to_string(path.as_ref())?;
            Ok(text.parse()?)
        }
    })()
        .map_err(|err| {
            err
                .context({
                    let res = ::alloc::fmt::format(
                        format_args!(
                            "failed to parse config at `{0}`", path.as_ref().display()
                        ),
                    );
                    res
                })
                .into()
        })
}

which fails to compile:

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in closure return types
 --> examples\context.rs:7:63
  |
7 | pub fn parse_config(path: impl AsRef<Path>) -> anyhow::Result<impl std::fmt::Display> {
  |                                                               ^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0562`.
@cgwalters
Copy link

cc rust-lang/impl-trait-initiative#18

In that issue I had a suggestion that it might be possible to rewrite the macro here to use an inner fn instead of a closure.

@andrewhickman andrewhickman added the enhancement New feature or request label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants