Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic function produces multiple identical methods in ASM. #46070

Closed
rickvanprim opened this issue Nov 18, 2017 · 3 comments
Closed

Generic function produces multiple identical methods in ASM. #46070

rickvanprim opened this issue Nov 18, 2017 · 3 comments
Labels
A-codegen Area: Code generation C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@rickvanprim
Copy link

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.

@TimNN TimNN added A-codegen Area: Code generation C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 21, 2017
@sfackler
Copy link
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
Copy link
Contributor

nox commented Apr 3, 2018

Probably helped by #49479.

@rickvanprim
Copy link
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
Labels
A-codegen Area: Code generation C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants