Skip to content

Implement AsMut<T> and AsRef<T> for !. - #161253

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
kpreid:neverref
Oct 21, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
kpreid:neverref

Conversation

@kpreid

@kpreid kpreid commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

This will allow e.g. &[!] to satisfy &[T] where T: AsRef<str>. It follows the recommendation from the never documentation:

When writing your own traits, ! should have an impl whenever there is an obvious impl which doesn’t panic!.

— https://doc.rust-lang.org/1.97.1/std/primitive.never.html#-and-traits

A class of use case for these impls is:

  • some code is generic over T: AsRef<SomeOtherType>,
  • and is still useful in the case where there are no Ts, because it takes Option<T> or &[T] or any other input that can be empty or never-called, yet still do things with other inputs,

in which case providing T = ! is slightly better than the alternative of providing an inhabited placeholder type, because it ensures that the generic code is compiled with knowledge of the fact that that there will never be any Ts, even if the specific code that cares about whether there are any Ts doesn’t get inlined into the call site that doesn’t provide any Ts.


The test tests/ui/impl-trait/generic-with-implicit-hrtb-without-dyn.rs had to be updated because it depended on this impl not existing. I’ve confirmed that the modified test still functions as a regression test by compiling it in nightly-2022-08-28 and seeing it ICE.

Tracking issue for never: #35121
(This change has no ACP or tracking issue of its own; I assume it is simple enough that T-libs-api can just accept or reject this PR, and the implementation will be stable when ! is stable.)

@rustbot label -T-libs +T-libs-api +F-never_type

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-never-type Area: `!` type. For issues not specific to `!`, use A-uninhabited-types. beta-accepted Accepted for backporting to the compiler in the beta channel. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. 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. to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.