diff options
| author | bors <bors@rust-lang.org> | 2014-07-10 19:06:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-07-10 19:06:59 +0000 |
| commit | 8bbf598d50960087342667fc47f5d38f4a9c2165 (patch) | |
| tree | f2fcde0e3fbe4301c5c8b80c4e11acbd9884fe4b /src | |
| parent | 345886cfddd881644fbf8c0e0c1487102b460cf6 (diff) | |
| parent | 9bc7b6f437ed041e4f7e52c1bf7e646e3c088ea5 (diff) | |
| download | rust-8bbf598d50960087342667fc47f5d38f4a9c2165.tar.gz rust-8bbf598d50960087342667fc47f5d38f4a9c2165.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc/middle/typeck/check/mod.rs | 7 | ||||
| -rw-r--r-- | src/test/compile-fail/typeck-cast-pointer-to-float.rs | 15 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 9f5fcb61f5f..cdf434f4099 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -1219,6 +1219,13 @@ fn check_cast(fcx: &FnCtxt, actual, fcx.infcx().ty_to_string(t_1)) }, t_e, None); + } else if ty::type_is_unsafe_ptr(t_e) && t_1_is_float { + fcx.type_error_message(span, |actual| { + format!("cannot cast from pointer to float directly: `{}` as `{}`; cast through an \ + integer first", + actual, + fcx.infcx().ty_to_string(t_1)) + }, t_e, None); } fcx.write_ty(id, t_1); diff --git a/src/test/compile-fail/typeck-cast-pointer-to-float.rs b/src/test/compile-fail/typeck-cast-pointer-to-float.rs new file mode 100644 index 00000000000..22a0978ef7c --- /dev/null +++ b/src/test/compile-fail/typeck-cast-pointer-to-float.rs @@ -0,0 +1,15 @@ +// Copyright 2014 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() { + let x : i16 = 22; + ((&x) as *const i16) as f32; + //~^ ERROR: cannot cast from pointer to float directly: `*const i16` as `f32` +} |
