# Static assertions + momorphization

**URL:** <https://internals.rust-lang.org/t/static-assertions-momorphization/22070>\
**Category:** language design\
**Created:** [December 24, 2024, 1:00pm UTC](https://internals.rust-lang.org/t/static-assertions-momorphization/22070 "2024-12-24T13:00:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoric](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yoric/32/1316_2.png) [@Yoric](https://internals.rust-lang.org/u/Yoric)\
**Post date:** [December 24, 2024, 1:00pm UTC](https://internals.rust-lang.org/t/static-assertions-momorphization/22070/1 "2024-12-24T13:00:40Z")

</div>

I've been slowly working my way towards a pre-RFC for letting developers extend the type system through refinement types.

While attempting to find counter-examples to my proposal, I've realized that much of what I'm doing at the moment could be implemented if only we could write things along the lines of:

```rust
fn _assert_is_valid<T>(&self) {
   static /* or `const` */_ : () = assert!(Self::some_const_fn<T>());
}

```

Now, this won't work because each syntactic instance of `static` is compiled into a single variable, instead of being one variable per monomorphization, so the assertion doesn't have access to `T` or even `Self` in the context. This makes sense if one thinks of `static` as a mechanism to define variables, but less if one thinks of `static` as a mechanism to run compile-time assertions, and I'm not sure why it makes sense for `const` at all.

So I wonder if it would make sense to have a variant of `static` or `const` that:

1. could never be used to define a new variable (so, basically restricted to passing/failing at compile time);
2. would be computed _after_ monomorphization.

Is there anything in the language that would block such a feature?

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [December 24, 2024, 2:46pm UTC](https://internals.rust-lang.org/t/static-assertions-momorphization/22070/2 "2024-12-24T14:46:01Z")

</div>

> I've been slowly working my way towards a pre-RFC for letting developers extend the type system through refinement types.

You may want to look into the pattern types initiative.

> [@Yoric](#):
>
> So I wonder if it would make sense to have a variant of `static` or `const` that:
> 
> 1. could never be used to define a new variable (so, basically restricted to passing/failing at compile time);
> 2. would be computed _after_ monomorphization.

I'm pretty sure that const blocks should work for this

```rust
const {
    assert!(...);
}

```

---

<div class="post-metadata">

**Author:** ![Yoric](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yoric/32/1316_2.png) [@Yoric](https://internals.rust-lang.org/u/Yoric)\
**Post date:** [December 24, 2024, 5:29pm UTC](https://internals.rust-lang.org/t/static-assertions-momorphization/22070/3 "2024-12-24T17:29:05Z")

</div>

They do, thank you very much!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [June 17, 2026, 5:30pm UTC](https://internals.rust-lang.org/t/static-assertions-momorphization/22070/4 "2026-06-17T17:30:02Z")

</div>

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
