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 using fn pointers in const fn with unleash miri #64635

Merged
merged 3 commits into from
Sep 22, 2019

Conversation

gnzlbg
Copy link
Contributor

@gnzlbg gnzlbg commented Sep 20, 2019

This allows using function pointers in const fns when -Zunleash-the-miri-within-you is enabled.

If the call to the const fn happens in a const-context, the function pointer is required to point to a const fn:

fn non_const_fn() -> i32 { 42 }
const fn const_fn() -> i32 { 42 }
const fn foo(x: fn() -> i32) -> i32 { x() }

let x: i32 = foo(non_const_fn_ptr); // OK
let y: i32 = foo(const_fn_ptr); // OK
const X: i32 = foo(non_const_fn_ptr); // ERROR: `non_const_fn` is not `const fn`
const Y: i32 = foo(const_fn_ptr); // OK

r? @oli-obk

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 20, 2019
@gnzlbg
Copy link
Contributor Author

gnzlbg commented Sep 20, 2019

@oli-obk so surprisingly this works, and the feature gate works as well, but when the feature is activated I think we should diagnose some issues (e.g. using non-const fn ptrs) in the function definitions as opposed to just erroring with a const-eval when that function gets called later.

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed (pretty log, 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.
2019-09-20T16:41:57.1984412Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-09-20T16:41:57.2166573Z ##[command]git config gc.auto 0
2019-09-20T16:41:57.2238783Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-09-20T16:41:57.2288399Z ##[command]git config --get-all http.proxy
2019-09-20T16:41:57.2440528Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/64635/merge:refs/remotes/pull/64635/merge
---
2019-09-20T17:43:16.1417668Z .................................................................................................... 1500/9030
2019-09-20T17:43:21.9367603Z ...........................................................F........................................ 1600/9030
2019-09-20T17:43:34.0933494Z ......................................................................i...............i............. 1700/9030
2019-09-20T17:43:40.6289763Z .................................................................................................... 1800/9030
2019-09-20T17:43:54.2747004Z .............................................................iiiii.................................. 1900/9030
2019-09-20T17:44:07.5589711Z .................................................................................................... 2100/9030
2019-09-20T17:44:10.0775069Z .................................................................................................... 2200/9030
2019-09-20T17:44:13.3020469Z .................................................................................................... 2300/9030
2019-09-20T17:44:21.5459744Z .................................................................................................... 2400/9030
---
2019-09-20T17:47:14.7060311Z .................................................i...............i.................................. 4700/9030
2019-09-20T17:47:23.8104621Z .................................................................................................... 4800/9030
2019-09-20T17:47:31.7622232Z .................................................................................................... 4900/9030
2019-09-20T17:47:41.1849751Z .................................................................................................... 5000/9030
2019-09-20T17:47:48.9015932Z .................................ii.ii.............................................................. 5100/9030
2019-09-20T17:47:58.3894168Z .................................................................................................... 5300/9030
2019-09-20T17:48:08.7609603Z .................................................................................................i.. 5400/9030
2019-09-20T17:48:17.0754583Z .................................................................................................... 5500/9030
2019-09-20T17:48:21.8524367Z .................................................................................................... 5600/9030
2019-09-20T17:48:21.8524367Z .................................................................................................... 5600/9030
2019-09-20T17:48:32.3433463Z ............................................................................................ii...i.. 5700/9030
2019-09-20T17:48:46.3062320Z ii...........i...................................................................................... 5800/9030
2019-09-20T17:49:06.3717148Z .................................................................................................... 6000/9030
2019-09-20T17:49:06.3717148Z .................................................................................................... 6000/9030
2019-09-20T17:49:12.3751457Z ..............................................................................................i..ii. 6100/9030
2019-09-20T17:49:39.5963111Z .................................................................................................... 6300/9030
2019-09-20T17:49:44.2282900Z .....................................................i.............................................. 6400/9030
2019-09-20T17:49:46.3729854Z .................................................................................................... 6500/9030
2019-09-20T17:49:48.7862846Z .........................i.......................................................................... 6600/9030
---
2019-09-20T17:53:49.5804350Z failures:
2019-09-20T17:53:49.5845557Z 
2019-09-20T17:53:49.5846272Z ---- [ui] ui/consts/const-eval/const_fn_ptr_fail2.rs stdout ----
2019-09-20T17:53:49.5846520Z 
2019-09-20T17:53:49.5847009Z error: /checkout/src/test/ui/consts/const-eval/const_fn_ptr_fail2.rs:8: unexpected error: '8:5: 8:9: any use of this value will cause an error [const_err]'
2019-09-20T17:53:49.5847193Z 
2019-09-20T17:53:49.5847672Z error: /checkout/src/test/ui/consts/const-eval/const_fn_ptr_fail2.rs:8: unexpected error: '8:5: 8:9: any use of this value will cause an error [const_err]'
2019-09-20T17:53:49.5847848Z 
2019-09-20T17:53:49.5848279Z error: /checkout/src/test/ui/consts/const-eval/const_fn_ptr_fail2.rs:11: expected error not found: any use of this value will cause an error
2019-09-20T17:53:49.5848496Z 
2019-09-20T17:53:49.5848936Z error: /checkout/src/test/ui/consts/const-eval/const_fn_ptr_fail2.rs:14: expected error not found: any use of this value will cause an error
2019-09-20T17:53:49.5849288Z error: 2 unexpected errors found, 2 expected errors not found
2019-09-20T17:53:49.5849435Z status: exit code: 1
2019-09-20T17:53:49.5849435Z status: exit code: 1
2019-09-20T17:53:49.5850296Z command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/consts/const-eval/const_fn_ptr_fail2.rs" "-Zthreads=1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/const-eval/const_fn_ptr_fail2" "-Crpath" "-O" "-Cdebuginfo=0" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/const-eval/const_fn_ptr_fail2/auxiliary" "-A" "unused"
2019-09-20T17:53:49.5851472Z unexpected errors (from JSON output): [
2019-09-20T17:53:49.5851721Z     Error {
2019-09-20T17:53:49.5851894Z         line_num: 8,
2019-09-20T17:53:49.5852072Z         kind: Some(
2019-09-20T17:53:49.5852399Z         ),
2019-09-20T17:53:49.5852399Z         ),
2019-09-20T17:53:49.5852583Z         msg: "8:5: 8:9: any use of this value will cause an error [const_err]",
2019-09-20T17:53:49.5852915Z     Error {
2019-09-20T17:53:49.5853106Z         line_num: 8,
2019-09-20T17:53:49.5853268Z         kind: Some(
2019-09-20T17:53:49.5853427Z             Error,
2019-09-20T17:53:49.5853427Z             Error,
2019-09-20T17:53:49.5853608Z         ),
2019-09-20T17:53:49.5853777Z         msg: "8:5: 8:9: any use of this value will cause an error [const_err]",
2019-09-20T17:53:49.5854239Z ]
2019-09-20T17:53:49.5854480Z 
2019-09-20T17:53:49.5854624Z not found errors (from test file): [
2019-09-20T17:53:49.5854778Z     Error {
2019-09-20T17:53:49.5854778Z     Error {
2019-09-20T17:53:49.5854919Z         line_num: 11,
2019-09-20T17:53:49.5855072Z         kind: Some(
2019-09-20T17:53:49.5855236Z             Error,
2019-09-20T17:53:49.5855376Z         ),
2019-09-20T17:53:49.5855518Z         msg: "any use of this value will cause an error",
2019-09-20T17:53:49.5855816Z     Error {
2019-09-20T17:53:49.5855953Z         line_num: 14,
2019-09-20T17:53:49.5856108Z         kind: Some(
2019-09-20T17:53:49.5856247Z             Error,
2019-09-20T17:53:49.5856247Z             Error,
2019-09-20T17:53:49.5856385Z         ),
2019-09-20T17:53:49.5856545Z         msg: "any use of this value will cause an error",
2019-09-20T17:53:49.5856818Z ]
2019-09-20T17:53:49.5856959Z 
2019-09-20T17:53:49.5857458Z thread '[ui] ui/consts/const-eval/const_fn_ptr_fail2.rs' panicked at 'explicit panic', src/tools/compiletest/src/runtest.rs:1512:13
2019-09-20T17:53:49.5857674Z note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
2019-09-20T17:53:49.5857674Z note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
2019-09-20T17:53:49.5857834Z 
2019-09-20T17:53:49.5858193Z ---- [ui] ui/consts/issue-56164.rs stdout ----
2019-09-20T17:53:49.5858609Z diff of stderr:
2019-09-20T17:53:49.5858856Z 
2019-09-20T17:53:49.5859043Z 4 LL | const fn foo() { (||{})() }
2019-09-20T17:53:49.5859353Z 6 
2019-09-20T17:53:49.5859761Z - error: function pointers are not allowed in const fn
2019-09-20T17:53:49.5859960Z + error[E0658]: function pointers in const fn are unstable
2019-09-20T17:53:49.5860445Z 8   --> $DIR/issue-56164.rs:8:5
2019-09-20T17:53:49.5860445Z 8   --> $DIR/issue-56164.rs:8:5
2019-09-20T17:53:49.5861052Z 9    |
2019-09-20T17:53:49.5861256Z 10 LL |     input()
2019-09-20T17:53:49.5861430Z 
2019-09-20T17:53:49.5861592Z 11    |     ^^^^^^^
2019-09-20T17:53:49.5861747Z +    |
2019-09-20T17:53:49.5862395Z +    = note: for more information, see ***/issues/51909
2019-09-20T17:53:49.5862824Z 12 
2019-09-20T17:53:49.5862993Z 13 error: aborting due to 2 previous errors
2019-09-20T17:53:49.5863152Z 14 
2019-09-20T17:53:49.5863327Z 
---
2019-09-20T17:53:49.5864890Z 
2019-09-20T17:53:49.5865013Z 
2019-09-20T17:53:49.5865177Z The actual stderr differed from the expected stderr.
2019-09-20T17:53:49.5865596Z Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/issue-56164/issue-56164.stderr
2019-09-20T17:53:49.5866010Z To update references, rerun the tests and pass the `--bless` flag
2019-09-20T17:53:49.5866466Z To only update this specific test, also pass `--test-args consts/issue-56164.rs`
2019-09-20T17:53:49.5866817Z error: 1 errors occurred comparing output.
2019-09-20T17:53:49.5866974Z status: exit code: 1
2019-09-20T17:53:49.5866974Z status: exit code: 1
2019-09-20T17:53:49.5867781Z command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/consts/issue-56164.rs" "-Zthreads=1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/issue-56164" "-Crpath" "-O" "-Cdebuginfo=0" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/issue-56164/auxiliary" "-A" "unused"
2019-09-20T17:53:49.5868415Z ------------------------------------------
2019-09-20T17:53:49.5868623Z 
2019-09-20T17:53:49.5869000Z ------------------------------------------
2019-09-20T17:53:49.5869190Z stderr:
2019-09-20T17:53:49.5869190Z stderr:
2019-09-20T17:53:49.5869697Z ------------------------------------------
2019-09-20T17:53:49.5869908Z error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2019-09-20T17:53:49.5870432Z   --> /checkout/src/test/ui/consts/issue-56164.rs:3:18
2019-09-20T17:53:49.5871169Z    |
2019-09-20T17:53:49.5871364Z LL | const fn foo() { (||{})() }
2019-09-20T17:53:49.5871818Z 
2019-09-20T17:53:49.5872045Z error[E0658]: function pointers in const fn are unstable
2019-09-20T17:53:49.5872619Z   --> /checkout/src/test/ui/consts/issue-56164.rs:8:5
2019-09-20T17:53:49.5873794Z    |
2019-09-20T17:53:49.5873794Z    |
2019-09-20T17:53:49.5873860Z LL |     input()
2019-09-20T17:53:49.5873902Z    |     ^^^^^^^
2019-09-20T17:53:49.5874067Z    |
2019-09-20T17:53:49.5874566Z    = note: for more information, see ***/issues/51909
2019-09-20T17:53:49.5874648Z 
2019-09-20T17:53:49.5874686Z error: aborting due to 2 previous errors
2019-09-20T17:53:49.5874725Z 
2019-09-20T17:53:49.5874764Z Some errors have detailed explanations: E0015, E0658.
---
2019-09-20T17:53:49.5875795Z diff of stderr:
2019-09-20T17:53:49.5875819Z 
2019-09-20T17:53:49.5875866Z 8    |                                                                help: use parentheses to call this function: `std::mem::transmute(...)`
2019-09-20T17:53:49.5875927Z 9    |
2019-09-20T17:53:49.5876155Z 10    = note: expected type `unsafe extern "rust-intrinsic" fn(isize) -> usize`
2019-09-20T17:53:49.5876402Z -               found type `unsafe extern "rust-intrinsic" fn(_) -> _ {std::intrinsics::transmute::<_, _>}`
2019-09-20T17:53:49.5876659Z +               found type `unsafe extern "rust-intrinsic" fn(_) -> _ {std::mem::transmute::<_, _>}`
2019-09-20T17:53:49.5876703Z 12 
2019-09-20T17:53:49.5877318Z - error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {std::intrinsics::transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
2019-09-20T17:53:49.5877655Z + error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {std::mem::transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
2019-09-20T17:53:49.5877852Z 14   --> $DIR/reify-intrinsic.rs:11:13
2019-09-20T17:53:49.5877891Z 15    |
2019-09-20T17:53:49.5878129Z 16 LL |     let _ = std::mem::transmute as unsafe extern "rust-intrinsic" fn(isize) -> usize;
2019-09-20T17:53:49.5878181Z 
2019-09-20T17:53:49.5878219Z The actual stderr differed from the expected stderr.
2019-09-20T17:53:49.5878484Z Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reify-intrinsic/reify-intrinsic.stderr
2019-09-20T17:53:49.5878484Z Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reify-intrinsic/reify-intrinsic.stderr
2019-09-20T17:53:49.5878696Z To update references, rerun the tests and pass the `--bless` flag
2019-09-20T17:53:49.5878912Z To only update this specific test, also pass `--test-args reify-intrinsic.rs`
2019-09-20T17:53:49.5879005Z error: 1 errors occurred comparing output.
2019-09-20T17:53:49.5879044Z status: exit code: 1
2019-09-20T17:53:49.5879044Z status: exit code: 1
2019-09-20T17:53:49.5879663Z command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/reify-intrinsic.rs" "-Zthreads=1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reify-intrinsic" "-Crpath" "-O" "-Cdebuginfo=0" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/reify-intrinsic/auxiliary" "-A" "unused"
2019-09-20T17:53:49.5879927Z ------------------------------------------
2019-09-20T17:53:49.5879969Z 
2019-09-20T17:53:49.5880159Z ------------------------------------------
2019-09-20T17:53:49.5880197Z stderr:
2019-09-20T17:53:49.5880197Z stderr:
2019-09-20T17:53:49.5880514Z ------------------------------------------
2019-09-20T17:53:49.5880576Z error[E0308]: cannot coerce intrinsics to function pointers
2019-09-20T17:53:49.5881255Z   --> /checkout/src/test/ui/reify-intrinsic.rs:6:64
2019-09-20T17:53:49.5881327Z    |
2019-09-20T17:53:49.5881588Z LL |     let _: unsafe extern "rust-intrinsic" fn(isize) -> usize = std::mem::transmute;
2019-09-20T17:53:49.5881716Z    |                                                                |
2019-09-20T17:53:49.5881773Z    |                                                                cannot coerce intrinsics to function pointers
2019-09-20T17:53:49.5881839Z    |                                                                help: use parentheses to call this function: `std::mem::transmute(...)`
2019-09-20T17:53:49.5881907Z    |
2019-09-20T17:53:49.5881907Z    |
2019-09-20T17:53:49.5882155Z    = note: expected type `unsafe extern "rust-intrinsic" fn(isize) -> usize`
2019-09-20T17:53:49.5886092Z               found type `unsafe extern "rust-intrinsic" fn(_) -> _ {std::mem::transmute::<_, _>}`
2019-09-20T17:53:49.5886170Z 
2019-09-20T17:53:49.5886502Z error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {std::mem::transmute::<_, _>}` as `unsafe extern "rust-intrinsic" fn(isize) -> usize` is invalid
2019-09-20T17:53:49.5886781Z    |
2019-09-20T17:53:49.5886781Z    |
2019-09-20T17:53:49.5887008Z LL |     let _ = std::mem::transmute as unsafe extern "rust-intrinsic" fn(isize) -> usize;
2019-09-20T17:53:49.5887099Z 
2019-09-20T17:53:49.5887135Z error: aborting due to 2 previous errors
2019-09-20T17:53:49.5887160Z 
2019-09-20T17:53:49.5887199Z Some errors have detailed explanations: E0308, E0606.
---
2019-09-20T17:53:49.5888652Z 
2019-09-20T17:53:49.5889576Z thread 'main' panicked at 'Some tests failed', src/tools/compiletest/src/main.rs:536:22
2019-09-20T17:53:49.5902706Z 
2019-09-20T17:53:49.5902873Z 
2019-09-20T17:53:49.5904646Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/src/test/ui" "--build-base" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui" "--stage-id" "stage2-x86_64-unknown-linux-gnu" "--mode" "ui" "--target" "x86_64-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/usr/lib/llvm-6.0/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--target-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python2.7" "--lldb-python" "/usr/bin/python2.7" "--gdb" "/usr/bin/gdb" "--quiet" "--llvm-version" "6.0.0\n" "--system-llvm" "--cc" "" "--cxx" "" "--cflags" "" "--llvm-components" "" "--llvm-cxxflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"
2019-09-20T17:53:49.5904898Z 
2019-09-20T17:53:49.5904926Z 
2019-09-20T17:53:49.5908823Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
2019-09-20T17:53:49.5908908Z Build completed unsuccessfully in 1:04:41
2019-09-20T17:53:49.5908908Z Build completed unsuccessfully in 1:04:41
2019-09-20T17:53:49.5962565Z == clock drift check ==
2019-09-20T17:53:49.5977916Z   local time: Fri Sep 20 17:53:49 UTC 2019
2019-09-20T17:53:49.7565995Z   network time: Fri, 20 Sep 2019 17:53:49 GMT
2019-09-20T17:53:49.7571388Z == end clock drift check ==
2019-09-20T17:53:50.5269753Z ##[error]Bash exited with code '1'.
2019-09-20T17:53:50.5308346Z ##[section]Starting: Checkout
2019-09-20T17:53:50.5310107Z ==============================================================================
2019-09-20T17:53:50.5310157Z Task         : Get sources
2019-09-20T17:53:50.5310198Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

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)

@Centril

This comment has been minimized.

@gnzlbg

This comment has been minimized.

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.

Would be good to add more tests in higher order positions and whatnot to check variances.

@@ -0,0 +1,20 @@
warning: function is never used: `double`
Copy link
Contributor

Choose a reason for hiding this comment

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

The dead_code warnings are off-topic and shouldn't be here.

x(y)
}

const Y: usize = bar(X, 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

This and Z should not type-check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the same issue mentioned in the OP and here and should be fixed by fixing that as well.

src/libsyntax/feature_gate/active.rs Outdated Show resolved Hide resolved
src/librustc_mir/transform/qualify_consts.rs Show resolved Hide resolved
sym::const_fn_ptr,
self.span,
GateIssue::Language,
"function pointers in const fn are unstable",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"function pointers in const fn are unstable",
"function pointers in `const fn` are unstable",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other errors do not follow this style. cc @oli-obk

Copy link
Contributor

Choose a reason for hiding this comment

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

The other errors are wrong then.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, but I'd rather not touch them before #64470 is merged

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that PR also enables function pointer calling behind unleash

const X: fn(usize) -> usize = double;

const fn bar(x: usize) -> usize {
X(x)
Copy link
Contributor Author

@gnzlbg gnzlbg Sep 21, 2019

Choose a reason for hiding this comment

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

@oli-obk this is the issue that we chatted about. Note that bar can be called at run-time, but it will always fail with a constant evaluation error when called in a const context because it tries to call a non-const fn.

I'm not sure how to generate an error in this case. Right now it generates a constant-evaluation error when used in const-context, but it would be nice to generate a better error message.

Copy link
Contributor

Choose a reason for hiding this comment

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

XD welcome to RFC territory. This needs an RFC, because we need surface language changes in order to differentiate between callable and not callable function pointers.

This is what I meant by "we can do it behind a feature gate for experimentation, but we'll never be able to stabilize it.". For all intents and purposes, this is const unsound just like casting pointers to usize or comparing pointers. The pointer ops have been made unsafe for this reason, so maybe we make this unsafe, too? Idk. Seems all fishy without an RFC but as an experimental unstable feature seems fine

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 I've updated the OP with a description of the semantics that this PR implements. I'm not sure why this would be const unsound. The const fn taking pointers should be parametric over the pointer constness, and we should be able to reject that at type checking time without new syntax AFAICT.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, that would be one way to do it, it would mean you can't pass any function pointers, because there are no const function pointers. Not compiling is the current semantics and is totally sound. This is all discussed in my RFC and needs its own RFC. I'm 🤧 right now and my brain is mush. I'd prefer not to write out an explanation right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I see what you mean now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure if we need language syntax for this initially, e.g., we could just add a type that users cannot write, add coercions to it from const fns, and then do the const parametricity part. But that's 99% of the work, and at that point not allowing people to write down the type feels like unnecessary.

Either way, the intent of this PR was never to go through all that trouble.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Sep 21, 2019

@Centril I've updated the OP with a description of the feature - this WIP was intended only for @oli-obk because I got stuck trying to produce better error messages.

self.span,
&format!("function pointers are not allowed in const fn"));
err.emit();
if !(unleash_miri || const_fn_ptr) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the feature is inherently broken, maybe just do the unleash part and no feature gate?

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed (pretty log, 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.
2019-09-21T11:31:52.6200433Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-09-21T11:31:52.6392441Z ##[command]git config gc.auto 0
2019-09-21T11:31:53.1606103Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-09-21T11:31:53.1612588Z ##[command]git config --get-all http.proxy
2019-09-21T11:31:53.1618779Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/64635/merge:refs/remotes/pull/64635/merge
---
2019-09-21T11:38:59.5847844Z     Finished release [optimized] target(s) in 1m 23s
2019-09-21T11:38:59.5935408Z tidy check
2019-09-21T11:39:00.4488759Z * 578 error codes
2019-09-21T11:39:00.4488879Z * highest error code: E0733
2019-09-21T11:39:00.4821032Z tidy error: /checkout/src/libsyntax/feature_gate/active.rs:412: feature const_fn_union is not sorted by since
2019-09-21T11:39:01.4035625Z some tidy checks failed
2019-09-21T11:39:01.4041598Z 
2019-09-21T11:39:01.4041598Z 
2019-09-21T11:39:01.4042468Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/tidy" "/checkout/src" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "--no-vendor"
2019-09-21T11:39:01.4043148Z 
2019-09-21T11:39:01.4043174Z 
2019-09-21T11:39:01.4049308Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
2019-09-21T11:39:01.4049600Z Build completed unsuccessfully in 0:01:26
2019-09-21T11:39:01.4049600Z Build completed unsuccessfully in 0:01:26
2019-09-21T11:39:01.4094993Z == clock drift check ==
2019-09-21T11:39:01.4111294Z   local time: Sat Sep 21 11:39:01 UTC 2019
2019-09-21T11:39:01.5603227Z   network time: Sat, 21 Sep 2019 11:39:01 GMT
2019-09-21T11:39:01.5607225Z == end clock drift check ==
2019-09-21T11:39:03.2497270Z ##[error]Bash exited with code '1'.
2019-09-21T11:39:03.2539979Z ##[section]Starting: Checkout
2019-09-21T11:39:03.2541459Z ==============================================================================
2019-09-21T11:39:03.2541504Z Task         : Get sources
2019-09-21T11:39:03.2541555Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

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)

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Sep 21, 2019

So I will remove the feature gate, and only allow this behind the unleash miri part.

@gnzlbg gnzlbg changed the title [WIP] Allow using fn pointers in const fn behind const_fn_ptr gate Allow using fn pointers in const fn with unleash miri Sep 21, 2019
@gnzlbg
Copy link
Contributor Author

gnzlbg commented Sep 21, 2019

I've done that.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 21, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Sep 21, 2019

📌 Commit d434496 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 Sep 21, 2019
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed (pretty log, 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.
2019-09-21T14:11:38.1235527Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-09-21T14:11:38.9593473Z ##[command]git config gc.auto 0
2019-09-21T14:11:38.9597725Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-09-21T14:11:38.9601751Z ##[command]git config --get-all http.proxy
2019-09-21T14:11:38.9603961Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/64635/merge:refs/remotes/pull/64635/merge
---
2019-09-21T15:09:03.5951897Z .................................................................................................... 1500/9032
2019-09-21T15:09:09.3389633Z .............................................................F...................................... 1600/9032
2019-09-21T15:09:20.9689737Z ........................................................................i...............i........... 1700/9032
2019-09-21T15:09:27.6197146Z .................................................................................................... 1800/9032
2019-09-21T15:09:36.1118312Z ...............................................................iiiii................................ 1900/9032
2019-09-21T15:09:54.9926271Z .................................................................................................... 2100/9032
2019-09-21T15:09:57.3656017Z .................................................................................................... 2200/9032
2019-09-21T15:10:00.3858530Z .................................................................................................... 2300/9032
2019-09-21T15:10:08.3514309Z .................................................................................................... 2400/9032
---
2019-09-21T15:12:55.6050531Z ...................................................i...............i................................ 4700/9032
2019-09-21T15:13:04.2341730Z .................................................................................................... 4800/9032
2019-09-21T15:13:12.0352699Z .................................................................................................... 4900/9032
2019-09-21T15:13:20.3891387Z .................................................................................................... 5000/9032
2019-09-21T15:13:28.2671405Z ...................................ii.ii............................................................ 5100/9032
2019-09-21T15:13:37.4095273Z .................................................................................................... 5300/9032
2019-09-21T15:13:47.1746715Z ...................................................................................................i 5400/9032
2019-09-21T15:13:54.8393295Z .................................................................................................... 5500/9032
2019-09-21T15:13:59.3840236Z .................................................................................................... 5600/9032
2019-09-21T15:13:59.3840236Z .................................................................................................... 5600/9032
2019-09-21T15:14:09.7613901Z ..............................................................................................ii...i 5700/9032
2019-09-21T15:14:22.8438903Z ..ii............i................................................................................... 5800/9032
2019-09-21T15:14:42.5655448Z .................................................................................................... 6000/9032
2019-09-21T15:14:48.5637702Z ................................................................................................i..i 6100/9032
2019-09-21T15:15:01.9800903Z i................................................................................................... 6200/9032
2019-09-21T15:15:12.0695107Z .................................................................................................... 6300/9032
---
2019-09-21T15:19:15.6734594Z 
2019-09-21T15:19:15.6735129Z ---- [ui] ui/consts/issue-56164.rs stdout ----
2019-09-21T15:19:15.6735475Z diff of stderr:
2019-09-21T15:19:15.6735529Z 
2019-09-21T15:19:15.6735571Z 4 LL | const fn foo() { (||{})() }
2019-09-21T15:19:15.6735672Z 6 
2019-09-21T15:19:15.6736178Z - error: function pointers are not allowed in const fn
2019-09-21T15:19:15.6736251Z + error: function pointers in `const fn` are unstable
2019-09-21T15:19:15.6736454Z 8   --> $DIR/issue-56164.rs:8:5
2019-09-21T15:19:15.6736454Z 8   --> $DIR/issue-56164.rs:8:5
2019-09-21T15:19:15.6736491Z 9    |
2019-09-21T15:19:15.6736542Z 10 LL |     input()
2019-09-21T15:19:15.6736565Z 
2019-09-21T15:19:15.6736585Z 
2019-09-21T15:19:15.6736621Z The actual stderr differed from the expected stderr.
2019-09-21T15:19:15.6736871Z Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/issue-56164/issue-56164.stderr
2019-09-21T15:19:15.6737078Z To update references, rerun the tests and pass the `--bless` flag
2019-09-21T15:19:15.6737287Z To only update this specific test, also pass `--test-args consts/issue-56164.rs`
2019-09-21T15:19:15.6737367Z error: 1 errors occurred comparing output.
2019-09-21T15:19:15.6737402Z status: exit code: 1
2019-09-21T15:19:15.6737402Z status: exit code: 1
2019-09-21T15:19:15.6737984Z command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/consts/issue-56164.rs" "-Zthreads=1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/issue-56164" "-Crpath" "-O" "-Cdebuginfo=0" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/consts/issue-56164/auxiliary" "-A" "unused"
2019-09-21T15:19:15.6738262Z ------------------------------------------
2019-09-21T15:19:15.6738290Z 
2019-09-21T15:19:15.6738462Z ------------------------------------------
2019-09-21T15:19:15.6738499Z stderr:
2019-09-21T15:19:15.6738499Z stderr:
2019-09-21T15:19:15.6738736Z ------------------------------------------
2019-09-21T15:19:15.6738781Z error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2019-09-21T15:19:15.6738987Z   --> /checkout/src/test/ui/consts/issue-56164.rs:3:18
2019-09-21T15:19:15.6739239Z    |
2019-09-21T15:19:15.6739273Z LL | const fn foo() { (||{})() }
2019-09-21T15:19:15.6739348Z 
2019-09-21T15:19:15.6739385Z error: function pointers in `const fn` are unstable
2019-09-21T15:19:15.6739791Z   --> /checkout/src/test/ui/consts/issue-56164.rs:8:5
2019-09-21T15:19:15.6739847Z    |
---
2019-09-21T15:19:15.6775593Z thread 'main' panicked at 'Some tests failed', src/tools/compiletest/src/main.rs:536:22
2019-09-21T15:19:15.6776045Z note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
2019-09-21T15:19:15.6791775Z 
2019-09-21T15:19:15.6792469Z 
2019-09-21T15:19:15.6795456Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/src/test/ui" "--build-base" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui" "--stage-id" "stage2-x86_64-unknown-linux-gnu" "--mode" "ui" "--target" "x86_64-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/usr/lib/llvm-6.0/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--target-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python2.7" "--lldb-python" "/usr/bin/python2.7" "--gdb" "/usr/bin/gdb" "--quiet" "--llvm-version" "6.0.0\n" "--system-llvm" "--cc" "" "--cxx" "" "--cflags" "" "--llvm-components" "" "--llvm-cxxflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"
2019-09-21T15:19:15.6796010Z 
2019-09-21T15:19:15.6796154Z 
2019-09-21T15:19:15.6799093Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
2019-09-21T15:19:15.6799322Z Build completed unsuccessfully in 1:00:57
2019-09-21T15:19:15.6799322Z Build completed unsuccessfully in 1:00:57
2019-09-21T15:19:15.6848923Z == clock drift check ==
2019-09-21T15:19:15.6886592Z   local time: Sat Sep 21 15:19:15 UTC 2019
2019-09-21T15:19:15.8895256Z   network time: Sat, 21 Sep 2019 15:19:15 GMT
2019-09-21T15:19:15.8896918Z == end clock drift check ==
2019-09-21T15:19:16.8024619Z ##[error]Bash exited with code '1'.
2019-09-21T15:19:16.8057654Z ##[section]Starting: Checkout
2019-09-21T15:19:16.8059229Z ==============================================================================
2019-09-21T15:19:16.8059273Z Task         : Get sources
2019-09-21T15:19:16.8059325Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

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)

@Centril
Copy link
Contributor

Centril commented Sep 21, 2019

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 21, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Sep 21, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Sep 21, 2019

📌 Commit 9d4053f 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 21, 2019
Centril added a commit to Centril/rust that referenced this pull request Sep 21, 2019
Allow using fn pointers in const fn with unleash miri

This allows using function pointers in const fns  when `-Zunleash-the-miri-within-you` is enabled.

If the call to the `const fn` happens in a `const`-context, the function pointer is required to point to a `const fn`:

```rust
fn non_const_fn() -> i32 { 42 }
const fn const_fn() -> i32 { 42 }
const fn foo(x: fn() -> i32) -> i32 { x() }

let x: i32 = foo(non_const_fn_ptr); // OK
let y: i32 = foo(const_fn_ptr); // OK
const X: i32 = foo(non_const_fn_ptr); // ERROR: `non_const_fn` is not `const fn`
const Y: i32 = foo(const_fn_ptr); // OK
```

r? @oli-obk
bors added a commit that referenced this pull request Sep 22, 2019
Rollup of 9 pull requests

Successful merges:

 - #63907 (Add explanation to type mismatch involving type params and assoc types)
 - #64615 (rustbuild: Turn down compression on exe installers)
 - #64617 (rustbuild: Turn down compression on msi installers)
 - #64618 (rustbuild: Improve output of `dist` step)
 - #64619 (Fixes #63962. Hint about missing tuple parentheses in patterns)
 - #64634 (Update to LLVM 9.0.0)
 - #64635 (Allow using fn pointers in const fn with unleash miri)
 - #64660 (unify errors for tuple/struct variants)
 - #64664 (fully remove AstBuilder)

Failed merges:

r? @ghost
@bors bors merged commit 9d4053f into rust-lang:master Sep 22, 2019
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.

None yet

5 participants