Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.. syntax copies lifetime #91219

Closed
DzenanJupic opened this issue Nov 25, 2021 · 2 comments
Closed

.. syntax copies lifetime #91219

DzenanJupic opened this issue Nov 25, 2021 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@DzenanJupic
Copy link

I tried this code playground:

use std::borrow::Cow;

struct S<'a> {
    cow: Cow<'a, str>,
    owned: String,
}

fn make_owned(s: S<'_>) -> S<'static> {
    S {
        cow: Cow::Owned(s.cow.into_owned()),
        owned: s.owned,
    }
}

// does not work
// fn make_owned2(s: S<'_>) -> S<'static> {
//     S {
//         cow: Cow::Owned(s.cow.into_owned()),
//         ..s
//     }
// }

I expected to see this happen:

Since all fields with a lifetime were replaced by owned values I expected make_owned2 to compile.

Instead, this happened:

The compiler is unable to understand, that the remaining fields don't influence the lifetime of the struct.

error[E0621]: explicit lifetime required in the type of `s`
  --> src/lib.rs:18:5
   |
18 | /     S {
19 | |         cow: Cow::Owned(s.cow.into_owned()),
20 | |         ..s
21 | |     }
   | |_____^ lifetime `'static` required

Meta

rustc --version --verbose:

rustc 1.58.0-nightly (65f3f8b22 2021-11-21)
binary: rustc
commit-hash: 65f3f8b220f020e562c5dd848ff7319257a7ba45
commit-date: 2021-11-21
host: x86_64-pc-windows-msvc
release: 1.58.0-nightly
LLVM version: 13.0.0
@DzenanJupic DzenanJupic added the C-bug Category: This is a bug. label Nov 25, 2021
@fmease
Copy link
Member

fmease commented Nov 25, 2021

In the current version of Rust, this works as intended: The type cannot change with the struct update syntax. On nightly with #![feature(type_changing_struct_update)], your second function compiles successfully. See #86555.

@nagisa
Copy link
Member

nagisa commented Nov 25, 2021

Closing as…

Duplicate of #86555

@nagisa nagisa closed this as completed Nov 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants