Skip to content

impl Trait cannot be used as return type #7

Open
@quark-zju

Description

@quark-zju

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`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions