-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Open
Labels
A-type-systemArea: Type systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: 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.Relevant 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.Relevant to the types team, which will review and decide on the PR/issue.
Description
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
Labels
A-type-systemArea: Type systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: 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.Relevant 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.Relevant to the types team, which will review and decide on the PR/issue.