Skip to content

Closure Does/Doesn't compile depending purely on usage of a {} body with --edition=2021 #105699

Open
@Olipro

Description

@Olipro

Example available on https://rust.godbolt.org/z/bzjPv6TGT

You get a failure if you remove the edition flag, but it's completely different and that may or may not also be incorrect.

trait Tr {}

struct T {
    obj: Box<dyn Tr>,
}
struct U;

impl Tr for U{}

impl T {
    fn works<'a>(&'a mut self) -> Option<&'a mut dyn Tr> {
        true.then(|| { self.obj.as_mut() })
    }

    fn fails<'a>(&'a mut self) -> Option<&'a mut dyn Tr> {
        true.then(|| self.obj.as_mut())
    }
}

fn main() {
    let mut t = T{obj: Box::new(U{})};
    let _u = t.works();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-closuresArea: Closures (`|…| { … }`)A-coercionsArea: implicit and explicit `expr as Type` coercionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions