Skip to content

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

Closed as not planned

Description

@traviscross

[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));
}

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    C-discussionCategory: Discussion or questions that doesn't represent real issues.
    T-langRelevant to the language team
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    on Mar 28, 2025
  2. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Mar 28, 2025
  3. compiler-errors commented on Mar 28, 2025

    @compiler-errors
    Contributor

    @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?

  4. compiler-errors commented on Mar 28, 2025

    @compiler-errors
    Contributor

    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 😅

  5. traviscross commented on Mar 28, 2025

    @traviscross
    ContributorAuthor

    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.

  6. traviscross commented on Mar 28, 2025

    @traviscross
    ContributorAuthor

    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.

  7. changed the title [-]`type_id` hack on stable Rust with no unsafe[/-] [+]Broken attempt at `type_id` hack on stable Rust with no unsafe[/+] on Mar 28, 2025
  8. removed
    T-langRelevant to the language team
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    on Mar 28, 2025
  9. 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[/+] on Mar 28, 2025
  10. added
    T-langRelevant to the language team
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    on Mar 28, 2025
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

    C-discussionCategory: Discussion or questions that doesn't represent real issues.T-langRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions