Skip to content

Commit 8bbf598

Browse files
committed
auto merge of #15559 : fhahn/rust/issue-15445-mut-cast, r=alexcrichton
I've added an error message for casts from raw pointers to floats #15445.
2 parents 345886c + 9bc7b6f commit 8bbf598

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,13 @@ fn check_cast(fcx: &FnCtxt,
12191219
actual,
12201220
fcx.infcx().ty_to_string(t_1))
12211221
}, t_e, None);
1222+
} else if ty::type_is_unsafe_ptr(t_e) && t_1_is_float {
1223+
fcx.type_error_message(span, |actual| {
1224+
format!("cannot cast from pointer to float directly: `{}` as `{}`; cast through an \
1225+
integer first",
1226+
actual,
1227+
fcx.infcx().ty_to_string(t_1))
1228+
}, t_e, None);
12221229
}
12231230

12241231
fcx.write_ty(id, t_1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 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+
fn main() {
12+
let x : i16 = 22;
13+
((&x) as *const i16) as f32;
14+
//~^ ERROR: cannot cast from pointer to float directly: `*const i16` as `f32`
15+
}

0 commit comments

Comments
 (0)