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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/librustc_mir/interpret/terminator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use rustc::mir;
use rustc::ty::{self, Ty};
use rustc::ty::layout::LayoutOf;
use rustc::ty::layout::{LayoutOf, Size};
use syntax::codemap::Span;
use rustc_target::spec::abi::Abi;

use rustc::mir::interpret::EvalResult;
use rustc::mir::interpret::{EvalResult, Scalar};
use super::{EvalContext, Place, Machine, ValTy};

use rustc_data_structures::indexed_vec::Idx;
Expand Down Expand Up @@ -41,13 +41,18 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
let discr_prim = self.value_to_scalar(discr_val)?;
let discr_layout = self.layout_of(discr_val.ty).unwrap();
trace!("SwitchInt({:?}, {:#?})", discr_prim, discr_layout);
let discr_prim = discr_prim.to_bits(discr_layout.size)?;

// Branch to the `otherwise` case by default, if no match is found.
let mut target_block = targets[targets.len() - 1];

for (index, &const_int) in values.iter().enumerate() {
if discr_prim == const_int {
// Compare using binary_op
let const_int = Scalar::Bits { bits: const_int, defined: 128 };
let res = self.binary_op(mir::BinOp::Eq,
discr_prim, discr_val.ty,
const_int, discr_val.ty
)?;
if res.0.to_bits(Size::from_bytes(1))? != 0 {
target_block = targets[index];
break;
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/const-eval/match-test-ptr-null.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
// Make sure match uses the usual pointer comparison code path -- i.e., it should complain
// that pointer comparison is disallowed, not that parts of a pointer are accessed as raw
// bytes.
let _: [u8; 0] = [4; { //~ ERROR could not evaluate repeat length
match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers
0 => 42, //~ ERROR constant contains unimplemented expression type
//~^ NOTE "pointer arithmetic or comparison" needs an rfc before being allowed
n => n,
}
}];
}
30 changes: 30 additions & 0 deletions src/test/ui/const-eval/match-test-ptr-null.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0018]: raw pointers cannot be cast to integers in constants
--> $DIR/match-test-ptr-null.rs:16:15
|
LL | match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0019]: constant contains unimplemented expression type
--> $DIR/match-test-ptr-null.rs:17:13
|
LL | 0 => 42, //~ ERROR constant contains unimplemented expression type
| ^

error[E0080]: could not evaluate repeat length
--> $DIR/match-test-ptr-null.rs:15:26
|
LL | let _: [u8; 0] = [4; { //~ ERROR could not evaluate repeat length
| __________________________^
LL | | match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers
LL | | 0 => 42, //~ ERROR constant contains unimplemented expression type
| | - "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
LL | | //~^ NOTE "pointer arithmetic or comparison" needs an rfc before being allowed
LL | | n => n,
LL | | }
LL | | }];
| |_____^

error: aborting due to 3 previous errors

Some errors occurred: E0018, E0019, E0080.
For more information about an error, try `rustc --explain E0018`.