Skip to content

Commit d285ea7

Browse files
committed
auto merge of #8980 : thestinger/rust/bool, r=thestinger
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 d0a1176 r=huonw e4a76e6 r=thestinger
2 parents b6d825e + b153219 commit d285ea7

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
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/libstd/str.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,8 @@ pub fn is_utf8(v: &[u8]) -> bool {
799799
// first C2 80 last DF BF
800800
// 3-byte encoding is for codepoints \u0800 to \uffff
801801
// first E0 A0 80 last EF BF BF
802+
// excluding surrogates codepoints \ud800 to \udfff
803+
// ED A0 80 to ED BF BF
802804
// 4-byte encoding is for codepoints \u10000 to \u10ffff
803805
// first F0 90 80 80 last F4 8F BF BF
804806
//
@@ -812,8 +814,6 @@ pub fn is_utf8(v: &[u8]) -> bool {
812814
// UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
813815
// %xF4 %x80-8F 2( UTF8-tail )
814816
// UTF8-tail = %x80-BF
815-
// --
816-
// This code allows surrogate pairs: \uD800 to \uDFFF -> ED A0 80 to ED BF BF
817817
match w {
818818
2 => if unsafe_get(v, i + 1) & 192u8 != TAG_CONT_U8 {
819819
return false
@@ -822,7 +822,9 @@ pub fn is_utf8(v: &[u8]) -> bool {
822822
unsafe_get(v, i + 1),
823823
unsafe_get(v, i + 2) & 192u8) {
824824
(0xE0 , 0xA0 .. 0xBF, TAG_CONT_U8) => (),
825-
(0xE1 .. 0xEF, 0x80 .. 0xBF, TAG_CONT_U8) => (),
825+
(0xE1 .. 0xEC, 0x80 .. 0xBF, TAG_CONT_U8) => (),
826+
(0xED , 0x80 .. 0x9F, TAG_CONT_U8) => (),
827+
(0xEE .. 0xEF, 0x80 .. 0xBF, TAG_CONT_U8) => (),
826828
_ => return false,
827829
},
828830
_ => match (v_i,
@@ -3012,6 +3014,7 @@ mod tests {
30123014
30133015
#[test]
30143016
fn test_is_utf8() {
3017+
// deny overlong encodings
30153018
assert!(!is_utf8([0xc0, 0x80]));
30163019
assert!(!is_utf8([0xc0, 0xae]));
30173020
assert!(!is_utf8([0xe0, 0x80, 0x80]));
@@ -3020,9 +3023,15 @@ mod tests {
30203023
assert!(!is_utf8([0xf0, 0x82, 0x82, 0xac]));
30213024
assert!(!is_utf8([0xf4, 0x90, 0x80, 0x80]));
30223025
3026+
// deny surrogates
3027+
assert!(!is_utf8([0xED, 0xA0, 0x80]));
3028+
assert!(!is_utf8([0xED, 0xBF, 0xBF]));
3029+
30233030
assert!(is_utf8([0xC2, 0x80]));
30243031
assert!(is_utf8([0xDF, 0xBF]));
30253032
assert!(is_utf8([0xE0, 0xA0, 0x80]));
3033+
assert!(is_utf8([0xED, 0x9F, 0xBF]));
3034+
assert!(is_utf8([0xEE, 0x80, 0x80]));
30263035
assert!(is_utf8([0xEF, 0xBF, 0xBF]));
30273036
assert!(is_utf8([0xF0, 0x90, 0x80, 0x80]));
30283037
assert!(is_utf8([0xF4, 0x8F, 0xBF, 0xBF]));

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)