Skip to content

support deeply nested coercions #40924

@nikomatsakis

Description

@nikomatsakis

It would be nice if this code were to compile:

struct Foo {
    v: Vec<i32>
}

fn bar(f: &Foo, g: &[i32]) {
    let x = match true {
        true => {
            match f {
                &Foo { ref v } => &v,
            }
        }
        false => {
            g
        }
    };

    println!("{:?}", x);
}

fn main() { }

At present, it does not. The reason is that we apply a &x[..] coercion to the inner match expression, but the result of the arm is not coerced, and hence has type &&Vec<i32>. This then fails the borrow-check because the borrow of v outlives the arm. If you change the arm to &Foo { ref v } => v or &Foo { ref v } => &v[..], it will compile fine.

The following very similar example using an if does work, or at least did, but somewhat accidentally. Pull request #40224 winds up breaking it as part of a refactoring; hopefully nobody will notice. =)

struct Foo {
    v: Vec<i32>
}

fn bar(f: &Foo, g: &[i32]) {
    let x = if true {
        match f {
            &Foo { ref v } => &v,
        }
    } else {
        g
    };

    println!("{:?}", x);
}

fn main() { }

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.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