Closed
Description
Compiled on Stable with Release target.
use std::any::Any;
#[inline(never)]
fn cast<T1: ?Sized, T2>(t: Box<T1>) -> Box<T2>
{
unsafe { Box::from_raw(Box::into_raw(t) as *mut T2) }
}
fn main()
{
let t: Box<Any> = Box::new(5);
let s: Box<u32> = cast(t);
println!("{}", *s);
let t: Box<Any> = Box::new(10.0f32);
let s: Box<f32> = cast(t);
println!("{}", *s);
}
In the ASM, I see the following produced.
playground::cast:
mov rax, rdi
ret
playground::cast:
mov rax, rdi
ret
I observed this earlier today on Rust Playground. I don't know if #[inline(never)]
is causing them not to be merged, or if it's expected that they don't get merged despite being identical. It seems to me like they should be merged, as they are identical aside from the type parameters, which is only a difference at compile time, not runtime.