diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2016-06-10 13:00:21 +0300 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2017-02-28 23:47:55 +0200 |
| commit | 7650afc1cea284956080098d39d670ea0b007ce7 (patch) | |
| tree | d8611263ba31a649a64a1bab7c960f3e152ce6d0 /src/test | |
| parent | e1cb9ba221e5cb0070ac82c6a234af11e4240680 (diff) | |
| download | rust-7650afc1cea284956080098d39d670ea0b007ce7.tar.gz rust-7650afc1cea284956080098d39d670ea0b007ce7.zip | |
Make transmuting from fn item types to pointer-sized types a hard error.
Diffstat (limited to 'src/test')
3 files changed, 48 insertions, 77 deletions
diff --git a/src/test/compile-fail/transmute-from-fn-item-types-error.rs b/src/test/compile-fail/transmute-from-fn-item-types-error.rs index 50bcd53ecb8..c3fe1de895d 100644 --- a/src/test/compile-fail/transmute-from-fn-item-types-error.rs +++ b/src/test/compile-fail/transmute-from-fn-item-types-error.rs @@ -10,14 +10,61 @@ use std::mem; +unsafe fn foo() -> (isize, *const (), Option<fn()>) { + let i = mem::transmute(bar); + //~^ ERROR is zero-sized and can't be transmuted + //~^^ NOTE cast with `as` to a pointer instead + + let p = mem::transmute(foo); + //~^ ERROR is zero-sized and can't be transmuted + //~^^ NOTE cast with `as` to a pointer instead + + let of = mem::transmute(main); + //~^ ERROR is zero-sized and can't be transmuted + //~^^ NOTE cast with `as` to a pointer instead + + (i, p, of) +} + unsafe fn bar() { - // Error, still, if the resulting type is not pointer-sized. + // Error as usual if the resulting type is not pointer-sized. mem::transmute::<_, u8>(main); //~^ ERROR transmute called with differently sized types + //~^^ NOTE transmuting between 0 bits and 8 bits + + mem::transmute::<_, *mut ()>(foo); + //~^ ERROR is zero-sized and can't be transmuted + //~^^ NOTE cast with `as` to a pointer instead + + mem::transmute::<_, fn()>(bar); + //~^ ERROR is zero-sized and can't be transmuted + //~^^ NOTE cast with `as` to a pointer instead + + // No error if a coercion would otherwise occur. + mem::transmute::<fn(), usize>(main); +} + +unsafe fn baz() { + mem::transmute::<_, *mut ()>(Some(foo)); + //~^ ERROR is zero-sized and can't be transmuted + //~^^ NOTE cast with `as` to a pointer instead + + mem::transmute::<_, fn()>(Some(bar)); + //~^ ERROR is zero-sized and can't be transmuted + //~^^ NOTE cast with `as` to a pointer instead + + mem::transmute::<_, Option<fn()>>(Some(baz)); + //~^ ERROR is zero-sized and can't be transmuted + //~^^ NOTE cast with `as` to a pointer instead + + // No error if a coercion would otherwise occur. + mem::transmute::<Option<fn()>, usize>(Some(main)); } fn main() { unsafe { + foo(); bar(); + baz(); } } diff --git a/src/test/compile-fail/transmute-from-fn-item-types-lint.rs b/src/test/compile-fail/transmute-from-fn-item-types-lint.rs deleted file mode 100644 index 08e660e878c..00000000000 --- a/src/test/compile-fail/transmute-from-fn-item-types-lint.rs +++ /dev/null @@ -1,49 +0,0 @@ -// 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. - -#![deny(transmute_from_fn_item_types)] - -use std::mem; - -unsafe fn foo() -> (isize, *const (), Option<fn()>) { - let i = mem::transmute(bar); - //~^ ERROR is now zero-sized and has to be cast to a pointer before transmuting - //~^^ WARNING was previously accepted - - let p = mem::transmute(foo); - //~^ ERROR is now zero-sized and has to be cast to a pointer before transmuting - //~^^ WARNING was previously accepted - - let of = mem::transmute(main); - //~^ ERROR is now zero-sized and has to be cast to a pointer before transmuting - //~^^ WARNING was previously accepted - - (i, p, of) -} - -unsafe fn bar() { - mem::transmute::<_, *mut ()>(foo); - //~^ ERROR is now zero-sized and has to be cast to a pointer before transmuting - //~^^ WARNING was previously accepted - - mem::transmute::<_, fn()>(bar); - //~^ ERROR is now zero-sized and has to be cast to a pointer before transmuting - //~^^ WARNING was previously accepted - - // No error if a coercion would otherwise occur. - mem::transmute::<fn(), usize>(main); -} - -fn main() { - unsafe { - foo(); - bar(); - } -} diff --git a/src/test/run-pass/transmute-from-fn-item-types.rs b/src/test/run-pass/transmute-from-fn-item-types.rs deleted file mode 100644 index 574a90e2ad6..00000000000 --- a/src/test/run-pass/transmute-from-fn-item-types.rs +++ /dev/null @@ -1,27 +0,0 @@ -// 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. - -#![allow(transmute_from_fn_item_types)] - -use std::mem; - -fn main() { - unsafe { - let u = mem::transmute(main); - let p = mem::transmute(main); - let f = mem::transmute(main); - let tuple: (usize, *mut (), fn()) = (u, p, f); - assert_eq!(mem::transmute::<_, [usize; 3]>(tuple), [main as usize; 3]); - - mem::transmute::<_, usize>(main); - mem::transmute::<_, *mut ()>(main); - mem::transmute::<_, fn()>(main); - } -} |
