-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Lint against inherent methods on types implementing Receiver and Deref #151583
Copy link
Copy link
Open
Labels
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.I-lang-nominatedNominated for discussion during a lang team meeting.Nominated for discussion during a lang team meeting.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.Items that are on lang's radar and will need eventual work or consideration.P-lang-drag-3Lang team prioritization drag level 3.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.Lang team prioritization drag level 3.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.T-langRelevant to the language teamRelevant to the language team
Metadata
Metadata
Assignees
Labels
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.I-lang-nominatedNominated for discussion during a lang team meeting.Nominated for discussion during a lang team meeting.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.Items that are on lang's radar and will need eventual work or consideration.P-lang-drag-3Lang team prioritization drag level 3.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.Lang team prioritization drag level 3.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.T-langRelevant to the language teamRelevant to the language team
Type
Fields
Give feedbackNo fields configured for issues without a type.
When inherent methods are added to types that implement
ReceiverorDereftoday, they take precedence over methods with the same name on the type they receive for or deref to. This means that it becomes impossible to call those with the method calling syntax, resulting in poorer ergonomics for the type that implementsReceiverorDeref. For example:Since
Receiverexists purely for these ergonomic reasons -- allowing methods to be called using the method syntax with custom receiver types -- this goes directly against the spirit of that trait. In the standard library it is already standard practice to not add inherent methods to types likeBoxandArcand instead make them associated functions.For this reason, there should be a lint that triggers on inherent methods on types that implement
ReceiverorDeref.In the example above, the lint could look like this:
The lint should only fire on methods that are
pub(or the maximal publicity of the type, so apub(crate)type would also trigger onpub(crate)functions).Another important consideration is that
Derefis often used for the subtyping pattern or for dereferencing to a "morally contained type". In this case, the target type is not a generic, but a concrete type or a rigid type containing a generic (for example[T]). For this reason the lint forDerefshould not trigger in those cases. The rationale is that since one knows the concrete type, avoiding name collisions is much easier. It also avoids false positives, since in the subtyping pattern, both types want to have inherent methods. In particular this will ensure that theDerefimplementation forVecwill not trigger the lint.Zulip discussion