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

Allow calling const unsafe fn in const fn behind a feature gate #55635

Merged
merged 23 commits into from
Dec 6, 2018

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Nov 2, 2018

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

@Centril Centril left a comment

Choose a reason for hiding this comment

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

Tests look mostly good :)

}
// not ok
const unsafe fn foo8_2() -> i32 {
foo4() //~ ERROR not allowed in const fn
Copy link
Contributor

Choose a reason for hiding this comment

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

The error message doesn't seem great here; the user isn't made aware that it can be fixed by wrapping in unsafe { ... } so knowing how to fix it sort of depends on having been introduced to the tracking issue / PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

It may indeed be worth having an extra note like:

= note: unsafe actions within a `const unsafe fn` still require an `unsafe` block

src/test/ui/consts/min_const_fn/min_const_fn_unsafe.stderr Outdated Show resolved Hide resolved
@Centril
Copy link
Contributor

Centril commented Nov 3, 2018

Passing the review over to Ralf;

r? @RalfJung

@rust-highfive rust-highfive assigned RalfJung and unassigned Centril Nov 3, 2018
@RalfJung
Copy link
Member

RalfJung commented Nov 3, 2018

This doesn't actually allow any new operations in unsafe const fn, right? It just allows marking them as unsafe?

Unfortunately most of this is code that I am not at all familiar with, so I cannot judge the side-effects this may have. @Centril does that mean you would r+ and just want to get my opinion as well?

@oli-obk I am a bit confused why there are two places where it makes the body of an unsafe fn not behave like an unsafe block: In librustc_mir/build/mod.rs and in librustc_mir/transform/check_unsafety.rs. Why is that?

@oli-obk
Copy link
Contributor Author

oli-obk commented Nov 3, 2018

check_unsafety can actually bug! out for const fn where the body is treated as unsafe, I'll fix that.

@Centril
Copy link
Contributor

Centril commented Nov 3, 2018

@RalfJung

This doesn't actually allow any new operations in unsafe const fn, right? It just allows marking them as unsafe?

The intention is that calling const unsafe fns should now be allowed in unsafe { ... } in const unsafe? fn.

Unfortunately most of this is code that I am not at all familiar with, so I cannot judge the side-effects this may have. @Centril does that mean you would r+ and just want to get my opinion as well?

I mainly want someone who is more familiar with the compiler internals than me (e.g. you) to review the code and see that it is correct and such; but if you are not familiar with the code maybe let's pass it to a third person (e.g. eddyb) to also check it?

@oli-obk
Copy link
Contributor Author

oli-obk commented Nov 3, 2018

cc @arielb1 who wrote the original unsafety checker for MIR

@RalfJung
Copy link
Member

RalfJung commented Nov 3, 2018

if you are not familiar with the code maybe let's pass it to a third person (e.g. eddyb) to also check it?

The unsafety checker I can at least read because it works on MIR (but I wouldn't call that familiarity); the is_min_const_fn etc. stuff and the HIR things I have no idea about at all.

@rust-highfive

This comment has been minimized.

#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
pub(crate) struct NonZero<T: Freeze>(pub(crate) T);
Copy link
Member

Choose a reason for hiding this comment

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

Glad this is not exported anymore!

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 4, 2018
@bors
Copy link
Contributor

bors commented Dec 4, 2018

⌛ Testing commit cb71752 with merge 2ba23a2...

bors added a commit that referenced this pull request Dec 4, 2018
Allow calling `const unsafe fn` in `const fn` behind a feature gate

cc #55607

r? @Centril
@bors
Copy link
Contributor

bors commented Dec 4, 2018

💔 Test failed - status-travis

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-tools 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:02:24]    Compiling rustc_tsan v0.0.0 (/checkout/src/librustc_tsan)
[00:02:24]    Compiling rustc_lsan v0.0.0 (/checkout/src/librustc_lsan)
[00:02:25]    Compiling rustc_asan v0.0.0 (/checkout/src/librustc_asan)
[00:02:25]    Compiling rustc_msan v0.0.0 (/checkout/src/librustc_msan)
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     |
[00:02:34]     |
[00:02:34] 73  |                       $Ty(unsafe { NonZero(n) })
[00:02:34]     |                           ^^^^^^ unnecessary `unsafe` block
[00:02:34] ...
[00:02:34] 110 | / nonzero_integers! {
[00:02:34] 111 | |     NonZeroU8(u8);
[00:02:34] 112 | |     NonZeroU16(u16);
[00:02:34] 113 | |     NonZeroU32(u32);
[00:02:34] 116 | |     NonZeroUsize(usize);
[00:02:34] 117 | | }
[00:02:34]     | |_- in this macro invocation
[00:02:34]     |
[00:02:34]     |
[00:02:34]     = note: #[warn(unused_unsafe)] on by default
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     |
[00:02:34]     |
[00:02:34] 73  |                       $Ty(unsafe { NonZero(n) })
[00:02:34]     |                           ^^^^^^ unnecessary `unsafe` block
[00:02:34] ...
[00:02:34] 110 | / nonzero_integers! {
[00:02:34] 111 | |     NonZeroU8(u8);
[00:02:34] 112 | |     NonZeroU16(u16);
[00:02:34] 113 | |     NonZeroU32(u32);
[00:02:34] 116 | |     NonZeroUsize(usize);
[00:02:34] 117 | | }
[00:02:34]     | |_- in this macro invocation
[00:02:34] 
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     |
[00:02:34]     |
[00:02:34] 81  |                           Some($Ty(unsafe { NonZero(n) }))
[00:02:34]     |                                    ^^^^^^ unnecessary `unsafe` block
[00:02:34] ...
[00:02:34] 110 | / nonzero_integers! {
[00:02:34] 111 | |     NonZeroU8(u8);
[00:02:34] 112 | |     NonZeroU16(u16);
[00:02:34] 113 | |     NonZeroU32(u32);
[00:02:34] 116 | |     NonZeroUsize(usize);
[00:02:34] 117 | | }
[00:02:34]     | |_- in this macro invocation
[00:02:34] 
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     --> src/libcore/ptr.rs:2762:36
[00:02:34]      |
[00:02:34] 2762 |             Some(Unique { pointer: unsafe { NonZero(ptr as _) }, _marker: PhantomData })
[00:02:34]      |                                    ^^^^^^ unnecessary `unsafe` block
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     --> src/libcore/ptr.rs:2818:27
[00:02:34]      |
[00:02:34] 2818 |         Unique { pointer: unsafe { NonZero(reference as _) }, _marker: PhantomData }
[00:02:34]      |                           ^^^^^^ unnecessary `unsafe` block
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     --> src/libcore/ptr.rs:2825:27
[00:02:34]      |
[00:02:34] 2825 |         Unique { pointer: unsafe { NonZero(reference as _) }, _marker: PhantomData }
[00:02:34]      |                           ^^^^^^ unnecessary `unsafe` block
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     --> src/libcore/ptr.rs:2898:28
[00:02:34]      |
[00:02:34] 2898 |         NonNull { pointer: unsafe { NonZero(ptr as _) } }
[00:02:34]      |                            ^^^^^^ unnecessary `unsafe` block
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     --> src/libcore/ptr.rs:3028:28
[00:02:34]      |
[00:02:34] 3028 |         NonNull { pointer: unsafe { NonZero(reference as _) } }
[00:02:34]      |                            ^^^^^^ unnecessary `unsafe` block
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]     --> src/libcore/ptr.rs:3036:28
[00:02:34]      |
[00:02:34] 3036 |         NonNull { pointer: unsafe { NonZero(reference as _) } }
[00:02:34]      |                            ^^^^^^ unnecessary `unsafe` block
[00:02:34] 
[00:02:34] warning: unnecessary `unsafe` block
[00:02:34]    |
[00:02:34]    |
[00:02:34] 27 |         unsafe { NonZero(self.0) }
[00:02:34]    |         ^^^^^^ unnecessary `unsafe` block
[00:02:39] [RUSTC-TIMING] core test:false 24.101
[00:02:41] [RUSTC-TIMING] compiler_builtins test:false 1.844
[00:02:41]    Compiling libc v0.0.0 (/checkout/src/rustc/libc_shim)
[00:02:41]    Compiling alloc v0.0.0 (/checkout/src/liballoc)
---
[01:09:42] [RUSTC-TIMING] arena test:false 0.926
[01:09:42]    Compiling rustc-ap-syntax_pos v297.0.0
[01:09:44] [RUSTC-TIMING] git2_curl test:false 2.904
[01:09:44]    Compiling cargo v0.33.0 (/checkout/src/tools/cargo)
[01:09:45] error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
[01:09:45]    --> /cargo/registry/src/git.luolix.top-1ecc6299db9ec823/rustc-ap-rustc_target-297.0.0/abi/mod.rs:830:1
[01:09:45]     |
[01:09:45] 830 | / newtype_index! {
[01:09:45] 831 | |     pub struct VariantIdx { .. }
[01:09:45] 832 | | }
[01:09:45]     | |_^ initializing type with `rustc_layout_scalar_valid_range` attr
[01:09:45]     |
[01:09:45]     = note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
[01:09:45] 
[01:09:45] 
[01:09:45] error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
[01:09:45]    --> /cargo/registry/src/git.luolix.top-1ecc6299db9ec823/rustc-ap-rustc_target-297.0.0/abi/mod.rs:830:1
[01:09:45]     |
[01:09:45] 830 | / newtype_index! {
[01:09:45] 831 | |     pub struct VariantIdx { .. }
[01:09:45] 832 | | }
[01:09:45]     | |_^ initializing type with `rustc_layout_scalar_valid_range` attr
[01:09:45]     |
[01:09:45]     = note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
[01:09:45] 
[01:09:45] error: aborting due to 2 previous errors
[01:09:45] 
[01:09:45] For more information about this error, try `rustc --explain E0133`.
---
[01:12:59]    Compiling rustc-ap-arena v297.0.0
[01:12:59]    Compiling rustc-ap-rustc_target v297.0.0
[01:12:59] [RUSTC-TIMING] arena test:false 0.528
[01:12:59]    Compiling rustc-ap-syntax_pos v297.0.0
[01:13:02] error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
[01:13:02]    --> /cargo/registry/src/git.luolix.top-1ecc6299db9ec823/rustc-ap-rustc_target-297.0.0/abi/mod.rs:830:1
[01:13:02]     |
[01:13:02] 830 | / newtype_index! {
[01:13:02] 831 | |     pub struct VariantIdx { .. }
[01:13:02] 832 | | }
[01:13:02]     | |_^ initializing type with `rustc_layout_scalar_valid_range` attr
[01:13:02]     |
[01:13:02]     = note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
[01:13:02] 
[01:13:02] 
[01:13:02] error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
[01:13:02]    --> /cargo/registry/src/git.luolix.top-1ecc6299db9ec823/rustc-ap-rustc_target-297.0.0/abi/mod.rs:830:1
[01:13:02]     |
[01:13:02] 830 | / newtype_index! {
[01:13:02] 831 | |     pub struct VariantIdx { .. }
[01:13:02] 832 | | }
[01:13:02]     | |_^ initializing type with `rustc_layout_scalar_valid_range` attr
[01:13:02]     |
[01:13:02]     = note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
[01:13:02] 
[01:13:02] error: aborting due to 2 previous errors
[01:13:02] 
[01:13:02] For more information about this error, try `rustc --explain E0133`.
---
[01:16:14] failures:
[01:16:14] 
[01:16:14] ---- [compile-fail] compile-fail/validity/nonzero.rs stdout ----
[01:16:14] 
[01:16:14] error: tests/compile-fail/validity/nonzero.rs:10: unexpected error: '10:19: 10:29: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block [E0133]'
[01:16:14] 
[01:16:14] error: tests/compile-fail/validity/nonzero.rs:10: expected error not found: encountered 0, but expected something greater or equal to 1
[01:16:14] error: 1 unexpected errors found, 1 expected errors not found
[01:16:14] status: exit code: 1
[01:16:14] status: exit code: 1
[01:16:14] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools-bin/miri" "tests/compile-fail/validity/nonzero.rs" "-L" "/tmp/compiletestvu0kh1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-C" "prefer-dynamic" "-o" "/tmp/compiletestvu0kh1/validity/nonzero.stage-id" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "-Dwarnings" "-Dunused" "--edition" "2018" "-L" "/tmp/compiletestvu0kh1/validity/nonzero.stage-id.aux" "-A" "unused"
[01:16:14] unexpected errors (from JSON output): [
[01:16:14]     Error {
[01:16:14]         line_num: 10,
[01:16:14]         kind: Some(
[01:16:14]         kind: Some(
[01:16:14]             Error
[01:16:14]         ),
[01:16:14]         msg: "10:19: 10:29: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block [E0133]"
[01:16:14] ]
[01:16:14] 
[01:16:14] not found errors (from test file): [
[01:16:14]     Error {
[01:16:14]     Error {
[01:16:14]         line_num: 10,
[01:16:14]         kind: Some(
[01:16:14]             Error
[01:16:14]         ),
[01:16:14]         msg: "encountered 0, but expected something greater or equal to 1"
[01:16:14] ]
[01:16:14] 
[01:16:14] thread '[compile-fail] compile-fail/validity/nonzero.rs' panicked at 'explicit panic', /cargo/registry/src/git.luolix.top-1ecc6299db9ec823/compiletest_rs-0.3.17/src/runtest.rs:1098:13
[01:16:14] note: Run with `RUST_BACKTRACE=1` for a backtrace.
---
travis_time:end:094631be:start=1543937712944865554,finish=1543937712951008419,duration=6142865
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:00efd01b
$ 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:00a40e95
travis_time:start:00a40e95
$ 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:075b2172
$ 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)

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 4, 2018
@oli-obk
Copy link
Contributor Author

oli-obk commented Dec 4, 2018

Uh... @alexcrichton @nrc what is the correct procedure for landing something that breaks a rustc-ap-* crate needed for cargo?

@alexcrichton
Copy link
Member

@oli-obk in theory there's no procedure as we just publish a new version of rustc-ap-syntax every night, whatever the nightly happened to be released with. I've just fixed a bug where it stopped running for a week or so, so maybe that's all that was needed?

Does that help? I fear I may be missing context about what's actually needed here

@oli-obk
Copy link
Contributor Author

oli-obk commented Dec 4, 2018

I made a breaking change to librustc_data_structures, which in turn broke rustc-ap-syntax_pos.

From your statement it sounds like that's normally fine, so it might be that that in turn would have broken rls, and since we're in publishing week, we can't break tools.

I am currently investigating if I can work around it by a targeted application of #[allow_internal_unstable].

@alexcrichton
Copy link
Member

Hm that's odd because that breakage should be impossible, the rustc-ap-syntax_pos crate depend on rustc-ap-rustc_data_structures, not the in-tree copy. Is the breakage in the logs above or elsewhere?

@oli-obk
Copy link
Contributor Author

oli-obk commented Dec 4, 2018

Is the breakage in the logs above or elsewhere?

It's in the logs above.... But I think I know the issue: I changed language semantics for the forever-unstable feature rustc_layout_scalar_valid_range attributes. Using them requires unsafe now.

@alexcrichton
Copy link
Member

Ah yes indeed! If that breaks crates on crates.io that's fine, it just means that until the release happens (where we require the RLS compiles) this won't be able to land. After the release though this can land, it'll break the RLS, and then the RLS will update to a newer version of the crate on crates.io as it's naturally published.

@kennytm
Copy link
Member

kennytm commented Dec 6, 2018

@bors retry

@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 Dec 6, 2018
@bors
Copy link
Contributor

bors commented Dec 6, 2018

⌛ Testing commit cb71752 with merge 128a1fa...

bors added a commit that referenced this pull request Dec 6, 2018
Allow calling `const unsafe fn` in `const fn` behind a feature gate

cc #55607

r? @Centril
@bors
Copy link
Contributor

bors commented Dec 6, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing 128a1fa to master...

@bors bors merged commit cb71752 into rust-lang:master Dec 6, 2018
@rust-highfive
Copy link
Collaborator

📣 Toolstate changed by #55635!

Tested on commit 128a1fa.
Direct link to PR: #55635

💔 miri on windows: test-pass → test-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra).
💔 miri on linux: test-pass → test-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra).
💔 rls on windows: test-pass → build-fail (cc @nrc @Xanewok, @rust-lang/infra).
💔 rls on linux: test-pass → build-fail (cc @nrc @Xanewok, @rust-lang/infra).
💔 rustfmt on windows: test-pass → build-fail (cc @nrc, @rust-lang/infra).
💔 rustfmt on linux: test-pass → build-fail (cc @nrc, @rust-lang/infra).

rust-highfive added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Dec 6, 2018
Tested on commit rust-lang/rust@128a1fa.
Direct link to PR: <rust-lang/rust#55635>

💔 miri on windows: test-pass → test-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra).
💔 miri on linux: test-pass → test-fail (cc @oli-obk @RalfJung @eddyb, @rust-lang/infra).
💔 rls on windows: test-pass → build-fail (cc @nrc @Xanewok, @rust-lang/infra).
💔 rls on linux: test-pass → build-fail (cc @nrc @Xanewok, @rust-lang/infra).
💔 rustfmt on windows: test-pass → build-fail (cc @nrc, @rust-lang/infra).
💔 rustfmt on linux: test-pass → build-fail (cc @nrc, @rust-lang/infra).
@oli-obk oli-obk deleted the min_const_unsafe_fn branch December 6, 2018 15:03
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.