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.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
sfackler commentedon Nov 21, 2017
I think there has been some investigation of duplicate function deduplication but I don't know how far it's gone.
There are some caveats here - it seems like it would make stack traces more confusing, since you'll have frames which correspond to function calls you don't actually make in the source.
nox commentedon Apr 3, 2018
Probably helped by #49479.
rickvanprim commentedon May 21, 2019
Sorry for the slow response. Just tested now and issue no longer reproduces!