Skip to content

Commit b49e9fa

Browse files
committed
forbid cast as bool
This is currently unsound since `bool` is represented as `i8`. It will become sound when `bool` is stored as `i8` but always used as `i1`. However, the current behaviour will always be identical to `x & 1 != 0`, so there's no need for it. It's also surprising, since `x != 0` is the expected behaviour. Closes #7311
1 parent 8827b94 commit b49e9fa

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

src/libextra/ebml.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ pub mod reader {
410410
}
411411

412412
fn read_bool(&mut self) -> bool {
413-
doc_as_u8(self.next_doc(EsBool)) as bool
413+
doc_as_u8(self.next_doc(EsBool)) != 0
414414
}
415415

416416
fn read_f64(&mut self) -> f64 {

src/librustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,7 @@ pub fn trans_item(ccx: @mut CrateContext, item: &ast::item) {
22192219
}
22202220
let v = ccx.const_values.get_copy(&item.id);
22212221
unsafe {
2222-
if !(llvm::LLVMConstIntGetZExtValue(v) as bool) {
2222+
if !(llvm::LLVMConstIntGetZExtValue(v) != 0) {
22232223
ccx.sess.span_fatal(expr.span, "static assertion failed");
22242224
}
22252225
}

src/librustc/middle/typeck/check/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2696,6 +2696,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
26962696
}, t_e, None);
26972697
}
26982698

2699+
let t1 = structurally_resolved_type(fcx, e.span, t_1);
26992700
let te = structurally_resolved_type(fcx, e.span, t_e);
27002701
let t_1_is_char = type_is_char(fcx, expr.span, t_1);
27012702

@@ -2710,6 +2711,9 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
27102711
fmt!("only `u8` can be cast as `char`, not `%s`", actual)
27112712
}, t_e, None);
27122713
}
2714+
} else if ty::get(t1).sty == ty::ty_bool {
2715+
fcx.tcx().sess.span_err(expr.span,
2716+
"cannot cast as `bool`, compare with zero instead");
27132717
} else if type_is_region_ptr(fcx, expr.span, t_e) &&
27142718
type_is_unsafe_ptr(fcx, expr.span, t_1) {
27152719

src/test/compile-fail/cast-as-bool.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern: cannot cast as `bool`, compare with zero instead
12+
fn main() { let u = (5 as bool); }

src/test/run-pass/cast.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ pub fn main() {
1818
assert_eq!(i as u8, 'Q' as u8);
1919
assert_eq!(i as u8 as i8, 'Q' as u8 as i8);
2020
assert_eq!(0x51u8 as char, 'Q');
21-
assert_eq!(true, 1 as bool);
2221
assert_eq!(0 as u32, false as u32);
2322
}

src/test/run-pass/supported-cast.rs

-15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub fn main() {
2626
info!(1 as int);
2727
info!(1 as uint);
2828
info!(1 as float);
29-
info!(1 as bool);
3029
info!(1 as *libc::FILE);
3130
info!(1 as i8);
3231
info!(1 as i16);
@@ -42,7 +41,6 @@ pub fn main() {
4241
info!(1u as int);
4342
info!(1u as uint);
4443
info!(1u as float);
45-
info!(1u as bool);
4644
info!(1u as *libc::FILE);
4745
info!(1u as i8);
4846
info!(1u as i16);
@@ -58,7 +56,6 @@ pub fn main() {
5856
info!(1i8 as int);
5957
info!(1i8 as uint);
6058
info!(1i8 as float);
61-
info!(1i8 as bool);
6259
info!(1i8 as *libc::FILE);
6360
info!(1i8 as i8);
6461
info!(1i8 as i16);
@@ -74,7 +71,6 @@ pub fn main() {
7471
info!(1u8 as int);
7572
info!(1u8 as uint);
7673
info!(1u8 as float);
77-
info!(1u8 as bool);
7874
info!(1u8 as *libc::FILE);
7975
info!(1u8 as i8);
8076
info!(1u8 as i16);
@@ -90,7 +86,6 @@ pub fn main() {
9086
info!(1i16 as int);
9187
info!(1i16 as uint);
9288
info!(1i16 as float);
93-
info!(1i16 as bool);
9489
info!(1i16 as *libc::FILE);
9590
info!(1i16 as i8);
9691
info!(1i16 as i16);
@@ -106,7 +101,6 @@ pub fn main() {
106101
info!(1u16 as int);
107102
info!(1u16 as uint);
108103
info!(1u16 as float);
109-
info!(1u16 as bool);
110104
info!(1u16 as *libc::FILE);
111105
info!(1u16 as i8);
112106
info!(1u16 as i16);
@@ -122,7 +116,6 @@ pub fn main() {
122116
info!(1i32 as int);
123117
info!(1i32 as uint);
124118
info!(1i32 as float);
125-
info!(1i32 as bool);
126119
info!(1i32 as *libc::FILE);
127120
info!(1i32 as i8);
128121
info!(1i32 as i16);
@@ -138,7 +131,6 @@ pub fn main() {
138131
info!(1u32 as int);
139132
info!(1u32 as uint);
140133
info!(1u32 as float);
141-
info!(1u32 as bool);
142134
info!(1u32 as *libc::FILE);
143135
info!(1u32 as i8);
144136
info!(1u32 as i16);
@@ -154,7 +146,6 @@ pub fn main() {
154146
info!(1i64 as int);
155147
info!(1i64 as uint);
156148
info!(1i64 as float);
157-
info!(1i64 as bool);
158149
info!(1i64 as *libc::FILE);
159150
info!(1i64 as i8);
160151
info!(1i64 as i16);
@@ -170,7 +161,6 @@ pub fn main() {
170161
info!(1u64 as int);
171162
info!(1u64 as uint);
172163
info!(1u64 as float);
173-
info!(1u64 as bool);
174164
info!(1u64 as *libc::FILE);
175165
info!(1u64 as i8);
176166
info!(1u64 as i16);
@@ -186,7 +176,6 @@ pub fn main() {
186176
info!(1u64 as int);
187177
info!(1u64 as uint);
188178
info!(1u64 as float);
189-
info!(1u64 as bool);
190179
info!(1u64 as *libc::FILE);
191180
info!(1u64 as i8);
192181
info!(1u64 as i16);
@@ -202,7 +191,6 @@ pub fn main() {
202191
info!(true as int);
203192
info!(true as uint);
204193
info!(true as float);
205-
info!(true as bool);
206194
info!(true as *libc::FILE);
207195
info!(true as i8);
208196
info!(true as i16);
@@ -218,7 +206,6 @@ pub fn main() {
218206
info!(1. as int);
219207
info!(1. as uint);
220208
info!(1. as float);
221-
info!(1. as bool);
222209
info!(1. as i8);
223210
info!(1. as i16);
224211
info!(1. as i32);
@@ -233,7 +220,6 @@ pub fn main() {
233220
info!(1f32 as int);
234221
info!(1f32 as uint);
235222
info!(1f32 as float);
236-
info!(1f32 as bool);
237223
info!(1f32 as i8);
238224
info!(1f32 as i16);
239225
info!(1f32 as i32);
@@ -248,7 +234,6 @@ pub fn main() {
248234
info!(1f64 as int);
249235
info!(1f64 as uint);
250236
info!(1f64 as float);
251-
info!(1f64 as bool);
252237
info!(1f64 as i8);
253238
info!(1f64 as i16);
254239
info!(1f64 as i32);

0 commit comments

Comments
 (0)