Skip to content

CTFE: use binary_op to compare integer with match disriminant #52400

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

Merged
merged 4 commits into from
Jul 15, 2018

Conversation

RalfJung
Copy link
Member

This is needed to unblock rust-lang/miri#401: There is code in the Windows initialization functions that uses match to test whether a pointer is NULL.

I will add a testcase in miri; I was not sure where to add a testcase in Rust itself.

r? @oli-obk

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

oli-obk commented Jul 15, 2018

Test case inside rustc:

    let _: [u8; 0] = [4; {
        match &1 as *const i32 as usize {
            0 => 42, // should not have "a raw memory access tried to access part of a pointer value as raw bytes" here, any other error is expected and fine
            n => n,
        } 
    }];

@RalfJung RalfJung mentioned this pull request Jul 15, 2018
@RalfJung
Copy link
Member Author

RalfJung commented Jul 15, 2018

That says "error[E0018]: raw pointers cannot be cast to integers in constants" and "error[E0019]: constant contains unimplemented expression type".

@oli-obk
Copy link
Contributor

oli-obk commented Jul 15, 2018

but on master it additionally says "a raw memory access tried to access part of a pointer value as raw bytes", on the 0 =>, which in your branch it won't.

@RalfJung
Copy link
Member Author

Done.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 15, 2018

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 15, 2018

📌 Commit 5be7414 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 Jul 15, 2018
@rust-highfive
Copy link
Contributor

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:03:34] travis_fold:start:tidy
travis_time:start:tidy
tidy check
[00:03:34] tidy error: /checkout/src/test/ui/const-eval/match-test-ptr-null.rs: incorrect license
[00:03:35] some tidy checks failed
[00:03:35] 
[00:03:35] 
[00:03:35] 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" "--quiet"
[00:03:35] 
[00:03:35] 
[00:03:35] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
[00:03:35] Build completed unsuccessfully in 0:00:46
[00:03:35] Build completed unsuccessfully in 0:00:46
[00:03:35] Makefile:79: recipe for target 'tidy' failed
[00:03:35] make: *** [tidy] Error 1

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:05befec5
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
travis_time:end:298f9b88:start=1531656164750353296,finish=1531656164758583704,duration=8230408
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:05cfc4c0
$ 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 -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:05e5e016
travis_time:start:05e5e016
$ 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:0ad6e0da
$ 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 Jul 15, 2018

@bors r- missing licence text

@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 Jul 15, 2018
@RalfJung
Copy link
Member Author

oops added!

@oli-obk
Copy link
Contributor

oli-obk commented Jul 15, 2018

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 15, 2018

📌 Commit 03b9020 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 Jul 15, 2018
@bors
Copy link
Collaborator

bors commented Jul 15, 2018

⌛ Testing commit 03b9020 with merge f4d040dce092c0896b8d075b742a84799014141b...

@bors
Copy link
Collaborator

bors commented Jul 15, 2018

💔 Test failed - status-appveyor

