2014-02-07 19:08:32 +00:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-07-15 13:48:57 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-05-15 13:33:51 +00:00
|
|
|
// ignore-tidy-linelength
|
2014-02-07 19:08:32 +00:00
|
|
|
// ignore-android: FIXME(#10381)
|
2013-11-04 06:53:01 +00:00
|
|
|
|
2014-02-07 03:57:09 +00:00
|
|
|
// compile-flags:-g
|
2014-04-24 09:35:48 +00:00
|
|
|
// gdb-command:rbreak zzz
|
|
|
|
// gdb-command:run
|
|
|
|
// gdb-command:finish
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-04-24 09:35:48 +00:00
|
|
|
// gdb-command:print some
|
2014-05-15 13:33:51 +00:00
|
|
|
// gdb-check:$1 = {RUST$ENCODED$ENUM$0$None = {0x12345678}}
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-04-24 09:35:48 +00:00
|
|
|
// gdb-command:print none
|
2014-05-15 13:33:51 +00:00
|
|
|
// gdb-check:$2 = {RUST$ENCODED$ENUM$0$None = {0x0}}
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-04-24 09:35:48 +00:00
|
|
|
// gdb-command:print full
|
2014-05-15 13:33:51 +00:00
|
|
|
// gdb-check:$3 = {RUST$ENCODED$ENUM$1$Empty = {454545, 0x87654321, 9988}}
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-04-24 09:35:48 +00:00
|
|
|
// gdb-command:print empty->discr
|
|
|
|
// gdb-check:$4 = (int *) 0x0
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-04-24 09:35:48 +00:00
|
|
|
// gdb-command:print droid
|
2014-05-15 13:33:51 +00:00
|
|
|
// gdb-check:$5 = {RUST$ENCODED$ENUM$2$Void = {id = 675675, range = 10000001, internals = 0x43218765}}
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-04-24 09:35:48 +00:00
|
|
|
// gdb-command:print void_droid->internals
|
|
|
|
// gdb-check:$6 = (int *) 0x0
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-04-24 09:35:48 +00:00
|
|
|
// gdb-command:continue
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-04-14 15:30:31 +00:00
|
|
|
#![feature(struct_variant)]
|
2013-10-03 03:00:54 +00:00
|
|
|
|
2013-07-15 13:48:57 +00:00
|
|
|
// If a struct has exactly two variants, one of them is empty, and the other one
|
|
|
|
// contains a non-nullable pointer, then this value is used as the discriminator.
|
|
|
|
// The test cases in this file make sure that something readable is generated for
|
|
|
|
// this kind of types.
|
2014-05-18 07:04:50 +00:00
|
|
|
// If the non-empty variant contains a single non-nullable pointer than the whole
|
|
|
|
// item is represented as just a pointer and not wrapped in a struct.
|
2013-08-08 12:14:52 +00:00
|
|
|
// Unfortunately (for these test cases) the content of the non-discriminant fields
|
|
|
|
// in the null-case is not defined. So we just read the discriminator field in
|
|
|
|
// this case (by casting the value to a memory-equivalent struct).
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2013-12-10 07:16:18 +00:00
|
|
|
enum MoreFields<'a> {
|
|
|
|
Full(u32, &'a int, i16),
|
2013-07-15 13:48:57 +00:00
|
|
|
Empty
|
|
|
|
}
|
|
|
|
|
2013-12-10 07:16:18 +00:00
|
|
|
struct MoreFieldsRepr<'a> {
|
2013-08-08 12:14:52 +00:00
|
|
|
a: u32,
|
2013-12-10 07:16:18 +00:00
|
|
|
discr: &'a int,
|
2013-08-08 12:14:52 +00:00
|
|
|
b: i16
|
|
|
|
}
|
|
|
|
|
2013-12-10 07:16:18 +00:00
|
|
|
enum NamedFields<'a> {
|
|
|
|
Droid { id: i32, range: i64, internals: &'a int },
|
2013-07-15 13:48:57 +00:00
|
|
|
Void
|
|
|
|
}
|
|
|
|
|
2013-12-10 07:16:18 +00:00
|
|
|
struct NamedFieldsRepr<'a> {
|
2013-08-08 12:14:52 +00:00
|
|
|
id: i32,
|
|
|
|
range: i64,
|
2013-12-10 07:16:18 +00:00
|
|
|
internals: &'a int
|
2013-08-08 12:14:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-15 13:48:57 +00:00
|
|
|
fn main() {
|
|
|
|
|
2014-06-27 19:30:25 +00:00
|
|
|
let some: Option<&u32> = Some(unsafe { std::mem::transmute(0x12345678u) });
|
2013-07-16 10:17:55 +00:00
|
|
|
let none: Option<&u32> = None;
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-06-27 19:30:25 +00:00
|
|
|
let full = Full(454545, unsafe { std::mem::transmute(0x87654321u) }, 9988);
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2014-06-27 19:30:25 +00:00
|
|
|
let int_val = 0i;
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 17:34:51 +00:00
|
|
|
let empty: &MoreFieldsRepr = unsafe { std::mem::transmute(&Empty) };
|
2013-07-15 13:48:57 +00:00
|
|
|
|
2013-08-08 12:14:52 +00:00
|
|
|
let droid = Droid {
|
|
|
|
id: 675675,
|
|
|
|
range: 10000001,
|
2014-06-27 19:30:25 +00:00
|
|
|
internals: unsafe { std::mem::transmute(0x43218765u) }
|
2013-08-08 12:14:52 +00:00
|
|
|
};
|
2013-07-15 13:48:57 +00:00
|
|
|
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 17:34:51 +00:00
|
|
|
let void_droid: &NamedFieldsRepr = unsafe { std::mem::transmute(&Void) };
|
2013-07-15 13:48:57 +00:00
|
|
|
|
|
|
|
zzz();
|
|
|
|
}
|
|
|
|
|
2013-08-08 12:14:52 +00:00
|
|
|
fn zzz() {()}
|