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

@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. F-never_type `#![feature(never_type)]` T-libs-api [DEPRECATED; DO NOT USE] and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 17, 2026
@QuineDot

Copy link
Copy Markdown

As Infallible will become !, this conflicts with implementations that can exist today.

use std::convert::Infallible;
struct S;
impl AsRef<S> for Infallible {
    fn as_ref(&self) -> &S {
        match *self {}
    }
}

@kpreid

kpreid commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

@QuineDot Thanks for pointing that out. That means this PR must either be rejected or get a crater run. I think the possible benefit is significant, so I won’t just close it immediately.

…unrelatedly, I just noticed that rustbot hasn’t assigned a reviewer at all.

r? libs-api

@theemathas theemathas added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. labels Aug 24, 2026
@theemathas

Copy link
Copy Markdown
Contributor

Not sure how to run crater on this one. Do we wait until after the never type stabilization PR is merged?

@JonathanBrouwer

JonathanBrouwer commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

You could run a crater with a AsRef<Infallible> and AsMut<Infallible> implementation, that should be representative.
That said, the stabilization PR looks likely to merge in the next few days so you could also just have some patience

@clarfonthey clarfonthey added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-libs-api [DEPRECATED; DO NOT USE] labels Aug 24, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

@kpreid FYI, libs-api isn't a thing any more, so, please r? libs in the future

ditto for the T-libs-api tag

@rustbot rustbot assigned JohnTitor and unassigned BurntSushi Aug 24, 2026
@steffahn

Copy link
Copy Markdown
Member

You could run a crater with a AsRef<Infallible> and AsMut<Infallible> implementation, that should be representative.

That would be AsRef<T> for Infallible / AsMut<T> for Infallible (clarifying just to avoid confusion).

@kpreid

kpreid commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

I’ll rebase this PR after the stabilization PR merges.

@JonathanBrouwer

Copy link
Copy Markdown
Member

(oops, thanks)

@rustbot

This comment has been minimized.

@kpreid

kpreid commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased, and the implementations are now marked #[stable].

@theemathas

Copy link
Copy Markdown
Contributor

Preparing for crater run
@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 25, 2026
Implement `AsMut<T>` and `AsRef<T>` for `!`.
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 65c7d39 (65c7d39f9c2b9d5c316725cb48fa3d360c64c663)
Base parent: e776960 (e7769602aca3770e8d8ea55716becb22e839a579)

@rust-rfcbot rust-rfcbot added the to-announce Announce this issue on triage meeting label Oct 20, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@clarfonthey clarfonthey 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 Oct 21, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

Should be ready to merge after making the change Josh mentioned.

@rustbot

This comment has been minimized.

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 `T`s, 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 `T`s, even if the specific code that cares about whether
there are any `T`s doesn’t get inlined into the call site that doesn’t
provide any `T`s.

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.
@rustbot

rustbot commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@kpreid

kpreid commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 21, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

@bors r+ rollup

Thank you!

@rust-bors

rust-bors Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

📋 This PR cannot be approved because it currently has the following label: needs-crater.

@theemathas theemathas removed the needs-crater This change needs a crater run to check for possible breakage in the ecosystem. label Oct 21, 2026
@theemathas

Copy link
Copy Markdown
Contributor

@bors r=clarfonthey rollup

@rust-bors

rust-bors Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit d016eb1 has been approved by clarfonthey

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 5. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious failures

@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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 21, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 21, 2026
Implement `AsMut<T>` and `AsRef<T>` for `!`.

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 `T`s, 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 `T`s, even if the specific code that cares about whether there are any `T`s doesn’t get inlined into the call site that doesn’t provide any `T`s.

---

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: rust-lang#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
rust-bors Bot pushed a commit that referenced this pull request Oct 21, 2026
…uwer

Rollup of 13 pull requests

Successful merges:

 - #162460 (Feat - `#[inline(always)] + #[target_feature(enable = "....")]` #2)
 - #163485 (`rustc_builtin_macros` cleanup, part 8)
 - #164457 (Add `vec_visualizer` lldb test)
 - #161253 (Implement `AsMut<T>` and `AsRef<T>` for `!`.)
 - #162369 (Document the `recursion_limit` built-in attribute)
 - #162840 (tier 3 target policy: target CPU and features must be supported by upstream backend)
 - #164329 (use default_field_values in declare_lint)
 - #164330 (Skip redundant monomorphization checks)
 - #164527 (doc: added addtional docs for Write)
 - #164656 (stabilize ExitCodeExt for Windows)
 - #164660 (Touch up attribute docs)
 - #164681 (tidy: use repository-relative path for style checks)
 - #164688 (re-enable test_dir_remove_dir test on QNX)
@rust-bors
rust-bors Bot merged commit 59d9a8f into rust-lang:main Oct 21, 2026
14 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 21, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 21, 2026
Rollup merge of #161253 - kpreid:neverref, r=clarfonthey

Implement `AsMut<T>` and `AsRef<T>` for `!`.

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 `T`s, 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 `T`s, even if the specific code that cares about whether there are any `T`s doesn’t get inlined into the call site that doesn’t provide any `T`s.

---

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
@kpreid
kpreid deleted the neverref branch October 22, 2026 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beta-accepted Accepted for backporting to the compiler in the beta channel. beta-nominated Nominated 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. F-never_type `#![feature(never_type)]` 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.