Skip to content

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

Open
kpreid wants to merge 1 commit into
rust-lang:mainfrom
kpreid:neverref
Open

Implement AsMut<T> and AsRef<T> for !.#161253
kpreid wants to merge 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

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

rustbot commented Aug 25, 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 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)

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>

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

kpreid commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Updated tests/ui/impl-trait/generic-with-implicit-hrtb-without-dyn.rs to not depend on this impl not existing. I’ve confirmed that the modified test still functions as a regression test by compiling it with cargo +nightly-2022-08-28 check and seeing an ICE.

@theemathas

Copy link
Copy Markdown
Contributor

@craterbot check

@craterbot

Copy link
Copy Markdown
Collaborator

👌 Experiment pr-161253 created and queued.
🤖 Automatically detected try build 65c7d39
⚠️ Try build based on commit e7ce9a3, but latest commit is e028a86. Did you forget to make a new try build?
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 25, 2026
@JohnTitor

Copy link
Copy Markdown
Member

r? libs

@rustbot rustbot assigned nia-e and unassigned JohnTitor Aug 30, 2026
@theemathas theemathas mentioned this pull request Sep 3, 2026
@theemathas

Copy link
Copy Markdown
Contributor

@craterbot cancel

See #162233

@craterbot

Copy link
Copy Markdown
Collaborator

🗑️ Experiment pr-161253 deleted!

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Sep 3, 2026
@theemathas

Copy link
Copy Markdown
Contributor

@craterbot

Copy link
Copy Markdown
Collaborator

👌 Experiment pr-161253 created and queued.
🤖 Automatically detected try build 65c7d39
⚠️ Try build based on commit e7ce9a3, but latest commit is e028a86. Did you forget to make a new try build?
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-never_type `#![feature(never_type)]` needs-crater This change needs a crater run to check for possible breakage in the ecosystem. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-crater Status: Waiting on a crater run to be completed. 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.