Skip to content
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

Idea: Check variance by recursively expanding types and looking for invariant / contravariant types #480

Open
obi1kenobi opened this issue Jun 29, 2023 · 2 comments
Labels
A-lint Area: new or existing lint C-enhancement Category: raise the bar on expectations

Comments

@obi1kenobi
Copy link
Owner

Relevant reading: https://doc.rust-lang.org/nomicon/subtyping.html
Especially this section: https://doc.rust-lang.org/nomicon/subtyping.html#variance

TL;DR: For many types, a held T<'a> can be converted into T<'b> if 'a: 'b: we're agreeing to use the type "for shorter." If at a later time, the implementation of such a T gains (for example) an UnsafeCell field, it will lose the aforementioned property. We say that instead of covariant, it will have become invariant.

This is an extremely subtle way to break semver, and one for which downstream users have no recourse.

To craft an effective lint, we want to explain not just that the variance change happened but also which type(s) caused it. Ideally, we'd show the entire chain of types all the way down to the UnsafeCell or other field that causes invariance.

To be effective, this requires cross-crate analysis, which requires us to be able to effectively link and query rustdocs of multiple crates and the stdlib together.

@obi1kenobi obi1kenobi added A-lint Area: new or existing lint C-enhancement Category: raise the bar on expectations labels Jun 29, 2023
@Nemo157
Copy link
Contributor

Nemo157 commented Jan 8, 2024

Potentially this information could be provided by rustdoc for each generic type, we have rust-lang/rfcs#3515 about adding it to the UI.

@obi1kenobi
Copy link
Owner Author

Nice, TIL! That would certainly make this much easier to implement. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: new or existing lint C-enhancement Category: raise the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants