Repository navigation
Implement AsMut<T> and AsRef<T> for !. - #161253
Conversation
|
As use std::convert::Infallible;
struct S;
impl AsRef<S> for Infallible {
fn as_ref(&self) -> &S {
match *self {}
}
} |
|
@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 |
|
Not sure how to run crater on this one. Do we wait until after the never type stabilization PR is merged? |
|
You could run a crater with a |
|
@kpreid FYI, libs-api isn't a thing any more, so, please r? libs in the future ditto for the T-libs-api tag |
That would be |
|
I’ll rebase this PR after the stabilization PR merges. |
|
(oops, thanks) |
This comment has been minimized.
This comment has been minimized.
|
Rebased, and the implementations are now marked |
|
Preparing for crater run |
This comment has been minimized.
This comment has been minimized.
Implement `AsMut<T>` and `AsRef<T>` for `!`.
This comment has been minimized.
This comment has been minimized.
|
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. |
|
Should be ready to merge after making the change Josh mentioned. |
This comment has been minimized.
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.
|
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. |
|
@rustbot ready |
|
@bors r+ rollup Thank you! |
|
📋 This PR cannot be approved because it currently has the following label: |
|
@bors r=clarfonthey rollup |
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
…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)
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
View all comments
This will allow e.g.
&[!]to satisfy&[T] where T: AsRef<str>. It follows the recommendation from the never documentation:— https://doc.rust-lang.org/1.97.1/std/primitive.never.html#-and-traits
A class of use case for these impls is:
T: AsRef<SomeOtherType>,Ts, because it takesOption<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 anyTs, even if the specific code that cares about whether there are anyTs doesn’t get inlined into the call site that doesn’t provide anyTs.The test
tests/ui/impl-trait/generic-with-implicit-hrtb-without-dyn.rshad 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