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

Add mem::forget_unsized() for forgetting unsized values #55785

Merged
merged 5 commits into from Nov 15, 2018
Merged

Add mem::forget_unsized() for forgetting unsized values #55785

merged 5 commits into from Nov 15, 2018

Conversation

ghost
Copy link

@ghost ghost commented Nov 8, 2018

Allows passing values of T: ?Sized types to mem::drop and mem::forget.

Adds mem::forget_unsized() that accepts T: ?Sized.

I had to revert the PR that removed the forget intrinsic and replaced it with ManuallyDrop: #40559
We can't use ManuallyDrop::new() here because it needs T: Sized and we don't have support for unsized return values yet (will we ever?).

r? @eddyb

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 8, 2018
@ghost
Copy link
Author

ghost commented Nov 8, 2018

By the way, I hope those #[cfg(stage0)] shenanigans work out. I've never used this attribute before.

@eddyb
Copy link
Member

eddyb commented Nov 8, 2018

r? @alexcrichton

@rust-highfive rust-highfive assigned alexcrichton and unassigned eddyb Nov 8, 2018
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-5.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:19f0e510:start=1541693632881082181,finish=1541693687703607503,duration=54822525322
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#Pull-Requests-and-Security-Restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
Setting environment variables from .travis.yml
$ export IMAGE=x86_64-gnu-llvm-5.0
---
[00:21:10]    Compiling rustc_msan v0.0.0 (/checkout/src/librustc_msan)
[00:21:17] error[E0277]: the size for values of type `T` cannot be known at compilation time
[00:21:17]    --> libcore/mem.rs:144:14
[00:21:17]     |
[00:21:17] 144 |     unsafe { intrinsics::forget(t) }
[00:21:17]     |
[00:21:17]     = help: the trait `marker::Sized` is not implemented for `T`
[00:21:17]     = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
[00:21:17]     = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
[00:21:17]     = help: consider adding a `where T: marker::Sized` bound
[00:21:17] note: required by `intrinsics::::forget`
[00:21:17]     |
[00:21:17]     |
[00:21:17] 719 |     pub fn forget<T>(_: T);
[00:21:17] 
[00:21:25] error: aborting due to previous error
[00:21:25] 
[00:21:25] For more information about this error, try `rustc --explain E0277`.
[00:21:25] For more information about this error, try `rustc --explain E0277`.
[00:21:25] error: Could not compile `core`.
[00:21:25] 
[00:21:25] To learn more, run the command again with --verbose.
[00:21:25] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "x86_64-unknown-linux-gnu" "-j" "4" "--release" "--locked" "--color" "always" "--features" "panic-unwind backtrace" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "--message-format" "json"
[00:21:25] expected success, got: exit code: 101
[00:21:25] thread 'main' panicked at 'cargo must succeed', bootstrap/compile.rs:1101:9
[00:21:25] travis_fold:end:stage1-std

[00:21:25] travis_time:end:stage1-std:start=1541694955363872284,finish=1541694983860242104,duration=28496369820


[00:21:25] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap build
[00:21:25] Build completed unsuccessfully in 0:17:37
[00:21:25] Makefile:28: recipe for target 'all' failed
[00:21:25] make: *** [all] Error 1

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:11efb3f3
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
travis_time:end:19b1f982:start=1541694984580977248,finish=1541694984585804604,duration=4827356
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:053b9d2b
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:107f2ba0
travis_time:start:107f2ba0
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:0c4923e4
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@alexcrichton
Copy link
Member

I'll admit that I don't really understand the implications of this change. These are two very prominent functions, though, so I'd be wary about exposing functionality which we don't actually want to stabilize.

How come these functions are necessary? Is there a way we could expose unstable versions for now?

@ghost
Copy link
Author

ghost commented Nov 8, 2018

How come these functions are necessary?

Consider how one might allocate an unsized value on the heap:

let val: impl ?Sized = ...;

let dest = alloc(Layout::for_value(&val));
ptr::copy_nonoverlapping(&val as *const _ as *const u8, dest, mem::size_of_val(&val));

mem::forget(val); // We need this!

As far as I can tell, there is literally no way to forget an unsized local right now.

Is there a way we could expose unstable versions for now?

I'd be okay with having something like mem::forget_unsized() until unsized locals get stabilized.

@eddyb
Copy link
Member

eddyb commented Nov 8, 2018

AFAIK calling these with an unsized value is unstable (and we could test that).
That is, I think this requires the gate:

fn forget_boxed<T: ?Sized>(b: Box<T>) {
    std::mem::forget(*b);
}

EDIT: this seems to only error on drop, not drop_boxed, that's worrying!

fn drop<T: ?Sized>(_: T) {}
fn drop_boxed<T: ?Sized>(b: Box<T>) {
    drop(*b);
}

But this does respect the feature-gate, so that seems inconsistent:

fn drop_boxed<T: ?Sized>(b: Box<T>) {
    {*b};
}

@alexcrichton
Copy link
Member

Hm so given @eddyb's discovery, could we perhaps add a separate method for now? The intention can be to remove the unstable separate method before stabilization, but for now it seems best to not tamper too much with the existing functions while the story for unsized locals is still being sorted out

@rust-lang rust-lang deleted a comment from rust-highfive Nov 9, 2018
@Centril
Copy link
Contributor

Centril commented Nov 9, 2018

I don't think we should land this (changing drop and forget) before #![feature(unsized_locals)] is stabilized since it effectively stabilizes the feature gate to a certain extent in a way that is observable.

#![feature(unsized_locals)]

fn drop<T: ?Sized>(_: T) {} // if you remove this line...
trait A {}
const FOO: fn(dyn A) = drop;
// then you will get:  ^^^^ doesn't have a size known at compile-time

I have no objections to doing what @alexcrichton suggests; adding two temporary unstable functions seems fine.

@eddyb
Copy link
Member

eddyb commented Nov 10, 2018

Note that we only need an unstable function for forget, drop you can do yourself.

@ghost
Copy link
Author

ghost commented Nov 12, 2018

@eddyb Sounds good!

Do I need to open a tracking issue for forget_unsized?

@alexcrichton
Copy link
Member

I think it's fine to skip it for now. Could the documentation perhaps explicitly say that it's a shim intended to go away once the lang feature is stabilized? I think it's ok to avoid copy/pasting most of the other docs too, the function can just reference the already existing one

@ghost
Copy link
Author

ghost commented Nov 12, 2018

@alexcrichton Done!

@ghost ghost changed the title Allow unsized types in mem::drop and mem::forget Add mem::forget_unsized() for forgetting unsized values Nov 12, 2018
@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Nov 12, 2018

📌 Commit 56d3a82 has been approved by alexcrichton

@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 Nov 12, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Nov 12, 2018
…xcrichton

Add mem::forget_unsized() for forgetting unsized values

~~Allows passing values of `T: ?Sized` types to `mem::drop` and `mem::forget`.~~

Adds `mem::forget_unsized()` that accepts `T: ?Sized`.

I had to revert the PR that removed the `forget` intrinsic and replaced it with `ManuallyDrop`: rust-lang#40559
We can't use `ManuallyDrop::new()` here because it needs `T: Sized` and we don't have support for unsized return values yet (will we ever?).

r? @eddyb
#[cfg(not(stage0))]
#[unstable(feature = "forget_unsized", issue = "0")]
pub fn forget_unsized<T: ?Sized>(t: T) {
unsafe { intrinsics::forget(t) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could ManuallyDrop be used in the future?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, provided we implement support for unsized return values. The reason is that ManuallyDrop::new(t) returns a ManuallyDrop<T>, which is unsized.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, I was thinking in terms of construction. A pub(crate) ManuallyDrop::forget method that doesn't actually return ManuallyDrop could still work, right?

Copy link
Author

@ghost ghost Nov 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, you're totally right. I think adding the forget intrinsic was unnecessary since we can do:

pub fn forget_unsized<T: ?Sized>(t: T) {
    ManuallyDrop { value: t };
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should we r- then and use that instead? Seems nicer than an intrinsic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems I was wrong. It's still impossible to initialize structs with unsized fields, so we'll have to live with intrinsics::forget for a while.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2015&gist=6becf5139279bf16437a35617dfbf47b

/// Like [`forget`], but also accepts unsized values.
///
/// This function is just a shim intended to be removed when the `unsized_locals` feature gets
/// stabilized.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in favor of making mem::forget support unsized types, I assume?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

pietroalbini added a commit to pietroalbini/rust that referenced this pull request Nov 15, 2018
…xcrichton

Add mem::forget_unsized() for forgetting unsized values

~~Allows passing values of `T: ?Sized` types to `mem::drop` and `mem::forget`.~~

Adds `mem::forget_unsized()` that accepts `T: ?Sized`.

I had to revert the PR that removed the `forget` intrinsic and replaced it with `ManuallyDrop`: rust-lang#40559
We can't use `ManuallyDrop::new()` here because it needs `T: Sized` and we don't have support for unsized return values yet (will we ever?).

r? @eddyb
bors added a commit that referenced this pull request Nov 15, 2018
Rollup of 17 pull requests

Successful merges:

 - #55182 (Redox: Update to new changes)
 - #55211 (Add BufWriter::buffer method)
 - #55507 (Add link to std::mem::size_of to size_of intrinsic documentation)
 - #55530 (Speed up String::from_utf16)
 - #55556 (Use `Mmap` to open the rmeta file.)
 - #55622 (NetBSD: link libstd with librt in addition to libpthread)
 - #55750 (Make `NodeId` and `HirLocalId` `newtype_index`)
 - #55778 (Wrap some query results in `Lrc`.)
 - #55781 (More precise spans for temps and their drops)
 - #55785 (Add mem::forget_unsized() for forgetting unsized values)
 - #55852 (Rewrite `...` as `..=` as a `MachineApplicable` 2018 idiom lint)
 - #55865 (Unix RwLock: avoid racy access to write_locked)
 - #55901 (fix various typos in doc comments)
 - #55926 (Change sidebar selector to fix compatibility with docs.rs)
 - #55930 (A handful of hir tweaks)
 - #55932 (core/char: Speed up `to_digit()` for `radix <= 10`)
 - #55956 (add tests for some fixed ICEs)

Failed merges:

r? @ghost
@bors bors merged commit 56d3a82 into rust-lang:master Nov 15, 2018
@ghost ghost deleted the unsized-drop-forget branch November 15, 2018 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

6 participants