Skip to content

Re-stabilize Weak::as_ptr and friends for unsized T - #80764

Merged
bors merged 8 commits into
rust-lang:masterfrom
CAD97:weak-unsized-as-ptr-again
Jan 16, 2021
Merged

bors merged 8 commits into
rust-lang:masterfrom
CAD97:weak-unsized-as-ptr-again

Conversation

@CAD97

@CAD97 CAD97 commented Jan 6, 2021 •

Copy link
Copy Markdown
Contributor

View all comments

As per T-lang consensus, this uses a branch to handle the dangling case. The discussed optimization of only doing the branch in the T: ?Sized case is left for a followup patch, as doing so is not trivial (as it requires specialization) and not obviously better (as it requires using wrapping_offset rather than offset more).

Basically said optimization

Specialize on T: Sized:

fn as_ptr(&self) -> *const T {
    if [ T is Sized ] || !is_dangling(ptr) {
        (ptr as *mut T).set_ptr_value( (ptr as *mut u8).wrapping_offset(data_offset) )
    } else {
        ptr::null()
    }
}

fn from_raw(*const T) -> Self {
    if [ T is Sized ] || !ptr.is_null() {
        let ptr = (ptr as *mut RcBox).set_ptr_value( (ptr as *mut u8).wrapping_offset(-data_offset) );
        Weak { ptr }
    } else {
        Weak::new()
    }
}

(but with more set_ptr_value to avoid Sized restrictions and maintain metadata.)

Written in this fashion, this is not a correctness-critical specialization (i.e. so long as [ T is Sized ] is false for unsized T, it can be rand() for sized T without breaking correctness), but it's still touchy, so I'd rather do it in another PR with separate review.


This effectively reverts #80422 and re-establishes #74160. T-libs previously signed off on this stable API change in #74160.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 6, 2021
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job x86_64-gnu-llvm-9 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
.................................................................................................... 9000/11244
.................................................................................................... 9100/11244
.................................................................................................... 9200/11244
........................................i......i.................................................... 9300/11244
...............................................................................iiiiii..iiiiii.i..... 9400/11244
.................................................................................................... 9600/11244
.................................................................................................... 9700/11244
.................................................................................................... 9800/11244
.................................................................................................... 9900/11244
---
Suite("src/test/assembly") not skipped for "bootstrap::test::Assembly" -- not in ["src/tools/tidy"]
Check compiletest suite=assembly mode=assembly (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)

running 27 tests
iiiiiiiiiiiiiiiiiiiiiiiiiii

 finished in 0.071 seconds
Suite("src/test/incremental") not skipped for "bootstrap::test::Incremental" -- not in ["src/tools/tidy"]
Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
 finished in 11.109 seconds
Check compiletest suite=debuginfo mode=debuginfo (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)

running 116 tests
iiiiiiiiii.i.i..i..i...ii...i.i....ii..........iiii.........i.....i...i........iii.ii.....iiii.....i 100/116
test result: ok. 78 passed; 0 failed; 38 ignored; 0 measured; 0 filtered out; finished in 2.36s

 finished in 2.432 seconds
Suite("src/test/ui-fulldeps") not skipped for "bootstrap::test::UiFullDeps" -- not in ["src/tools/tidy"]
---
....................................................................F............................... 200/247
..................F............................
failures:

---- rc::tests::test_maybe_thin_unsized stdout ----
thread 'rc::tests::test_maybe_thin_unsized' panicked at 'assertion failed: `(left == right)`
  left: `"\"swordfish\""`,
 right: `"swordfish"`', library/alloc/src/rc/tests.rs:327:5

---- sync::tests::test_maybe_thin_unsized stdout ----
thread 'sync::tests::test_maybe_thin_unsized' panicked at 'assertion failed: `(left == right)`
  left: `"\"swordfish\""`,
 right: `"swordfish"`', library/alloc/src/sync/tests.rs:362:5

failures:
failures:
    rc::tests::test_maybe_thin_unsized
    sync::tests::test_maybe_thin_unsized
test result: FAILED. 245 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.25s


error: test failed, to rerun pass '-p alloc --lib'


command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "test" "--target" "x86_64-unknown-linux-gnu" "-Zbinary-dep-depinfo" "-j" "16" "--release" "--locked" "--color" "always" "--features" "panic-unwind backtrace compiler-builtins-c" "--manifest-path" "/checkout/library/test/Cargo.toml" "-p" "alloc" "--" "--quiet"


failed to run: /checkout/obj/build/bootstrap/debug/bootstrap --stage 2 test --exclude src/tools/tidy
Build completed unsuccessfully in 0:21:35

@CAD97
CAD97 force-pushed the weak-unsized-as-ptr-again branch from 863883b to 9e7e7ca Compare January 6, 2021 23:36
As per T-lang consensus, this uses a branch to handle the dangling case.
The discussed optimization of only doing the branch in the T: ?Sized
case is left for a followup patch, as doing so is not trivial
(as it requires specialization for correctness, not just optimization).
@CAD97
CAD97 force-pushed the weak-unsized-as-ptr-again branch from 9e7e7ca to 6bc772c Compare January 7, 2021 00:31
Comment thread library/alloc/src/rc.rs Resolved
Comment thread library/alloc/src/rc.rs Resolved
Comment thread library/alloc/src/rc.rs Resolved
Comment thread library/alloc/src/sync.rs Resolved
@Mark-Simulacrum

Copy link
Copy Markdown
Member

@RalfJung do you think you can handle review here? It'll likely take me some time to get up to speed on the relevant code, I have not looked at Rc/Weak in detail in a while.

@RalfJung

ghost commented Jan 9, 2021

Copy link
Copy Markdown
Member

Sure, can do.
r? @RalfJung

Comment thread library/alloc/src/rc.rs Resolved
Comment thread library/alloc/src/rc.rs Resolved
Comment thread library/alloc/src/sync.rs Resolved
Co-authored-by: Ralf Jung <post@ralfj.de>
@RalfJung

ghost commented Jan 16, 2021

Copy link
Copy Markdown
Member

Awesome, thanks a lot. :)
@bors r+

@bors

ghost commented Jan 16, 2021

Copy link
Copy Markdown
Contributor

📌 Commit c14e919 has been approved by RalfJung

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 16, 2021
@bors
bors merged commit 5702cfa into rust-lang:master Jan 16, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 16, 2021
@CAD97
CAD97 deleted the weak-unsized-as-ptr-again branch January 16, 2021 23:16
@clarfonthey clarfonthey added the A-rc Area: `Rc` and `Arc` label Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-rc Area: `Rc` and `Arc` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants