Skip to content

SemVer hazard: adding a Drop impl #15471

@djc

Description

@djc

It appears that adding a Drop impl for a type that previously didn't have on can be a semver hazard. Example error from rustls/pki-types#79:

djc-2021 main rustls-pki-types $ cargo c
    Checking rustls-pki-types v1.11.0 (/Users/djc/src/rustls-pki-types)
error[E0505]: cannot move out of `key` because it is borrowed
   --> src/lib.rs:287:52
    |
285 |     fn try_from(key: Vec<u8>) -> Result<Self, Self::Error> {
    |                 --- binding `key` declared here
286 |         Ok(match PrivateKeyDer::try_from(&key[..])? {
    |                  ----------------------------------
    |                  |                        |
    |                  |                        borrow of `key` occurs here
    |                  a temporary with access to the borrow is created here ...
287 |             PrivateKeyDer::Pkcs1(_) => Self::Pkcs1(key.into()),
    |                                                    ^^^ move out of `key` occurs here
...
291 |     }
    |     - ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `ControlFlow<Result<Infallible, &str>, PrivateKeyDer<'_>>`
    |
    = note: the temporary is part of an expression at the end of a block;
            consider forcing this temporary to be dropped sooner, before the block's local variables are dropped
help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block
    |
286 ~         let x = Ok(match PrivateKeyDer::try_from(&key[..])? {
287 |             PrivateKeyDer::Pkcs1(_) => Self::Pkcs1(key.into()),
288 |             PrivateKeyDer::Sec1(_) => Self::Sec1(key.into()),
289 |             PrivateKeyDer::Pkcs8(_) => Self::Pkcs8(key.into()),
290 ~         }); x
    |
help: consider cloning the value if the performance cost is acceptable
    |
286 |         Ok(match PrivateKeyDer::try_from(&key.clone()[..])? {
    |                                              ++++++++

We found that the SemVer reference currently doesn't seem to talk about this, although the Drop reference docs have a bunch of context in https://doc.rust-lang.org/std/ops/trait.Drop.html#drop-check.

cc @obi1kenobi

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-documenting-cargo-itselfArea: Cargo's documentationA-semverArea: semver specifications, version matching, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions