Skip to content

Fix mem::conjure_zst panic message to use any::type_name instead - #153073

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
mehdiakiki:fix/conjure-zst-panic-message
Feb 28, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
mehdiakiki:fix/conjure-zst-panic-message

Conversation

@mehdiakiki

@mehdiakiki mehdiakiki commented Feb 25, 2026 •

Copy link
Copy Markdown
Contributor

Use crate::any::type_name::<T>() instead of stringify!(T) in the
runtime panic message of conjure_zst , so the actual type name (e.g.
i32) is shown rather than the literal string [T].

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 25, 2026
@rustbot

rustbot commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, jhpratt, joboet, scottmcm

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

⚠️ #[rustc_allow_const_fn_unstable] needs careful audit to avoid accidentally exposing unstable
implementation details on stable.

cc @rust-lang/wg-const-eval

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/mem/mod.rs Resolved
Comment thread library/core/src/mem/mod.rs Outdated
intrinsics::const_eval_select!(
@capture[T] { _marker: crate::marker::PhantomData<T> = crate::marker::PhantomData::<T> } -> ():
if const {
if size_of::<T>() != 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like you could use if size_of::<T>() != 0 first instead of having these repeated in const/non-const branches.

@joboet joboet left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you call type_name before the const_assert!, you can largely keep the previous version:

let name = type_name::<T>();
const_assert!(..., name: &str = name);

View changes since this review

Comment thread library/core/src/mem/mod.rs Resolved
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 25, 2026
@rustbot

rustbot commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@RalfJung

Copy link
Copy Markdown
Member

If you call type_name before the const_assert!, you can largely keep the previous version:

This has the unfortunate effect of calling type_name even in const mode and then just ignoring the result... but I guess it is the easiest thing to do here.

Comment thread library/core/src/mem/mod.rs Resolved
@joboet

joboet commented Feb 25, 2026

Copy link
Copy Markdown
Member

If you call type_name before the const_assert!, you can largely keep the previous version:

This has the unfortunate effect of calling type_name even in const mode and then just ignoring the result... but I guess it is the easiest thing to do here.

Since ZST-ness is known at compile time, the optimiser will in all likelihood remove all this for ZSTs anyway.

@RalfJung

Copy link
Copy Markdown
Member

Const-code doesn't get optimized, so there we will evaluate the intrinsic. But that's just a bit of unnecessary work in compilations that will fail due to a const-panic; doesn't seem worth much effort to fix that.

@mehdiakiki

Copy link
Copy Markdown
Contributor Author

I will have a look at it later today!

@rust-log-analyzer

This comment has been minimized.

@asder8215

Copy link
Copy Markdown
Contributor

Ralf/Joboet will say this as well, but make sure to squash all your commits into one commit!

Comment thread library/core/src/mem/mod.rs Outdated
Comment on lines +1493 to +1498
let name = crate::any::type_name::<T>();
const_assert!(
size_of::<T>() == 0,
"mem::conjure_zst invoked on a nonzero-sized type",
"mem::conjure_zst invoked on type {t}, which is not zero-sized",
t: &str = stringify!(T)
"mem::conjure_zst invoked on a non-zero-sized type",
"mem::conjure_zst invoked on type {name}, which is not zero-sized",
name: &str = name

@RalfJung RalfJung Feb 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
let name = crate::any::type_name::<T>();
const_assert!(
size_of::<T>() == 0,
"mem::conjure_zst invoked on a nonzero-sized type",
"mem::conjure_zst invoked on type {t}, which is not zero-sized",
t: &str = stringify!(T)
"mem::conjure_zst invoked on a non-zero-sized type",
"mem::conjure_zst invoked on type {name}, which is not zero-sized",
name: &str = name
const_assert!(
size_of::<T>() == 0,
"mem::conjure_zst invoked on a non-zero-sized type",
"mem::conjure_zst invoked on type {name}, which is not zero-sized",
name: &str = crate::any::type_name::<T>(),

no need to call the intrinsic before the macro

(also GH seems to really destroy indentation with these suggestions... thanks GH)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok!

@mehdiakiki
mehdiakiki force-pushed the fix/conjure-zst-panic-message branch from f2a2a8b to 9092262 Compare February 27, 2026 01:38
@RalfJung

Copy link
Copy Markdown
Member

Looks good, thanks!
@bors r+ rollup

@rust-bors

rust-bors Bot commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 9092262 has been approved by RalfJung

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 27, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 27, 2026
…essage, r=RalfJung

Fix mem::conjure_zst panic message to use any::type_name instead

Use `crate::any::type_name::<T>()` instead of `stringify!(T)` in the
runtime panic message of `conjure_zst` , so the actual type name (e.g.
`i32`) is shown rather than the literal string `[T]`.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 27, 2026
…essage, r=RalfJung

Fix mem::conjure_zst panic message to use any::type_name instead

Use `crate::any::type_name::<T>()` instead of `stringify!(T)` in the
runtime panic message of `conjure_zst` , so the actual type name (e.g.
`i32`) is shown rather than the literal string `[T]`.
rust-bors Bot pushed a commit that referenced this pull request Feb 27, 2026
…uwer

Rollup of 11 pull requests

Successful merges:

 - #151431 (Add new unstable attribute: `#[export_visibility = ...]`.)
 - #153012 (Stop using `LinkedGraph` in `lexical_region_resolve`)
 - #153179 (Force a CI LLVM stamp bump)
 - #150828 (Improved security section in rustdoc for `current_exe`)
 - #152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate)
 - #152674 (rustc_public: remove the `CrateDefItems` trait)
 - #153073 (Fix mem::conjure_zst panic message to use any::type_name instead)
 - #153117 (Remove mutation from macro path URL construction)
 - #153128 (Recover feature lang_items for emscripten)
 - #153138 (Print path root when printing path)
 - #153159 (Work around a false `err.emit();` type error in rust-analyzer)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 27, 2026
…essage, r=RalfJung

Fix mem::conjure_zst panic message to use any::type_name instead

Use `crate::any::type_name::<T>()` instead of `stringify!(T)` in the
runtime panic message of `conjure_zst` , so the actual type name (e.g.
`i32`) is shown rather than the literal string `[T]`.
rust-bors Bot pushed a commit that referenced this pull request Feb 27, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #151143 (explicit tail calls: support indirect arguments)
 - #153012 (Stop using `LinkedGraph` in `lexical_region_resolve`)
 - #153175 (Clarify a confusing green-path function)
 - #153179 (Force a CI LLVM stamp bump)
 - #150828 (Improved security section in rustdoc for `current_exe`)
 - #152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate)
 - #152674 (rustc_public: remove the `CrateDefItems` trait)
 - #153073 (Fix mem::conjure_zst panic message to use any::type_name instead)
 - #153117 (Remove mutation from macro path URL construction)
 - #153128 (Recover feature lang_items for emscripten)
 - #153138 (Print path root when printing path)
 - #153159 (Work around a false `err.emit();` type error in rust-analyzer)
rust-bors Bot pushed a commit that referenced this pull request Feb 27, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #151143 (explicit tail calls: support indirect arguments)
 - #153012 (Stop using `LinkedGraph` in `lexical_region_resolve`)
 - #153175 (Clarify a confusing green-path function)
 - #153179 (Force a CI LLVM stamp bump)
 - #150828 (Improved security section in rustdoc for `current_exe`)
 - #152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate)
 - #152674 (rustc_public: remove the `CrateDefItems` trait)
 - #153073 (Fix mem::conjure_zst panic message to use any::type_name instead)
 - #153117 (Remove mutation from macro path URL construction)
 - #153128 (Recover feature lang_items for emscripten)
 - #153138 (Print path root when printing path)
 - #153159 (Work around a false `err.emit();` type error in rust-analyzer)
@rust-bors
rust-bors Bot merged commit f5383a3 into rust-lang:main Feb 28, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 28, 2026
rust-timer added a commit that referenced this pull request Feb 28, 2026
Rollup merge of #153073 - mehdiakiki:fix/conjure-zst-panic-message, r=RalfJung

Fix mem::conjure_zst panic message to use any::type_name instead

Use `crate::any::type_name::<T>()` instead of `stringify!(T)` in the
runtime panic message of `conjure_zst` , so the actual type name (e.g.
`i32`) is shown rather than the literal string `[T]`.
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 19, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#151143 (explicit tail calls: support indirect arguments)
 - rust-lang/rust#153012 (Stop using `LinkedGraph` in `lexical_region_resolve`)
 - rust-lang/rust#153175 (Clarify a confusing green-path function)
 - rust-lang/rust#153179 (Force a CI LLVM stamp bump)
 - rust-lang/rust#150828 (Improved security section in rustdoc for `current_exe`)
 - rust-lang/rust#152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate)
 - rust-lang/rust#152674 (rustc_public: remove the `CrateDefItems` trait)
 - rust-lang/rust#153073 (Fix mem::conjure_zst panic message to use any::type_name instead)
 - rust-lang/rust#153117 (Remove mutation from macro path URL construction)
 - rust-lang/rust#153128 (Recover feature lang_items for emscripten)
 - rust-lang/rust#153138 (Print path root when printing path)
 - rust-lang/rust#153159 (Work around a false `err.emit();` type error in rust-analyzer)
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 19, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#151143 (explicit tail calls: support indirect arguments)
 - rust-lang/rust#153012 (Stop using `LinkedGraph` in `lexical_region_resolve`)
 - rust-lang/rust#153175 (Clarify a confusing green-path function)
 - rust-lang/rust#153179 (Force a CI LLVM stamp bump)
 - rust-lang/rust#150828 (Improved security section in rustdoc for `current_exe`)
 - rust-lang/rust#152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate)
 - rust-lang/rust#152674 (rustc_public: remove the `CrateDefItems` trait)
 - rust-lang/rust#153073 (Fix mem::conjure_zst panic message to use any::type_name instead)
 - rust-lang/rust#153117 (Remove mutation from macro path URL construction)
 - rust-lang/rust#153128 (Recover feature lang_items for emscripten)
 - rust-lang/rust#153138 (Print path root when printing path)
 - rust-lang/rust#153159 (Work around a false `err.emit();` type error in rust-analyzer)
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 27, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#151143 (explicit tail calls: support indirect arguments)
 - rust-lang/rust#153012 (Stop using `LinkedGraph` in `lexical_region_resolve`)
 - rust-lang/rust#153175 (Clarify a confusing green-path function)
 - rust-lang/rust#153179 (Force a CI LLVM stamp bump)
 - rust-lang/rust#150828 (Improved security section in rustdoc for `current_exe`)
 - rust-lang/rust#152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate)
 - rust-lang/rust#152674 (rustc_public: remove the `CrateDefItems` trait)
 - rust-lang/rust#153073 (Fix mem::conjure_zst panic message to use any::type_name instead)
 - rust-lang/rust#153117 (Remove mutation from macro path URL construction)
 - rust-lang/rust#153128 (Recover feature lang_items for emscripten)
 - rust-lang/rust#153138 (Print path root when printing path)
 - rust-lang/rust#153159 (Work around a false `err.emit();` type error in rust-analyzer)
jhpratt added a commit to jhpratt/rust that referenced this pull request Sep 26, 2026
…rfonthey

Stabilize `mem::conjure_zst`

Tracking issue: rust-lang#95383

---

Stabilized API:

```rust
pub const unsafe fn conjure_zst<T>() -> T;
```

The notable thing about this implementation is that if this function is called at run time (as opposed to in consteval), then the function will panic at run time (as opposed to at compile time) if `T` is not a ZST.

If the function panics in consteval, the panic message is a fixed literal string. If the function panics at run time, the panic message includes the `type_name` of `T`. Note that [`type_name`](https://doc.rust-lang.org/nightly/std/any/fn.type_name.html) is currently [unstable in const](rust-lang#63084).

---

Implementation history:

* rust-lang#95385 (attempted to add the API without an ACP, and then the PR was closed)
* rust-lang/libs-team#292 (The ACP. It has a lot of additional context and discussion, including the explanation of why this is needed)
* rust-lang#146479 (the actual implementation)
* rust-lang#153073 (changes the panic message)
* rust-lang#155840 (internal implementation change)
rust-bors Bot pushed a commit that referenced this pull request Sep 26, 2026
Rollup merge of #161710 - theemathas:stab-conjure-zst, r=clarfonthey

Stabilize `mem::conjure_zst`

Tracking issue: #95383

---

Stabilized API:

```rust
pub const unsafe fn conjure_zst<T>() -> T;
```

The notable thing about this implementation is that if this function is called at run time (as opposed to in consteval), then the function will panic at run time (as opposed to at compile time) if `T` is not a ZST.

If the function panics in consteval, the panic message is a fixed literal string. If the function panics at run time, the panic message includes the `type_name` of `T`. Note that [`type_name`](https://doc.rust-lang.org/nightly/std/any/fn.type_name.html) is currently [unstable in const](#63084).

---

Implementation history:

* #95385 (attempted to add the API without an ACP, and then the PR was closed)
* rust-lang/libs-team#292 (The ACP. It has a lot of additional context and discussion, including the explanation of why this is needed)
* #146479 (the actual implementation)
* #153073 (changes the panic message)
* #155840 (internal implementation change)
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Sep 27, 2026
Stabilize `mem::conjure_zst`

Tracking issue: rust-lang/rust#95383

---

Stabilized API:

```rust
pub const unsafe fn conjure_zst<T>() -> T;
```

The notable thing about this implementation is that if this function is called at run time (as opposed to in consteval), then the function will panic at run time (as opposed to at compile time) if `T` is not a ZST.

If the function panics in consteval, the panic message is a fixed literal string. If the function panics at run time, the panic message includes the `type_name` of `T`. Note that [`type_name`](https://doc.rust-lang.org/nightly/std/any/fn.type_name.html) is currently [unstable in const](rust-lang/rust#63084).

---

Implementation history:

* rust-lang/rust#95385 (attempted to add the API without an ACP, and then the PR was closed)
* rust-lang/libs-team#292 (The ACP. It has a lot of additional context and discussion, including the explanation of why this is needed)
* rust-lang/rust#146479 (the actual implementation)
* rust-lang/rust#153073 (changes the panic message)
* rust-lang/rust#155840 (internal implementation change)
makai410 pushed a commit to makai410/rust that referenced this pull request Oct 1, 2026
…nathanBrouwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang#151143 (explicit tail calls: support indirect arguments)
 - rust-lang#153012 (Stop using `LinkedGraph` in `lexical_region_resolve`)
 - rust-lang#153175 (Clarify a confusing green-path function)
 - rust-lang#153179 (Force a CI LLVM stamp bump)
 - rust-lang#150828 (Improved security section in rustdoc for `current_exe`)
 - rust-lang#152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate)
 - rust-lang#152674 (rustc_public: remove the `CrateDefItems` trait)
 - rust-lang#153073 (Fix mem::conjure_zst panic message to use any::type_name instead)
 - rust-lang#153117 (Remove mutation from macro path URL construction)
 - rust-lang#153128 (Recover feature lang_items for emscripten)
 - rust-lang#153138 (Print path root when printing path)
 - rust-lang#153159 (Work around a false `err.emit();` type error in rust-analyzer)
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Oct 1, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#151143 (explicit tail calls: support indirect arguments)
 - rust-lang/rust#153012 (Stop using `LinkedGraph` in `lexical_region_resolve`)
 - rust-lang/rust#153175 (Clarify a confusing green-path function)
 - rust-lang/rust#153179 (Force a CI LLVM stamp bump)
 - rust-lang/rust#150828 (Improved security section in rustdoc for `current_exe`)
 - rust-lang/rust#152673 (rustc_public: rewrite `bridge_impl` to reduce boilerplate)
 - rust-lang/rust#152674 (rustc_public: remove the `CrateDefItems` trait)
 - rust-lang/rust#153073 (Fix mem::conjure_zst panic message to use any::type_name instead)
 - rust-lang/rust#153117 (Remove mutation from macro path URL construction)
 - rust-lang/rust#153128 (Recover feature lang_items for emscripten)
 - rust-lang/rust#153138 (Print path root when printing path)
 - rust-lang/rust#153159 (Work around a false `err.emit();` type error in rust-analyzer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants