rust/src/test/compile-fail/fat-ptr-cast.rs
Ulrik Sverdrup 4806fb29a0 typeck: Make sure casts to or from fat pointers are illegal
Fixes ICEs where non-fat pointers and scalars are cast to fat pointers,

Fixes #21397
Fixes #22955
Fixes #23237
Fixes #24100
2015-05-02 01:18:33 +02:00

32 lines
1.0 KiB
Rust

// Copyright 2015 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.
// Make sure casts between thin pointer <-> fat pointer are illegal.
pub trait Trait {}
fn main() {
let a: &[i32] = &[1, 2, 3];
let b: Box<[i32]> = Box::new([1, 2, 3]);
let p = a as *const [i32];
let q = a.as_ptr();
a as usize; //~ ERROR illegal cast
b as usize; //~ ERROR illegal cast
p as usize; //~ ERROR illegal cast
// #22955
q as *const [i32]; //~ ERROR illegal cast
// #21397
let t: *mut (Trait + 'static) = 0 as *mut _; //~ ERROR illegal cast
let mut fail: *const str = 0 as *const str; //~ ERROR illegal cast
}