Skip to content

Generic function produces multiple identical methods in ASM. #46070

Closed
@rickvanprim

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

added
A-codegenArea: Code generation
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Nov 21, 2017
sfackler

sfackler commented on Nov 21, 2017

@sfackler
Member

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

nox commented on Apr 3, 2018

@nox
Contributor

Probably helped by #49479.

rickvanprim

rickvanprim commented on May 21, 2019

@rickvanprim
Author

Sorry for the slow response. Just tested now and issue no longer reproduces!

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

        Participants

        @nox@TimNN@sfackler@rickvanprim

        Issue actions

          Generic function produces multiple identical methods in ASM. · Issue #46070 · rust-lang/rust