Skip to content

Double-indirection with Cow causes inconsistent behavior of the borrow checker #43058

Closed
@darkwisebear

Description

@darkwisebear

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)A-borrow-checkerArea: The borrow checkerA-type-systemArea: Type systemC-bugCategory: This is a bug.E-mentorCall 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.P-lowLow priorityfixed-by-NLLBugs fixed, but only when NLL is enabled.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions