rust-lang / rust-lang/rust

The documentation of `Arc`'s `Weak::upgrade` does not mention dissosiation

Open
#154,936 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs T-libs
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Location (URL)

https://doc.rust-lang.org/std/sync/struct.Weak.html#method.upgrade

Summary

The documentation says:

Attempts to upgrade the Weak pointer to an Arc, delaying dropping of the inner value if successful.
Returns None if the inner value has since been dropped.

It is possible that the value is not dropped, but the Weak reference is dissosiated due to the call to Arc::make_mute and upgrade would still return None:

use std::sync::Arc;

fn main() {
    let mut r = Arc::new(S { num: 5 });
    
    println!("Having {}", r.num);

    let wr = Arc::downgrade(&r);

    let s = Arc::make_mut(&mut r);

    s.num += 1;

    assert!(wr.upgrade().is_none());
}

#[derive(Debug)]
pub struct S {
    pub num: i32,
}

impl Clone for S {
    fn clone(&self) -> Self {
        Self { num: self.num }
    }
}

impl Drop for S {
    fn drop(&mut self) {
        println!("Dropping {}", self.num);
    }
}

Should it be mentioned in the documentation of Weak::upgrade that it may return None for dessosiated references which does not necessarily imply dropping?

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 at the documented Arc::Weak::upgrade entry point in the linked standard-library URL and compare its wording with the Arc::make_mut behavior shown in the issue. Done means the documentation explains that dissociated Weak references can make upgrade return None even when the inner value has not been dropped.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.