rust-lang / rust-lang/rust

Implementing `Copy` can be a breaking change

Open
#126,179 11 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-lifetimes A-trait-system C-bug I-lang-radar T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

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

// Uncomment this line and the code stops compiling
//impl Copy for Foo<'static> {}

struct Bar<'a>(Foo<'a>);

fn do_thing<'a>(b: Bar<'a>) {
    drop(b.0);
}

I expected to see this happen: Uncommenting the line in question does not break the code, as implementing Copy should not be a breaking change

Instead, this happened:

error: lifetime may not live long enough
  --> src/lib.rs:10:10
   |
9  | fn do_thing<'a>(b: Bar<'a>) {
   |             -- lifetime `'a` defined here
10 |     drop(b.0);
   |          ^^^ copying this value requires that `'a` must outlive `'static`
Meta

Regressed in 1.42 according to Godbolt.

@rustbot label A-borrow-checker A-lifetimes A-traits T-types regression-from-stable-to-stable

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the src/lib.rs reproducer and compare compilation with and without the Copy implementation, as in the report and its Godbolt regression check. Investigate the borrow-checker, lifetime, and trait behavior implicated by the diagnostic. Done means the reproducer compiles after implementing Copy for Foo<'static> without requiring 'a to outlive 'static.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.