diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs index fd6c4f44ba42..ba37ee454eb4 100644 --- a/src/librustc_typeck/check/cast.rs +++ b/src/librustc_typeck/check/cast.rs @@ -1,4 +1,4 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -20,7 +20,8 @@ //! * `e` is a C-like enum and `U` is an integer type; *enum-cast* //! * `e` has type `bool` or `char` and `U` is an integer; *prim-int-cast* //! * `e` has type `u8` and `U` is `char`; *u8-char-cast* -//! * `e` has type `&[T; n]` and `U` is `*const T`; *array-ptr-cast* +//! * `e` has type `&.[T; n]` and `U` is `*T`, +//! while if `e` is `&[T; n]` then `U` is `*const T`; *array-ptr-cast* //! * `e` is a function pointer type and `U` has type `*T`, //! while `T: Sized`; *fptr-ptr-cast* //! * `e` is a function pointer type and `U` is an integer; *fptr-addr-cast* @@ -345,7 +346,7 @@ impl<'tcx> CastCheck<'tcx> { { // array-ptr-cast. - if m_expr.mutbl == hir::MutImmutable && m_cast.mutbl == hir::MutImmutable { + if !(m_expr.mutbl == hir::MutImmutable && m_cast.mutbl == hir::MutMutable) { if let ty::TyArray(ety, _) = m_expr.ty.sty { // Due to the limitations of LLVM global constants, // region pointers end up pointing at copies of diff --git a/src/test/compile-fail/cast-mut-ref-to-const-ptr.rs b/src/test/compile-fail/cast-mut-ref-to-const-ptr.rs new file mode 100644 index 000000000000..1da4ce2caddf --- /dev/null +++ b/src/test/compile-fail/cast-mut-ref-to-const-ptr.rs @@ -0,0 +1,14 @@ +// Copyright 2016 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. + +fn main() { + let my_num = &[10, 20] as *mut i32; + //~ error: casting `&[i32; 2]` as `*mut i32` is invalid +} diff --git a/src/test/compile-fail/vector-cast-weirdness.rs b/src/test/compile-fail/vector-cast-weirdness.rs deleted file mode 100644 index 26c59c440d47..000000000000 --- a/src/test/compile-fail/vector-cast-weirdness.rs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2012 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. - -// Issue #14893. Tests that casts from vectors don't behave strangely in the -// presence of the `_` type shorthand notation. -// Update: after a change to the way casts are done, we have more type information -// around and so the errors here are no longer exactly the same. - -struct X { - y: [u8; 2], -} - -fn main() { - let x1 = X { y: [0, 0] }; - - // No longer a type mismatch - the `_` can be fully resolved by type inference. - let p1: *const u8 = &x1.y as *const _; - let t1: *const [u8; 2] = &x1.y as *const _; - let h1: *const [u8; 2] = &x1.y as *const [u8; 2]; - - let mut x1 = X { y: [0, 0] }; - - // This is still an error since we don't allow casts from &mut [T; n] to *mut T. - let p1: *mut u8 = &mut x1.y as *mut _; //~ ERROR casting - let t1: *mut [u8; 2] = &mut x1.y as *mut _; - let h1: *mut [u8; 2] = &mut x1.y as *mut [u8; 2]; -} diff --git a/src/test/run-pass/cast.rs b/src/test/run-pass/cast.rs index bb60626a4bf0..8ddeefeef321 100644 --- a/src/test/run-pass/cast.rs +++ b/src/test/run-pass/cast.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. - pub fn main() { let i: isize = 'Q' as isize; assert_eq!(i, 0x51); @@ -19,4 +18,8 @@ pub fn main() { assert_eq!(i as u8 as i8, 'Q' as u8 as i8); assert_eq!(0x51 as char, 'Q'); assert_eq!(0 as u32, false as u32); + + let x = &[10, 20] as *const i32; + let y = &mut [10, 20] as *mut i32; + let z = &mut [10, 20] as *const i32; }