Skip to content
Open
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
319 changes: 319 additions & 0 deletions text/3888-const-self-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
- Feature Name: `const_self_fields`
- Start Date: 2025-11-26
- RFC PR: [rust-lang/rfcs#3888](https://github.com/rust-lang/rfcs/pull/3888)
- Rust Issue: [rust-lang/rust#3888](https://github.com/rust-lang/rust/issues/3888)

# Summary
[summary]: #summary

This RFC proposes per-type fields that can be accessed through a value or trait object using new `const self` and `static const self` syntax:

```rust
impl Foo{
const self METADATA_FIELD: i32 = 5;
static const self STATIC_METADATA_FIELD: i32 = 10;
}
trait Bar {
const self METADATA_FIELD: i32;
static const self STATIC_METADATA_FIELD: i32;
}
```
This allows code like:
```rust
fn use_bar(bar: &dyn Bar) {
let x: i32 = bar.METADATA_FIELD; // const self
let y: &'static i32 = &bar.STATIC_METADATA_FIELD; // static const self
}
fn use_foo(foo: &Foo) {
let x: i32 = foo.METADATA_FIELD; // const self
let y: &'static i32 = &foo.STATIC_METADATA_FIELD; // static const self
}
```
When combined with traits, enables object-safe, per-implementation constant data that can be read through `&dyn Trait` in a more efficient manner than a dynamic function call, by storing the constant in trait object metadata instead of as a vtable method.
# Motivation
[motivation]: #motivation
Today, Rust has associated constants on types and traits:
```rust
trait Foo {
const VALUE: i32;
}

impl Foo for MyType {
const VALUE: i32 = 5;
}
```
For monomorphized code where `Self` is known, `MyType::VALUE` is an excellent fit.

However: You cannot directly read an associated const through a `&dyn Foo`. There is no stable, efficient way to write `foo.VALUE` where `foo: &dyn Foo` and have that dynamically dispatch to the concrete implementation’s const value.

The common workaround is a vtable method:
```rust
trait Foo {
fn value(&self) -> i32;
}
```

This forces a dynamic function call, which is very slow compared to the `const self` and `static const self` equivalent, and does not have as much compiler optimization potential.

When using a trait object, `const self` and `static const self` store the bits directly inside the vtable, so accessing it is around as performant as accessing a field from a struct, which is of course, much more performant than a dynamic function call.

Imagine a hot loop walking over thousands of `&dyn Behavior` objects every frame to read a tiny “flag”. If that’s a virtual method, you pay a dynamic function call on every object. With `const self` and `static const self`, you’re just doing a metadata load, so the per-object overhead is noticeably much smaller.



# Guide-level explanation
[guide-level-explanation]: #guide-level-explanation

### What is const self?

`const self` introduces metadata fields: constants that belong to a type (or trait implementation) but are accessed through a `self` expression.

Example:

```rust
struct Foo;

impl Foo {
const self CONST_FIELD: u32 = 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it’s pointless to allow const self fields on inherent impls (except maybe for macros?), so I’d favor only allowing them on trait impls (unless someone can convince me otherwise).

Copy link
Author

@izagawd izagawd Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only added them in inherent trait impls because usually, items available in traits are also available in inherent implementations. I am indifferent with this decision, so I can just remove it if most people think its unnecessary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think keeping it for inherent impls for consistency would be nice

Copy link

@theemathas theemathas Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, associated types are currently allowed in stable rust only in trait impls.

I don't see any reason to allow const self in inherent impls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, associated types are currently allowed in stable rust only in trait impls.

afaict that's mostly because of a limitation of the trait solver, rather than than Rust deciding we don't want associated types in inherent impls. it is available as a nightly feature, though iirc it's incomplete and buggy.

I don't see any reason to allow const self in inherent impls.

if you want to have things on your struct that act like fields (so you can do my_struct.some_field) but aren't actually stored in your struct, this is a good way (though it doesn't match the preferred name casing). abusing Deref also works in some cases.

}

fn write_header(h: &Foo) {
// Reads a per-type constant through a value:
assert_eq!(h.CONST_FIELD, 1);
let value: u32 = h.CONST_FIELD;
}
```

A `const self` field's type can have interior mutability, because the compiler does not operate on the field directly by its reference, even if it is stored in a trait object's metadata.
It first copies the field, and does the operations on that copied value, similar to how `const` variables work in rust.
This makes using it with interior mutability sound.

When using references like shown below:

```rust
let value : &u32 = &h.CONST_FIELD;
```

This works similarly to how `const` variables work in Rust: it copies `CONST_FIELD`, then takes a reference to that copy. Unlike a normal `const` item though, the resulting reference does **not** have a `'static` lifetime; it has a temporary lifetime, as if you had written:

```rust
let tmp: u32 = h.CONST_FIELD; // copied
let value: &u32 = &tmp;
```
### What is static const self


`static const self` is similar to `const self`, however, working on `static const self` fields means working directly with its reference. Think: global `static` variable you are not allowed to mutate.
This means that the type of a `static const self` field must not have any interior mutability. In other words, the type of the field must implement `Freeze`. This is enforced by the compiler.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statics can't be generic. Allowing them in traits would incorrectly bypass this restriction given that trait impls can be generic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I’m wrong, but my understanding is that Rust forbids generic statics because the compiler can’t guarantee a single, unique memory location for each instantiation. With monomorphization, the “same” generic static could end up duplicated in multiple codegen units, each with a different address

If that is the case, I did document that you cannot rely on static const self in having unique memory addresses.
Is that a bad design, or should I make it more clear?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a bad design. Part of being a static is having a single unique location in memory. That's why it is okay to allow them to be mutable (static mut or interior mutable static). We should definitely not allow anything to be mutable without a guarantee about its location in memory. And if we don't allow it to be mutable in any way, it can just be a const.

Copy link
Author

@izagawd izagawd Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static might not have been the best name then, since static does imply a fixed memory location. Someone did recommend it to be called const self ref

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like it's just a constant of reference type, and doesn't have to be a separate language feature.


Example:

```rust
struct Foo;

impl Foo {
static const self STATIC_CONST_FIELD: u32 = 1;
}

fn write_header(h: &Foo) {
// Reads a per-type constant through a value:
assert_eq!(h.STATIC_CONST_FIELD, 1);
let reference: &'static u32 = &h.STATIC_CONST_FIELD;
}
```
`static const self` field's references have `'static` lifetimes.

Note that unlike normal `static` variables, you cannot rely on the reference of a `static const self` field to be the same reference of the same `static const self` field of the same underlying type.


### Trait objects and metadata fields

The main power shows up with traits and trait objects:

```rust
trait Serializer {
// Per-implementation metadata field:
const self FORMAT_VERSION: u32;
}

struct JsonSerializer;
struct BinarySerializer;

impl Serializer for JsonSerializer {
const self FORMAT_VERSION: u32 = 1;
}

impl Serializer for BinarySerializer {
const self FORMAT_VERSION: u32 = 2;
}

fn write_header(writer: &mut dyn std::io::Write, s: &dyn Serializer) {
// Dynamically picks the implementation’s FORMAT_VERSION
writer.write_all(&[s.FORMAT_VERSION as u8]).unwrap();
}
```

Accessing `FORMAT_VERSION` on a trait object is intended to be as cheap as reading a field from a struct: no virtual call, just a read from the vtable metadata for that trait object.
It is much more efficient than having a `format_version(&self)`, trait method, which does a virtual call.

On a non trait object, accessing `FORMAT_VERSION` will be as efficient as accessing a `const` value.

Naming conventions for `const self` and `static const self` fields follow the same conventions as other `const` and `static` variables (e.g. `SCREAMING_SNAKE_CASE` as recommended by the Rust style guide); this RFC does not introduce any new naming rules.

To be more specific about which trait's `const self`/`static const self` field should be accessed, a new `instance.(some_path::Trait.NAME)` syntax can be used.

`T::FIELD` would give a compile-time error when `FIELD` is a `static const self` or `const self` field. These fields are only accessible through value syntax (`expr.FIELD`), not type paths.
### How should programmers think about it?

Programmers can think of `const self`/`static const self` metadata fields as “const but per-type” constants that can be read through references and trait objects, and a replacement for patterns like:
```rust
trait Foo {
fn version(&self) -> u32; // just returns a literal
}
```
Where the data truly is constant and better modeled as a field in metadata.

### Teaching differences: new vs existing Rust programmers

For new Rust programmers, `const self` and `static const self` can be introduced after associated constants:
* Types can have constants: `Type::CONST`
* Sometimes you want those constants visible through trait objects; that’s where `const self` metadata fields come in.
* Sometimes you want to be able to directly reference those constants. Good for when it is too large; that's where `static const self` metadata fields come in.
* You can access `self.CONST_FIELD` even if self is `&dyn Trait`, as long as the trait declares it.

# Reference-level explanation
[reference-level-explanation]: #reference-level-explanation
### Restrictions

For `const self`, we only ever operate on copies, so its type having interior mutability is fine.

For `static const self`, we get a `&'static T` directly from the metadata; to keep that sound we additionally require `T: Freeze` so that `&T` truly represents immutable data.

Both `const self` and `static const self` field's type are required to be `Sized`, and must have a `'static` lifetime.

Assume we have:

```rust
struct Foo;
impl Foo{
const self X: Type = value;
static const self Y: OtherType = value;
}
```

then it can be used like:

```rust

let variable = obj.X; //ok. Copies it
let variable2 : &_ = &obj.X; // ok, but what it actually does is copy it, and uses the reference of the copy. Reference lifetime is not 'static.


let variable3 = obj.Y; // ok if the type of 'Y' implements Copy
let variable4 : &'static _ = &obj.Y; // ok. Lifetime of reference is 'static, uses the reference directly
```


### Resolution Semantics


For a path expression `T::NAME` where `NAME` is a `const self` or `static const self` field of type `T`, it would give a compiler error.
This is because allowing `T::NAME` syntax would also mean that `dyn Trait::NAME` syntax should be valid, which shouldn't work, since the `dyn Trait` type does not have any information on the `const` value.

`const self` and `static const self` fields are not simply type-level constants; they are value-accessible metadata.

For an expression `expr.NAME` where `NAME` is declared as `const self NAME: Type` or `static const self NAME: Type`:

* First, the compiler tries to resolve `NAME` as a normal struct field on the type of expr.
* If that fails, it tries to resolve `NAME` as a `const self`/`static const self` field from:
* inherent impls of the receiver type
* If that fails, it then tries to resolve scoped traits implemented by the receiver type, using the same autoderef/autoref rules as method lookup.
* A struct cannot have a normal field and an inherent `const self`/`static const self` field with the same name.
* If multiple traits, both implemented by type `T` and are in scope, provide `const self` or `static const self` fields with the same name and `expr.NAME` is used (where `expr` is an instance of type `T`), that is also an ambiguity error. The programmer must disambiguate using `expr.(Trait.NAME)`.

### Trait objects

For a trait object: `&dyn Trait`, where `Trait` defines:

```rust
trait Trait {
fn do_something(&self);
const self AGE: i32;
const self LARGE_VALUE: LargeType;
}
```

We would have this VTable layout
```
[0] drop_in_place_fn_ptr
[1] size: usize
[2] align: usize
[3] do_something_fn_ptr
[4] AGE: i32 //stored inline
[5] LARGE_VALUE: LargeType //stored inline
```
This layout is conceptual; the precise placement of metadata in the vtable is left as an implementation detail, as long as the observable behavior (one metadata load per access) is preserved.
### Lifetimes

Taking a reference to a `static const self` field always yields a `&'static T`. This is sound since `static const self` types are required to implement `Freeze`, and are required to be `'static`.
```rust
let p: &'static i32 = &bar.STATIC_METADATA_FIELD;
```
However, you get a potentially different `'static` reference every time you use the same `static const self` field from the same type. This is because the storage for a `static const self` field potentially lives in a trait object’s metadata, and different trait objects of the same underlying type do not necessarily share the same exact metadata.
# Drawbacks
[drawbacks]: #drawbacks

1. Programmers must distinguish:
* Fields (expr.field),
* Associated consts (T::CONST),
* And const fields (expr.METADATA).
2. Vtable layout grows to include inline metadata, which:
* Increases vtable size when heavily used.
* Needs careful specification for any future stable trait-object ABI.
3. Dot syntax now covers both per-instance fields and per-type metadata; tools and docs will need to present these clearly to avoid confusion.

# Rationale and alternatives
[rationale-and-alternatives]: #rationale-and-alternatives

### Why this design?
* Explicitly value-only access (expr.NAME) keeps the mental model simple, as it functions similarly to a field access

* If you have a trait object, you can read its per-impl metadata.

* If you just have a type, associated consts remain the right tool.

* By forbidding `T::NAME`, we avoid:
* Confusion over `dyn Trait::NAME`.
* Having to explain when a const is “type-level” vs “metadata-level” under the same syntax.
* A metadata load is cheaper and more predictable than a virtual method call. Especially important when touching many trait objects in tight loops.

### Why not a macro/library?
A library or macro cannot extend the vtable layout or teach the optimizer that certain values are metadata; it can only generate more methods or global lookup tables. `const self`/`static const self` requires language and compiler support to achieve the desired ergonomics and performance.

### Alternatives
Keep using methods:
```rust
fn value(&self) -> u32; // remains the standard way.
```
Downsides:
* Conceptual mismatch (constant-as-method).
* Extra indirection and call overhead.

# Prior art
[prior-art]: #prior-art

As of the day this RFC was published, there is no mainstream language with a similar feature. The common workaround is having a virtual function return the literal, but that does not mean we should not strive for a more efficient method.

This RFC can be seen as:
* Making explicit a pattern that compiler and runtimes already rely on internally (metadata attached to vtables).
* Exposing it in a controlled, ergonomic way for user code.
# Unresolved questions
[unresolved-questions]: #unresolved-questions

* Is there a better declaration syntax than `static const self NAME : Type`/`const self NAME : Type`?
* Is `obj.METADATA_FIELD` syntax too conflicting with `obj.normal_field`?
* Is `obj.(Trait.METADATA_FIELD)` a good syntax for disambiguating?
# Future possibilities
[future-possibilities]: #future-possibilities

* Faster type matching than `dyn Any`: Since `dyn Any` does a virtual call to get the `TypeId`, using `static const self` to store the `TypeId` would be a much more efficient way to downcast.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a bit more efficient. I don't think the perf difference would be all that much on modern cpus, especially when you also take the typeid comparison itself into account.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did benchmarks. Since static const self is not actually a feature in rust, I simulated it by creating structs that were similar to it. These were the results:

dyn_typeid_eq:             ~1.15 ns
static_const_self_typeid_eq: ~0.32 ns

the static const self equivalent seems to be more than triple times more performant than the dyn_typeid equivalent

Version details:

rustc 1.93.0-nightly (6647be936 2025-11-09)
binary: rustc
commit-hash: 6647be93640686a2a443a49f15c3390b68c8b5dd
commit-date: 2025-11-09
host: x86_64-pc-windows-msvc
release: 1.93.0-nightly
LLVM version: 21.1.3

My CPU is: AMD Ryzen 7 9800X3D if that helps

code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=d85bae34e59fd2ca008bbeff72254917

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a fair bit more than I would have expected.