Skip to content

Add more array references to raw pointer casts 2. #33411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/librustc_typeck/check/cast.rs
Original file line number Diff line number Diff line change
@@ -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.
//
Expand All @@ -20,7 +20,7 @@
//! * `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`; *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*
Expand Down Expand Up @@ -445,7 +445,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
Expand Down Expand Up @@ -494,4 +494,3 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span)
}
}

14 changes: 14 additions & 0 deletions src/test/compile-fail/cast-ref-to-mut-ptr.rs
Original file line number Diff line number Diff line change
@@ -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 <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 = &[10, 20] as *mut i32;
//~^ ERROR casting `&[i32; 2]` as `*mut i32` is invalid
}
34 changes: 0 additions & 34 deletions src/test/compile-fail/vector-cast-weirdness.rs

This file was deleted.

6 changes: 5 additions & 1 deletion src/test/run-pass/cast.rs
Original file line number Diff line number Diff line change
@@ -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.
//
Expand All @@ -19,4 +19,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;
}