From 293b57701b7bcdd88e44ce8f671511ba35cac13e Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Mon, 29 Sep 2014 19:58:32 -0700 Subject: [PATCH 1/4] Add missing case for pointer -> int when translating constant cast expressions Closes issue #17458 --- src/librustc/middle/trans/consts.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/librustc/middle/trans/consts.rs b/src/librustc/middle/trans/consts.rs index d39fe4a1e7003..456ff69e0ec04 100644 --- a/src/librustc/middle/trans/consts.rs +++ b/src/librustc/middle/trans/consts.rs @@ -547,6 +547,9 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr, (expr::cast_integral, expr::cast_pointer) => { llvm::LLVMConstIntToPtr(v, llty.to_ref()) } + (expr::cast_pointer, expr::cast_integral) => { + llvm::LLVMConstPtrToInt(v, llty.to_ref()) + } _ => { cx.sess().impossible_case(e.span, "bad combination of types for cast") From 7c9db32f828729a415f4c334cb69f485e3b84d3d Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Tue, 30 Sep 2014 19:48:17 -0700 Subject: [PATCH 2/4] Disallow casting directly between C-like enums and unsafe pointers This closes issue #17444 --- src/librustc/middle/typeck/check/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index d4c38d48a8c56..c051782a83b18 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -1468,7 +1468,7 @@ fn check_cast(fcx: &FnCtxt, // casts to scalars other than `char` and `bare fn` are trivial let t_1_is_trivial = t_1_is_scalar && !t_1_is_char && !t_1_is_bare_fn; if ty::type_is_c_like_enum(fcx.tcx(), t_e) && t_1_is_trivial { - if t_1_is_float { + if t_1_is_float || ty::type_is_unsafe_ptr(t_1) { fcx.type_error_message(span, |actual| { format!("illegal cast; cast through an \ integer first: `{}` as `{}`", From eb8b36973db4fc0340f8f5345b67410d68af5115 Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Mon, 29 Sep 2014 20:22:29 -0700 Subject: [PATCH 3/4] Add regression test for issue #17458 --- src/test/run-pass/issue-17458.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/test/run-pass/issue-17458.rs diff --git a/src/test/run-pass/issue-17458.rs b/src/test/run-pass/issue-17458.rs new file mode 100644 index 0000000000000..a32a31e97e3e0 --- /dev/null +++ b/src/test/run-pass/issue-17458.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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +static X: uint = 0 as *const uint as uint; + +fn main() { + assert_eq!(X, 0); +} From 93408be7884754a3a661412c6be940cc1e9691d7 Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Mon, 29 Sep 2014 20:22:43 -0700 Subject: [PATCH 4/4] Add regression test for issue #17444 --- src/test/compile-fail/issue-17444.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/test/compile-fail/issue-17444.rs diff --git a/src/test/compile-fail/issue-17444.rs b/src/test/compile-fail/issue-17444.rs new file mode 100644 index 0000000000000..33777e820edc8 --- /dev/null +++ b/src/test/compile-fail/issue-17444.rs @@ -0,0 +1,18 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +enum Test { + Foo = 0 +} + +fn main() { + let _x = Foo as *const int; + //~^ ERROR illegal cast; cast through an integer first: `Test` as `*const int` +}