Skip to content

RFC: Generic const and static #1520

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

Closed
wants to merge 3 commits into from
Closed

RFC: Generic const and static #1520

wants to merge 3 commits into from

Conversation

comex
Copy link

@comex comex commented Mar 1, 2016

As it says on the tin.

Rendered

@arielb1
Copy link
Contributor

arielb1 commented Mar 1, 2016

I'm against generic statics because it is not obvious how they are monomorphized: currently, monomorphized items always have internal linkage, and monomorphizing them from 2 different crates creates 2 different copies. Doing this in a static could cause some confusion.

@comex
Copy link
Author

comex commented Mar 1, 2016

I didn't think of that, but AFAIK C++ has always had that issue (it supports both static variables in generic functions and classes, and as of C++14, generic variables - plus I believe it guarantees uniqueness for pointers to specialized functions), and solves it by marking those symbols as weak, so the dynamic linker picks one for the whole program. Rust could do the same. Weak symbols have some performance impact, but as long as generic statics are uncommon it shouldn't matter. The number could be further whittled down by detecting truly immutable objects and going back to internal linkage for those - after all, if Rust doesn't guarantee uniqueness for fn instantiations, that line has already been crossed (i.e. surprising behavior with pointer comparisons), so there's little harm extending the same minor pitfall to immutable variables. (And of course, specializations involving non-exported types can also be internal.)

@aturon
Copy link
Member

aturon commented Mar 3, 2016

cc @eddyb @nikomatsakis

@eddyb
Copy link
Member

eddyb commented Mar 3, 2016

IIRC we never considered associated statics because maintaining uniqueness was deemed unreasonably difficult/impossible (cc @alexcrichton) and plans for generic consts were supersceded by const fn.

…features (which I hope will be filled in too eventually).
@alexcrichton alexcrichton added the T-lang Relevant to the language team, which will review and decide on the RFC. label Mar 7, 2016
@nikomatsakis
Copy link
Contributor

We discussed this in the @rust-lang/lang meeting recently. I think that, at least for constants, we are all generally sympathetic to this goal, but we feel it's a bit premature to dive into it right now. The story with constants -- and especially constants and generics! -- is quite up in the air. We have const fns, associated constants, and the MIR transition, all of which affect how constants are implemented in the compiler and handled in the type system. Moreover, there is an obvious demand for constant-parameterized items (think fn foo<const N: usize>()). It seems most important to press on that last point, and MIR, and to some extent associated constants (which are entangled), and then return to this topic of generic constants.

(That said, I think one of the biggest points of tension here is between const fn and generic constants, since there seems to be quite some overlap between the two, though that is not to say we could not have both of them; perhaps they are both useful.)

@nikomatsakis
Copy link
Contributor

Postponed under #1557

yvt added a commit to yvt/ngspades that referenced this pull request Apr 25, 2020
This commit introduces a trait `StableVtable`, which provides a
canonicalized version of `TraitObject::vtable` that is suitable for
representing a type identity, eliminating the need for storing `TypeId`
in handle types and thus reducing the size of each of them by 8 bytes.

In addition, this commit makes `SmallBox::vtable` non-null, which allows
further space optimization when a handle is stored in `Option` or
a similar enumerate type.

This approach has the following shortcomings:

- The runtime overhead of down-casting is slightly increased owing to an
  additional indirection required to access the canonicalized `vtable`.
  Still, it's supposedly faster than `std::any::Any`.

- Storing a generic type in a handle is even harder due to the lack of
  generic static items (<rust-lang/rfcs#1520>).
  This approach requires a static variable where the canonical `vtable`
  for a specific type is stored. A potential work-around would be
  providing a slow path for types that can't generate a stable `vtable`
  pointers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants