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

Revert "Remove all transmute calls in generated code" #902

Merged
merged 2 commits into from
Apr 13, 2020

Conversation

EPashkin
Copy link
Member

@EPashkin EPashkin commented Apr 13, 2020

Part of #901

This reverts commit 5ca26d9.

Comment from #903:

Unfortunately, it wasn't a successful experiment... A solution is still possible without transmute:

fn foo() {
    println!("bla");
}

fn bar(i: u32) {
    println!("bar");
}

fn main() {
    unsafe {
        let b = bar as fn(u32);
        let b = &b;
        let b = b as *const fn(u32) as *const fn();
        (*b)();
    }
}

But transmute seems to be the simplest solution. So let's go back to it.

@@ -137,7 +137,7 @@ impl ToCode for Chunk {
);
let self_str = if in_trait { "Self, " } else { "" };
let s2 = format!(
"\tSome(*(&{}::<{}F> as *const _ as *const _)), Box_::into_raw(f))",
"\tSome(transmute({}::<{}F> as usize)), Box_::into_raw(f))",
Copy link
Member

@sdroege sdroege Apr 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, can we do

Suggested change
"\tSome(transmute({}::<{}F> as usize)), Box_::into_raw(f))",
"\tSome(transmute::<_, unsafe extern "C" fn()>({}::<{}F> as *const ())), Box_::into_raw(f))",

That's less likely to fall apart and closer to the unsafe code guidelines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but without second ::

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, updated comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some(transmute::<_, unsafe extern "C" fn()>(
                    activate_trampoline::<Self, F> as *const (),
                )),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct

Copy link
Member Author

@EPashkin EPashkin Apr 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only problem of this change that we need change manual code again instead of just reverting commit :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to do that anyway, there were some other changes in the manual code that should be kept

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EPashkin Can you update?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, forgot to push.
Done

@sdroege
Copy link
Member

sdroege commented Apr 13, 2020

Thanks, let's get this in then and then fix up everything else :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants