Skip to content

Generic function produces multiple identical methods in ASM. #46070

Description

@rickvanprim

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions