Skip to content

Reexplain NonNull's covariance details more concisely #141933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
workingjubilee opened this issue Jun 2, 2025 · 2 comments · May be fixed by #141946
Open

Reexplain NonNull's covariance details more concisely #141933

workingjubilee opened this issue Jun 2, 2025 · 2 comments · May be fixed by #141946
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-raw-pointers Area: raw pointers, MaybeUninit, NonNull T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@workingjubilee
Copy link
Member

Location

std::ptr::NonNull
https://doc.rust-lang.org/stable/std/ptr/struct.NonNull.html

Summary

This page spills a lot of digital ink on the matter of covariance versus invariance. The fact of the matter is it is usually correct to use, which is why it was chosen to be like that. There are abstractions that require invariance, but they must be designed carefully to begin with, and the current format of the description is very repetitive. In practice, we have not seen an explosion of bugs from people using NonNull incorrectly over the past 60 versions, and I do not believe it is primarily because of the documented cautions. I do, however, see people incorrectly using &mut T, &T when they want NonNull<T>.

A more precise summarization would be more useful and less intimidating.

@workingjubilee workingjubilee added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Jun 2, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 2, 2025
@workingjubilee workingjubilee added A-raw-pointers Area: raw pointers, MaybeUninit, NonNull T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 2, 2025
@workingjubilee
Copy link
Member Author

In particular, if we want to discuss "if your type cannot safely be covariant" at all, then we should provide a very simple example of that case.

@xizheyin
Copy link
Contributor

xizheyin commented Jun 3, 2025

I think there also needs to be a clearer explanation here (https://doc.rust-lang.org/reference/subtyping.html#r-subtyping.variance.invariant), such as some examples, because the concepts are very difficult for newbies to understand.

@xizheyin xizheyin linked a pull request Jun 3, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-raw-pointers Area: raw pointers, MaybeUninit, NonNull T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants