-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-NLLArea: Non-lexical lifetimes (NLL)Area: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-type-systemArea: Type systemArea: Type systemC-bugCategory: This is a bug.Category: This is a bug.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.P-lowLow priorityLow priorityfixed-by-NLLBugs fixed, but only when NLL is enabled.Bugs fixed, but only when NLL is enabled.
Description
UPDATE: This needs a test! Mentoring instructions can be found here.
While working on some code using std::borrow::Cow I encountered some strange behavior. Here is a simplified version of the code I was using:
use std::borrow::Cow;
#[derive(Clone, Debug)]
struct S<'a> {
name: Cow<'a, str>
}
#[derive(Clone, Debug)]
struct T<'a> {
s: Cow<'a, [S<'a>]>
}
fn main() {
let s1 = [S { name: Cow::Borrowed("Test1") }, S { name: Cow::Borrowed("Test2") }];
let b1 = T { s: Cow::Borrowed(&s1) };
let s2 = [S { name: Cow::Borrowed("Test3") }, S { name: Cow::Borrowed("Test4") }];
let b2 = T { s: Cow::Borrowed(&s2) };
let mut v = Vec::new();
v.push(b1);
v.push(b2);
println!("{:?}", v);
}
This fails with
error[E0597]:
s2
does not live long enough
Removing b1,s1 or b2,s2 from the code fixes the error. However, since the very same pattern isused for both variable pairs it seems to be inconsistent behavior of the borrow checker.
Meta
Versions checked:
rustc 1.18.0 (03fc9d6 2017-06-06)
binary: rustc
commit-hash: 03fc9d6
commit-date: 2017-06-06
host: x86_64-pc-windows-gnu
release: 1.18.0
LLVM version: 3.9
rustc 1.19.0-beta.2 (a175ee5 2017-06-15)
rustc 1.20.0-nightly (734c836 2017-07-03)
Metadata
Metadata
Assignees
Labels
A-NLLArea: Non-lexical lifetimes (NLL)Area: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-type-systemArea: Type systemArea: Type systemC-bugCategory: This is a bug.Category: This is a bug.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.P-lowLow priorityLow priorityfixed-by-NLLBugs fixed, but only when NLL is enabled.Bugs fixed, but only when NLL is enabled.