Skip to content

Disregard: Broken attempt at type_id hack on stable Rust with no unsafe #139043

New issue

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

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

Already on GitHub? # to your account

Closed
traviscross opened this issue Mar 28, 2025 · 4 comments
Closed
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@traviscross
Copy link
Contributor

traviscross commented Mar 28, 2025

[Edit: Removed a bunch of things that don't matter because this doesn't do what was intended:]

use core::{any::TypeId, marker::PhantomData};

fn type_id_of<T: ?Sized>() -> TypeId {
    trait Marker {}
    impl<T: ?Sized> Marker for T {}
    fn hack<T: ?Sized + Marker + 'static>(_: *const T) -> TypeId {
        TypeId::of::<T>()
    }
    let p = &PhantomData::<T>;
    let p = &raw const *p as *const (dyn Marker + 'static);
    hack(p)
}

fn type_id_of_val<T: ?Sized>(_: &T) -> TypeId {
    type_id_of::<T>()
}

fn main() {
    let x = ();
    dbg!(type_id_of_val(&x));
}
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-discussion Category: Discussion or questions that doesn't represent real issues. T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue. labels Mar 28, 2025
@traviscross traviscross removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 28, 2025
@compiler-errors
Copy link
Member

@traviscross:

All this code does is compute the type id of the type dyn Marker + 'static. It's not violating any sort of library UB. https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6a90175675c2021bf4f385714a3507a1

What exactly are you trying to demonstrate in this code?

@compiler-errors
Copy link
Member

Also @traviscross could you please quit tagging approximately everyone in the world and then editing away those tags as soon as someone comments? It's pretty disruptive 😅

@traviscross
Copy link
Contributor Author

OK, thanks for that. One moment, I went too far in trying to simplify the example, but I may still have a version that works.

@traviscross
Copy link
Contributor Author

No, never mind. I thought I had something, but it doesn't work. Obviously have been staring at this too long. Sorry for the noise.

@traviscross traviscross closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2025
@traviscross traviscross changed the title type_id hack on stable Rust with no unsafe Broken attempt at type_id hack on stable Rust with no unsafe Mar 28, 2025
@traviscross traviscross removed T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue. labels Mar 28, 2025
@traviscross traviscross changed the title Broken attempt at type_id hack on stable Rust with no unsafe Disregard: Broken attempt at type_id hack on stable Rust with no unsafe Mar 28, 2025
@rustbot rustbot added T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue. labels Mar 28, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants