Skip to content

repeat expression do not honor lt bounds of copy #95477

Closed
@lcnr

Description

@lcnr
#![feature(nll)]

#[derive(Clone)]
struct Foo<'a>(fn(&'a ()) -> &'a ());

impl Copy for Foo<'static> {}

fn mk_foo<'a>() -> Foo<'a> {
    println!("mk_foo");
    Foo(|x| x)
}

fn foo<'a>() -> [Foo<'a>; 100] {
    [mk_foo::<'a>(); 100]
}

fn main() {
    foo();
}

this compiles both with and without feature(nll). Note that mk_foo creates a value of type Foo<'a> which may not be copied.

Moving mk_foo::<'a>() into a local errors as expected:

#![feature(nll)]

#[derive(Clone)]
struct Foo<'a>(fn(&'a ()) -> &'a ());

impl Copy for Foo<'static> {}

fn mk_foo<'a>() -> Foo<'a> {
    println!("mk_foo");
    Foo(|x| x)
}

fn foo<'a>() -> [Foo<'a>; 100] {
    let x = mk_foo::<'a>();
    [x; 100]
}

this results in

error: lifetime may not live long enough
  --> src/main.rs:15:6
   |
13 | fn foo<'a>() -> [Foo<'a>; 100] {
   |        -- lifetime `'a` defined here
14 |     let x = mk_foo::<'a>();
15 |     [x; 100]
   |      ^ copying this value requires that `'a` must outlive `'static`

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-lowLow priorityT-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

Status

Completed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions