Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
Go
Haskell
HLSL
Hook
Hylo
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
BPF gcc (trunk)
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.8.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
Options
Source code
#![feature(extern_types, raw_identifiers, untagged_unions, specialization)] pub trait Zeroed {} pub trait New<T> { fn new(this: *mut T) -> Self; } pub trait ZeroedInit<T> { type Type: DefaultInit<T> + New<T>; fn zeroed_init(this: *mut T) -> Self::Type; } pub struct DoNotZeroInit<T>(*mut T); impl<T> New<T> for DoNotZeroInit<T> { #[inline(always)] fn new(this: *mut T) -> Self { return DoNotZeroInit(this); } } pub struct DoZeroInit<T>(*mut T); impl<T> New<T> for DoZeroInit<T> { #[inline(always)] fn new(this: *mut T) -> Self { return DoZeroInit(this); } } impl<T> ZeroedInit<T> for T { default type Type = DoNotZeroInit<T>; #[inline(always)] default fn zeroed_init(this: *mut T) -> Self::Type { return <Self::Type as New<T>>::new(this); } } impl<T: Zeroed> ZeroedInit<T> for T { type Type = DoZeroInit<T>; #[inline(always)] fn zeroed_init(this: *mut T) -> Self::Type { return <Self::Type as New<T>>::new(this); } } pub trait DefaultInit<T> { fn default_init(self); } impl<T: std::default::Default> DefaultInit<T> for DoNotZeroInit<T> { #[inline(always)] fn default_init(self) { unsafe { std::ptr::write(self.0, <T as std::default::Default>::default()); } } } impl<T> DefaultInit<T> for DoZeroInit<T> { #[inline(always)] fn default_init(self) {} } pub struct Foo(u32); pub struct Bar(u32); pub struct Baz(u32); impl Zeroed for Foo {} impl std::default::Default for Bar { fn default() -> Bar { return Bar(42); } } pub extern "C" fn do_foo(this: *mut Foo) { DefaultInit::default_init(<Foo as ZeroedInit<Foo>>::zeroed_init(this)); } pub extern "C" fn do_bar(this: *mut Bar) { DefaultInit::default_init(<Bar as ZeroedInit<Bar>>::zeroed_init(this)); } pub extern "C" fn do_baz(this: *mut Baz) { // See https://github.com/rust-lang/rust/issues/33017 let _ignore = this; //DefaultInit::default_init(<Baz as ZeroedInit<Baz>>::zeroed_init(this)); } #[repr(C)] pub union TransmuteHack<T: Copy, U: Copy> { pub from: T, pub to: U, } pub fn foo((x, y): (u8, u8)) -> bool { return (x | y) > 0x0f; } pub static SSS: () = (); pub static SSSS: usize = unsafe { TransmuteHack { from: [&SSS as *const _, 0 as *const _][1] }.to }; extern { static _prop1_offset: usize; static _prop2_offset: usize; static _prop3_offset: usize; static _prop4_offset: usize; pub type T; pub fn unsafe_borrow(_: &T) -> &T; pub fn unsafe_borrow_mut(_: &mut T) -> &T; } pub fn borrow(var: &T) -> &T { unsafe { unsafe_borrow(var) } } pub fn borrow_mut(var: &mut T) -> &T { unsafe { unsafe_borrow_mut(var) } } pub struct S { pub value: usize, } #[repr(transparent)] pub struct Ivar1 { pub _prop1: S,//String, } #[repr(transparent)] pub struct Ivar2 { pub _prop2: S,//Vec<String>, } #[repr(transparent)] pub struct Ivar3 { pub _prop3: S,//Vec<String>, } #[repr(transparent)] pub struct Ivar4(std::marker::PhantomData<u8>, std::marker::PhantomData<u8>, std::marker::PhantomData<u8>, S); impl std::ops::Deref for T { type Target = Ivar1; #[inline(always)] fn deref(&self) -> &Self::Target { let ptr = unsafe { (self as *const _ as *const u8).add(_prop1_offset) as *const () }; let ivar = unsafe { &*(ptr as *const Self::Target) }; return ivar; } } impl std::ops::DerefMut for T { #[inline(always)] fn deref_mut(&mut self) -> &mut Self::Target { let ptr = unsafe { (self as *mut _ as *mut u8).add(_prop1_offset) as *mut () }; let ivar = unsafe { &mut *(ptr as *mut Self::Target) }; return ivar; } } impl std::ops::Deref for Ivar1 { type Target = Ivar2; #[inline(always)] fn deref(&self) -> &Self::Target { let ptr = unsafe { (self as *const _ as *const u8).add(_prop2_offset - _prop1_offset) as *const () }; let ivar = unsafe { &*(ptr as *const Self::Target) }; return ivar; } } impl std::ops::DerefMut for Ivar1 { #[inline(always)] fn deref_mut(&mut self) -> &mut Self::Target { let ptr = unsafe { (self as *mut _ as *mut u8).add(_prop2_offset - _prop1_offset) as *mut () }; let ivar = unsafe { &mut *(ptr as *mut Self::Target) }; return ivar; } } impl std::ops::Deref for Ivar2 { type Target = Ivar3; #[inline(always)] fn deref(&self) -> &Self::Target { let ptr = unsafe { (self as *const _ as *const u8).add(_prop3_offset - _prop2_offset) as *const () }; let ivar = unsafe { &*(ptr as *const Self::Target) }; return ivar; } } impl std::ops::DerefMut for Ivar2 { #[inline(always)] fn deref_mut(&mut self) -> &mut Self::Target { let ptr = unsafe { (self as *mut _ as *mut u8).add(_prop3_offset - _prop2_offset) as *mut () }; let ivar = unsafe { &mut *(ptr as *mut Self::Target) }; return ivar; } } impl std::ops::Deref for Ivar3 { type Target = Ivar4; #[inline(always)] fn deref(&self) -> &Self::Target { let ptr = unsafe { (self as *const _ as *const u8).add(_prop4_offset - _prop3_offset) as *const () }; let ivar = unsafe { &*(ptr as *const Self::Target) }; return ivar; } } impl std::ops::DerefMut for Ivar3 { #[inline(always)] fn deref_mut(&mut self) -> &mut Self::Target { let ptr = unsafe { (self as *mut _ as *mut u8).add(_prop4_offset - _prop3_offset) as *mut () }; let ivar = unsafe { &mut *(ptr as *mut Self::Target) }; return ivar; } } impl T { pub fn foo(&mut self) { //self._prop3 = S { value: 42 };//"Hello".to_string(); //self.3 = S { value: 13 };//"Hello".to_string(); //let _x = &mut self._prop1; //let _y = &mut self.3; //std::mem::swap(&mut self._prop1, &mut self.3); let _x = borrow(self); //let _y = borrow_mut(self); } } extern { pub type Alt; } trait Fields { type Type; fn fields(self) -> Self::Type; } struct AltFieldsRef<'a> { _prop1: &'a S, _prop2: &'a S, _prop3: &'a S, _prop4: &'a S, } impl<'a> Fields for &'a Alt { type Type = AltFieldsRef<'a>; #[inline(always)] fn fields(self) -> AltFieldsRef<'a> { let base = self as *const _ as *const u8; return AltFieldsRef { _prop1: unsafe { &*(base.add(_prop1_offset) as *const S) }, _prop2: unsafe { &*(base.add(_prop2_offset) as *const S) }, _prop3: unsafe { &*(base.add(_prop3_offset) as *const S) }, _prop4: unsafe { &*(base.add(_prop4_offset) as *const S) }, }; } } struct AltFieldsMut<'a> { _prop1: &'a mut S, _prop2: &'a mut S, _prop3: &'a mut S, _prop4: &'a mut S, } impl<'a> Fields for &'a mut Alt { type Type = AltFieldsMut<'a>; #[inline(always)] fn fields(self) -> AltFieldsMut<'a> { let base = self as *mut _ as *mut u8; return AltFieldsMut { _prop1: unsafe { &mut *(base.add(_prop1_offset) as *mut S) }, _prop2: unsafe { &mut *(base.add(_prop2_offset) as *mut S) }, _prop3: unsafe { &mut *(base.add(_prop3_offset) as *mut S) }, _prop4: unsafe { &mut *(base.add(_prop4_offset) as *mut S) }, }; } } impl Alt { pub fn foo(&mut self) { let mut _fields = self.fields(); //let _foo = self; //std::mem::swap(&mut *_fields._prop1, &mut *_fields._prop4); //let _x = &mut *fields._prop1; //let _y = &mut *fields._prop4; } } extern { pub type A; } pub struct GGSelf<T: ?Sized>(*mut T); pub struct GGField<T: ?Sized>(*mut T); impl <T: ?Sized> GGField<T> { pub fn as_ref(&self) -> &T { return unsafe { &*self.0 }; } pub fn as_mut(&mut self) -> &mut T { return unsafe { &mut *self.0 }; } } impl <T: ?Sized> GGSelf<T> { pub fn as_ref(&self) -> &T { return unsafe { &*self.0 }; } pub fn as_mut(&mut self) -> &mut T { return unsafe { &mut *self.0 }; } } impl<T: ?Sized> std::convert::AsRef<T> for GGField<T> { fn as_ref(&self) -> &T { return unsafe { &*self.0 }; } } impl<T: ?Sized> std::convert::AsMut<T> for GGField<T> { fn as_mut(&mut self) -> &mut T { return unsafe { &mut *self.0 }; } } pub struct GGFields { _prop1: GGField<S>, _prop2: GGField<S>, _prop3: GGField<S>, _prop4: GGField<S>, this: GGSelf<A>, } impl GGFields { pub fn as_ref(&self) -> &A { return self.this.as_ref(); } pub fn as_mut(&mut self) -> &mut A { return self.this.as_mut(); } } pub struct AFields<'a> { _prop1: &'a mut S, _prop2: &'a mut S, _prop3: &'a mut S, _prop4: &'a mut S, this: &'a mut A, } pub struct WWW<T0: ?Sized> { _prop1: T0, } pub struct DDD { //_prop1: WWW<&mut u8>, } #[repr(C)] pub struct VVV<'a, 'b: 'a>(&'a mut AFields<'b>); //struct DumbRef<'a, 'b, T: 'a + 'b + ?Sized>(pub &'a T, std::marker::PhantomData<&'b T>); //struct DumbMut<'a, 'b, T: 'a + 'b + ?Sized>(pub &'a mut T, std::marker::PhantomData<&'b T>); //struct DumbRef<'a, 'b, T: 'a + 'b + ?Sized>(pub &'a T, &'b AFields<'a>); //struct DumbMut<'a, 'b, T: 'a + 'b + ?Sized>(pub &'a mut T, &'b mut AFields<'a>); impl<'a> AFields<'a> { #[inline(always)] pub fn to_ref(&self) -> &'a A { return unsafe { &*(self.this as *const _) }; } #[inline(always)] pub fn vvv<'b>(&'b mut self) -> VVV<'a, 'b> { return VVV(self); } #[inline(always)] pub fn to_mut(&mut self) -> &'a mut A {//(&'a mut A, &'b mut Self) where 'b: 'a { //return DumbMut(unsafe { &mut *self.this }, std::marker::PhantomData); return unsafe { &mut **(self as *mut _ as *mut () as *mut *mut A) };//(self.this, self); //return self.this; } #[inline(always)] pub fn xxx(&mut self) -> &mut Self {//(&'a mut A, &'b mut Self) where 'b: 'a { //return DumbMut(unsafe { &mut *self.this }, std::marker::PhantomData); //return extend_lifetime(self.this); return self; } #[inline(always)] pub fn this(&mut self) -> &mut A { return self.this; } pub fn extend_lifetime(&mut self) -> &'a mut Self { return unsafe { std::mem::transmute(self) }; } //pub fn extend_lifetime(r: &mut A) -> &'a mut A { // return unsafe { std::mem::transmute(r) }; //} } // This is unsafe. pub fn extend_lifetime<'a, 'b, T>(r: &'a mut T) -> &'b mut T { return unsafe { std::mem::transmute::<&'a mut T, &'b mut T>(r) }; } pub fn extend_lifetime2<'a, 'b: 'a, T, T2: ?Sized>(r: &'a mut T, _: &'b T2) -> &'b mut T { return unsafe { std::mem::transmute(r) }; } impl A { pub fn foo(&mut self) -> & A { //union NoDrop<T>{ value: T } let base = self as *mut _ as *mut u8; let _fields = AFields {//NoDrop { value: AFields { this: self, _prop1: unsafe { &mut *(base.add(_prop1_offset) as *mut S) }, _prop2: unsafe { &mut *(base.add(_prop2_offset) as *mut S) }, _prop3: unsafe { &mut *(base.add(_prop3_offset) as *mut S) }, _prop4: unsafe { &mut *(base.add(_prop4_offset) as *mut S) }, }.extend_lifetime();//}; //let mut _fields = &mut *_fields; //let mut _fields = unsafe { _fields.value }; //let _foo = _fields.this();//AFields::extend_lifetime(_fields.vvv().0.this); //let _bar = _fields.this(); //let _foo = &mut *_fields.vvv().0.this; //let _bar = AFields::extend_lifetime(_fields.vvv().0.this); //let _bar = AFields::extend_lifetime(_fields.vvv().0.this); //let _bar = _fields.xxx().this; //let _bar = extend_lifetime(_fields.vvv().0.this); //let _bar = _fields.vvv().0.this; //let _baz = _fields.to_mut().1; //let _bar = _fields.to_mut().0; //*_fields._prop1 = S { value: 42 }; let _foo = _fields.this(); //std::mem::swap(&mut *_fields._prop1, &mut *_fields._prop4); //let _x = &mut *_fields._prop1; //let _y = &mut *_fields._prop4; //println!("{:?}", _foo as *const _); return _foo; } pub fn gg(&mut self) -> & A { //union NoDrop<T>{ value: T } let base = self as *mut _ as *mut u8; let mut _fields = GGFields {//NoDrop { value: AFields { _prop1: GGField(unsafe { base.add(_prop1_offset) as *mut S }), _prop2: GGField(unsafe { base.add(_prop2_offset) as *mut S }), _prop3: GGField(unsafe { base.add(_prop3_offset) as *mut S }), _prop4: GGField(unsafe { base.add(_prop4_offset) as *mut S }), this: GGSelf(base as *mut Self), }; //let _fields = extend_lifetime(&mut _fields); let _fields = extend_lifetime2(&mut _fields, self); //std::mem::forget(self); //let _x = self; //std::mem::swap(&mut *_fields._prop1.as_mut(), &mut *_fields._prop4.as_mut()); std::mem::swap(&mut *std::convert::AsMut::as_mut(&mut _fields._prop1), &mut *_fields._prop4.as_mut()); let _foo = _fields.as_mut(); //let _bar = _fields.as_ref(); return _foo; } } pub trait OFields { type Type; fn fields() -> Self::Type; } impl A { pub fn __never_used() { //pub type J = u32; pub struct A { x: u32, } impl OFields for ::A { type Type = A; fn fields() -> Self::Type { return A { x: 42 }; } } } } pub static OOO: [u8; 2] = { [1, 2] }; impl A { pub fn __never_used2() { let _x = <A as OFields>::fields().x; } } pub struct Parity { pub _prop1: S, pub _prop2: S, pub _prop3: S, pub _prop4: S, } impl Parity { pub fn foo(&mut self) -> &mut Parity { //self._prop3 = S { value: 42 };//"Hello".to_string(); //self._prop4 = S { value: 13 };//"Hello".to_string(); //let _x = &mut self._prop1; //let _y = &mut self._prop4; //std::mem::swap(&mut self._prop1, &mut self._prop4); let _foo = self; //self._prop1 = S { value: 42 }; return _foo; } }
rust source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
BPF gcc (trunk)
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.8.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
Options
Source code
union TransmuteHack<T: Copy, U: Copy> { from: T, to: U, } pub static VALUE: usize = 42; pub static ADDRESS: usize = unsafe { TransmuteHack { from: &VALUE }.to }; pub static FOO: usize = ADDRESS / 2;
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree