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

TypeId only allows sized types that have a 'static bound #20168

Closed
Marwes opened this issue Dec 23, 2014 · 8 comments
Closed

TypeId only allows sized types that have a 'static bound #20168

Marwes opened this issue Dec 23, 2014 · 8 comments
Labels
A-typesystem Area: The type system

Comments

@Marwes
Copy link
Contributor

Marwes commented Dec 23, 2014

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 #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).

@pythonesque
Copy link
Contributor

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!

@Marwes
Copy link
Contributor Author

Marwes commented Jan 2, 2015

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.

@kmcallister kmcallister added the A-typesystem Area: The type system label Jan 16, 2015
@arielb1
Copy link
Contributor

arielb1 commented Sep 13, 2015

This requires an RFC I think.

@UserAB1236872
Copy link

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>>.

@KodrAus
Copy link
Contributor

KodrAus commented Dec 16, 2016

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.

@Marwes
Copy link
Contributor Author

Marwes commented Dec 16, 2016

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.

@KodrAus
Copy link
Contributor

KodrAus commented Dec 18, 2016

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 :)

@Mark-Simulacrum
Copy link
Member

We've accepted an RFC for this: https://github.com/rust-lang/rfcs/blob/master/text/1849-non-static-type-id.md, tracking issue #41875.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

7 participants