Ran into the 'static bound when trying to use std::any::Any with types containing borrowed references. The original issue about TypeId indicates that it would be possible but might be problematic to implement https://github.com/rust-lang/rust/issues/9913.
It would seem that now with DST it might also be possible to get the TypeId of unsized types as well (might also be relevant for TyDesc which allows non 'static bound but not DST).
I don't really think it would be possible to do usefully. Since lifetimes have subtype relations, you really want to be able to ask "outlives" (a la instanceof) questions about them, which isn't something well-suited to simple equality. And the case of recursive functions with lifetimes (so lifetimes aren't always monomorphizable) means that I don't think you could assign a globally unique TypeId incorporating lifetime information in any interesting way, since the lifetimes (hence TypeIds) would only really be comparable within the same function where the lifetimes were defined (unless you did some sort of global data flow analysis). I could easily be wrong about this, of course!
You are probably right that it might not be possible to do usefully but I believe that for my purposes I would be happy if I could retrieve the TypeId for Foo<'static> if I have a Foo<'a>. I believe I could handle any other lifetime issues manually. Being unable to retrieve any sort of TypeId at all once you have a non-static type feels a bit limiting though.
This requires an RFC I think.
Maybe have an unsafe fn for the non-'static case? Uses of non-'static TypeIds would pretty much only be realistically usable in a scenario that bypasses the borrow checker anyway. Internally an unsafe_type_id::<Foo<'a>>() would effectively just yield the same ID as type_id::<Foo<'static>>.
I think it would be good to get some movement on this. The unsafe fn idea sounds like a reasonable one, and a corresponding unsafe fn on TypeId so it could be used on stable would be great.
I can put together an RFC if nobody's actively working on it.
For anyone looking for a workaround to this limitation I am using this pattern to get TypeIds for non-static types. It requires a bit of boilerplate but it is workable.
I played around with a similar idea, but didn't like that it's possible to implement incorrectly, which in my case would mean interpreting memory incorrectly. Which I guess is why you've marked it as unsafe.
I'd like to propose simply removing the 'static bound from the type_id intrinsic. That way implementations of Any and TypeId for non-static types can easily be put together outside of std. Just trying to work out if that's a thing that needs an RFC or not :)
We've accepted an RFC for this: https://github.com/rust-lang/rfcs/blob/master/text/1849-non-static-type-id.md, tracking issue https://github.com/rust-lang/rust/issues/41875.
Most helpful comment
For anyone looking for a workaround to this limitation I am using this pattern to get
TypeIds for non-static types. It requires a bit of boilerplate but it is workable.