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

make CStr::from_bytes_with_nul_unchecked() a const fn #54745

Merged
merged 2 commits into from
Oct 3, 2018

Conversation

abonander
Copy link
Contributor

closes #54678

@rust-highfive
Copy link
Collaborator

r? @dtolnay

(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 Oct 2, 2018
@abonander
Copy link
Contributor Author

Not sure if this needs a test to confirm the ability to use this in a const, I guess as a regression test if this were to be removed.

@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.
[00:04:58]    Compiling alloc v0.0.0 (/checkout/src/liballoc)
[00:04:59]    Compiling alloc_system v0.0.0 (/checkout/src/liballoc_system)
[00:04:59]    Compiling panic_abort v0.0.0 (/checkout/src/libpanic_abort)
[00:05:03]    Compiling panic_unwind v0.0.0 (/checkout/src/libpanic_unwind)
[00:05:08] error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
[00:05:08]      |
[00:05:08]      |
[00:05:08] 1044 |         mem::transmute(bytes)
[00:05:08]      |         ^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
[00:05:08]      |
[00:05:08]      = note: consult the function's documentation for information on how to avoid undefined behavior
[00:05:08] error: aborting due to previous error
[00:05:08] 
[00:05:08] error: Could not compile `std`.
[00:05:08] 
[00:05:08] 
[00:05:08] To learn more, run the command again with --verbose.
[00:05:08] 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 jemalloc backtrace" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "--message-format" "json"
[00:05:08] expected success, got: exit code: 101
[00:05:08] thread 'main' panicked at 'cargo must succeed', bootstrap/compile.rs:1112:9
[00:05:08] travis_fold:end:stage0-std

[00:05:08] travis_time:end:stage0-std:start=1538477459977074430,finish=1538477497042180674,duration=37065106244

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)

@abonander
Copy link
Contributor Author

@oli-obk little snag with const_transmute here

@@ -1040,8 +1040,8 @@ impl CStr {
/// ```
#[inline]
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
&*(bytes as *const [u8] as *const CStr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh. I should have checked the original source :D You should be able to just add #![feature(const_raw_ptr_deref)] and keep the original source ;)

@oli-obk
Copy link
Contributor

oli-obk commented Oct 2, 2018

Not sure if this needs a test to confirm the ability to use this in a const, I guess as a regression test if this were to be removed.

A test would be great, yes.

@abonander
Copy link
Contributor Author

r? @oli-obk

@rust-highfive rust-highfive assigned oli-obk and unassigned dtolnay Oct 2, 2018
@@ -1040,7 +1040,8 @@ impl CStr {
/// ```
#[inline]
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
#[rustc_const_unstable]
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@oli-obk preferred ordering of the attributes? shortest to longest or doesn't really matter?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it matters. I ususally put the const unstable attribute below the stable attribute

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, and added it to the crate feature flags as well.

@@ -1040,7 +1040,8 @@ impl CStr {
/// ```
#[inline]
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
#[rustc_const_unstable]
Copy link
Contributor

Choose a reason for hiding this comment

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

you need to specify a feature name like in

#[rustc_const_unstable(feature="fzzzzzt")] //~ERROR internal feature

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I just make one up or what?

Copy link
Contributor

Choose a reason for hiding this comment

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

jup :D that's how feature names work

@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.
[00:04:52]    Compiling alloc v0.0.0 (/checkout/src/liballoc)
[00:04:53]    Compiling alloc_system v0.0.0 (/checkout/src/liballoc_system)
[00:04:53]    Compiling panic_abort v0.0.0 (/checkout/src/libpanic_abort)
[00:04:57]    Compiling panic_unwind v0.0.0 (/checkout/src/libpanic_unwind)
[00:04:59] error[E0548]: incorrect stability attribute type
[00:04:59]      |
[00:04:59] 1043 |     #[rustc_const_unstable]
[00:04:59]      |     ^^^^^^^^^^^^^^^^^^^^^^^
[00:04:59] 
---
[00:05:01] 
[00:05:01] To learn more, run the command again with --verbose.
[00:05:01] 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 jemalloc backtrace" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "--message-format" "json"
[00:05:01] expected success, got: exit code: 101
[00:05:01] thread 'main' panicked at 'cargo must succeed', bootstrap/compile.rs:1112:9
[00:05:01] travis_fold:end:stage0-std

[00:05:01] travis_time:end:stage0-std:start=1538479266326394735,finish=1538479303466363965,duration=37139969230


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

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:08ccbdf5
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
travis_time:end:26a7a514:start=1538479304164944651,finish=1538479304170228523,duration=5283872
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:072432a7
$ 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:03aefab2
travis_time:start:03aefab2
$ 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:0965d8e0
$ 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)

@oli-obk
Copy link
Contributor

oli-obk commented Oct 2, 2018

@bors r+

@bors
Copy link
Contributor

bors commented Oct 2, 2018

📌 Commit e0caaec has been approved by oli-obk

@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 Oct 2, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Oct 2, 2018
make `CStr::from_bytes_with_nul_unchecked()` a const fn

closes rust-lang#54678
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Oct 2, 2018
make `CStr::from_bytes_with_nul_unchecked()` a const fn

closes rust-lang#54678
bors added a commit that referenced this pull request Oct 2, 2018
Rollup of 10 pull requests

Successful merges:

 - #54269 (#53840: Consolidate pattern check errors)
 - #54458 (Allow both explicit and elided lifetimes in the same impl header)
 - #54603 (Add `crate::` to trait suggestions in Rust 2018.)
 - #54648 (Update Cargo's submodule)
 - #54680 (make run-pass tests with empty main just compile-pass tests)
 - #54687 (Use impl_header_lifetime_elision in libcore)
 - #54699 (Re-export `getopts` so custom drivers can reference it.)
 - #54702 (do not promote comparing function pointers)
 - #54728 (Renumber `proc_macro` tracking issues)
 - #54745 (make `CStr::from_bytes_with_nul_unchecked()` a const fn)

Failed merges:

r? @ghost
@bors bors merged commit e0caaec into rust-lang:master Oct 3, 2018
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.

CStr unchecked constructor can be a const fn
5 participants