@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 Jul 15, 2018
@rust-highfive
Copy link
Contributor

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.
Check compiletest suite=ui mode=ui (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
[00:45:48] 
[00:45:48] running 1568 tests
[00:45:51] ..................................................................................................i.
[00:45:55] ............................................F...................i...................................
[00:46:00] ....................................................................................................
[00:46:03] ....................................................................................................
[00:46:05] ....................................................................................................
[00:46:08] ....................................................................................................
---
[00:46:36] .................................................................................i..................
[00:46:39] ....................................................................
[00:46:39] failures:
[00:46:39] 
[00:46:39] ---- [ui] ui/const-eval/match-test-ptr-null.rs stdout ----
[00:46:39] 
[00:46:39] 
[00:46:39] 1 error[E0018]: raw pointers cannot be cast to integers in constants
[00:46:39] -   --> $DIR/match-test-ptr-null.rs:6:15
[00:46:39] +   --> $DIR/match-test-ptr-null.rs:16:15
[00:46:39] 3    |
[00:46:39] 4 LL |         match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers
[00:46:39] 
[00:46:39] 6 
[00:46:39] 7 error[E0019]: constant contains unimplemented expression type
[00:46:39] 7 error[E0019]: constant contains unimplemented expression type
[00:46:39] -   --> $DIR/match-test-ptr-null.rs:7:13
[00:46:39] +   --> $DIR/match-test-ptr-null.rs:17:13
[00:46:39] 9    |
[00:46:39] 10 LL |             0 => 42, //~ ERROR constant contains unimplemented expression type
[00:46:39] 
[00:46:39] 12 
[00:46:39] 13 error[E0080]: could not evaluate repeat length
[00:46:39] 13 error[E0080]: could not evaluate repeat length
[00:46:39] -   --> $DIR/match-test-ptr-null.rs:5:26
[00:46:39] +   --> $DIR/match-test-ptr-null.rs:15:26
[00:46:39] 15    |
[00:46:39] 16 LL |       let _: [u8; 0] = [4; { //~ ERROR could not evaluate repeat length
[00:46:39] 
[00:46:39] 
[00:46:39] The actual stderr differed from the expected stderr.
[00:46:39] The actual stderr differed from the expected stderr.
[00:46:39] Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/const-eval/match-test-ptr-null/match-test-ptr-null.stderr
[00:46:39] To update references, rerun the tests and pass the `--bless` flag
[00:46:39] To only update this specific test, also pass `--test-args const-eval/match-test-ptr-null.rs`
[00:46:39] error: 1 errors occurred comparing output.
[00:46:39] status: exit code: 101
[00:46:39] status: exit code: 101
[00:46:39] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/const-eval/match-test-ptr-null.rs" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/const-eval/match-test-ptr-null/a" "-Crpath" "-O" "-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/const-eval/match-test-ptr-null/auxiliary" "-A" "unused"
[00:46:39] ------------------------------------------
[00:46:39] 
[00:46:39] ------------------------------------------
[00:46:39] stderr:
[00:46:39] stderr:
[00:46:39] ------------------------------------------
[00:46:39] {"message":"raw pointers cannot be cast to integers in constants","code":{"code":"E0018","explanation":"\n\nThe value of static and constant integers must be known at compile time. You\ncan't cast a pointer to an integer because the address of a pointer can\nvary.\n\nFor example, if you write:\n\n```compile_fail,E0018\nstatic MY_STATIC: u32 = 42;\nstatic MY_STATIC_ADDR: usize = &MY_STATIC as *const _ as usize;\nstatic WHAT: usize = (MY_STATIC_ADDR^17) + MY_STATIC_ADDR;\n```\n\nThen `MY_STATIC_ADDR` would contain the address of `MY_STATIC`. However,\nthe address can change when the program is linked, as well as change\nbetween different executions due to ASLR, and many linkers would\nnot be able to calculate the value of `WHAT`.\n\nOn the other hand, static and constant pointers can point either to\na known numeric address or to the address of a symbol.\n\n```\nstatic MY_STATIC: u32 = 42;\nstatic MY_STATIC_ADDR: &'static u32 = &MY_STATIC;\nconst CONST_ADDR: *const u8 = 0x5f3759df as *const u8;\n```\n\nThis does not pose a problem by itself because they can't be\naccessed directly.\n"},"level":"error","spans":[{"file_name":"/checkout/src/test/ui/const-eval/match-test-ptr-null.rs","byte_start":766,"byte_end":791,"line_start":16,"line_end":16,"column_start":15,"column_end":40,"is_primary":true,"text":[{"text":"        match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers","highlight_start":15,"highlight_end":40}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":rror[E0019]: constant contains unimplemented expression type\n  --> /checkout/src/test/ui/const-eval/match-test-ptr-null.rs:17:13\n   |\nLL |             0 => 42, //~ ERROR constant contains unimplemented expression type\n   |             ^\n\n"}
[00:46:39] {"message":"could not evaluate repeat length","code":{"code":"E0080","explanation":"\nThis error indicates that the compiler was unable to sensibly evaluate an\nconstant expression that had to be evaluated. Attempting to divide by 0\nor causing integer overflow are two ways to induce this error. For example:\n\n```compile_fail,E0080\nenum Enum {\n    X = (1 << 500),\n    Y = (1 / 0)\n}\n```\n\nEnsure that the expressions given can be evaluated as the desired integer type.\nSee the FFI section of the Reference for more information about using a custom\ninteger type:\n\nhttps://doc.rust-lang.org/reference.html#ffi-attributes\n"},"level":"error","spans":[{"file_name":"/checkout/src/test/ui/const-eval/match-test-ptr-null.rs","byte_start":856,"byte_end":857,"line_start":17,"line_end":17,"column_start":13,"column_end":14,"is_primary":false,"text":[{"text":"            0 => 42, //~ ERROR constant contains unimplemented expression type","highlight_start":13,"highlight_end":14}],"label":"\"pointer arithmetic or comparison\" needs an rfc before being allowed inside constants","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"/checkout/src/test/ui/const-eval/match-test-ptr-null.rs","byte_start":707,"byte_end":1049,"line_start":15,"line_end":21,"column_start":26,"column_end":6,"is_primary":true,"text":[{"text":"    let _: [u8; 0] = [4; { //~ ERROR could not evaluate repeat length","highlight_start":26,"highlight_end":70},{"text":"        match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers","highlight_start":1,"highlight_end":92},{"text":"            0 => 42, //~ ERROR constant contains unimplemented expression type","highlight_start":1,"highlight_end":79},{"text":"            //~^ NOTE \"pointer arithmetic or comparison\" needs an rfc before being allowed","highlight_start":1,"highlight_end":91},{"text":"            n => n,","highlight_start":1,"highlight_end":20},{"text":"        }","highlight_start":1,"highlight_end":10},{"text":"    }];","highlight_start":1,"highlight_end":6}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error[E0080]: could not evaluate repeat length\n  --> /checkout/src/test/ui/const-eval/match-test-ptr-null.rs:15:26\n   |\nLL |       let _: [u8; 0] = [4; { //~ ERROR could not evaluate repeat length\n   |  __________________________^\nLL | |         match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers\nLL | |             0 => 42, //~ ERROR constant contains unimplemented expression type\n   | |             - \"pointer arithmetic or comparison\" needs an rfc before being allowed inside constants\nLL | |             //~^ NOTE \"pointer arithmetic or comparison\" needs an rfc before being allowed\nLL | |             n => n,\nLL | |         }\nLL | |     }];\n   | |_____^\n\n"}
[00:46:39] {"message":"aborting due to 3 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to 3 previous errors\n\n"}
[00:46:39] {"message":"Some errors occurred: E0018, E0019, E0080.","code":null,"level":"","spans":[],"children":[],"rendered":"Some errors occurred: E0018, E0019, E0080.\n"}
[00:46:39] {"message":"For more information about an error, try `rustc --explain E0018`.","code":null,"level":"","spans":[],"children":[],"rendered":"For more information about an error, try `rustc --explain E0018`.\n"}
[00:46:39] ------------------------------------------
[00:46:39] 
[00:46:39] 
[00:46:39] thread '[ui] ui/const-eval/match-test-ptr-null.rs' panicked at 'explicit panic', tools/compiletest/src/runtest.rs:3162:9
[00:46:39] 
[00:46:39] 
[00:46:39] failures:
[00:46:39] failures:
[00:46:39]     [ui] ui/const-eval/match-test-ptr-null.rs
[00:46:39] test result: FAILED. 1562 passed; 1 failed; 5 ignored; 0 measured; 0 filtered out
[00:46:39] 
[00:46:39] thread 'main' panicked at 'Some tests failed', tools/compiletest/src/main.rs:498:22
[00:46:39] 
[00:46:39] 
[00:46:39] 
[00:46:39] 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-5.0/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Zunstable-options " "--target-rustcflags" "-Crpath -O -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" "5.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"
[00:46:39] 
[00:46:39] 
[00:46:39] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
[00:46:39] Build completed unsuccessfully in 0:01:27
[00:46:39] Build completed unsuccessfully in 0:01:27
[00:46:39] Makefile:58: recipe for target 'check' failed
[00:46:39] make: *** [check] Error 1
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:0175cb35
travis_time:start:0175cb35
$ 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 -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:03e59fea
travis_time:start:03e59fea
$ 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:1b61b9f1
$ 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 Jul 15, 2018

r=me with ui test updated to reflect the line changes

@bors delegate

@oli-obk
Copy link
Contributor

oli-obk commented Jul 15, 2018

@bors delegate+

@bors
Copy link
Collaborator

bors commented Jul 15, 2018

✌️ @RalfJung can now approve this pull request

@RalfJung
Copy link
Member Author

Dang, sorry -- forgot to --bless again after adding the license...

@RalfJung
Copy link
Member Author

@bors r=oli-obk

@bors
Copy link
Collaborator

bors commented Jul 15, 2018

📌 Commit f9825c2 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 Jul 15, 2018
@bors
Copy link
Collaborator

bors commented Jul 15, 2018

⌛ Testing commit f9825c2 with merge 31f1bc7...

bors added a commit that referenced this pull request Jul 15, 2018
CTFE: use binary_op to compare integer with match disriminant

This is needed to unblock rust-lang/miri#401: There is code in the Windows initialization functions that uses `match` to test whether a pointer is NULL.

I will add a testcase in miri; I was not sure where to add a testcase in Rust itself.

r? @oli-obk
@bors
Copy link
Collaborator

bors commented Jul 15, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: oli-obk
Pushing 31f1bc7 to master...

@bors bors merged commit f9825c2 into rust-lang:master Jul 15, 2018
@RalfJung RalfJung deleted the match branch July 23, 2018 09:16
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.

4 participants