-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Trait coherence: disallow inherent impls on external types #7385
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
base: master
Are you sure you want to change the base?
Conversation
0d7b96a
to
4c72c34
Compare
CodSpeed Performance ReportMerging #7385 will not alter performanceComparing Summary
|
This improves the trait coherence checks for impl self case, and polishes a bit of the wording on the existing diagnostics. * Add CompileError::InherentImplForExternalType and diagnostics. Reason/issue/help now refer to "package" to match coherence scope * Enforce package-level check in impl-self type checking Reject inherent impls for external nominal types (struct/enum) Temporary whitelist: allow inherent impls on std::storage::StorageKey<_> This is a workaround so current code that uses this pattern keeps working.
4c72c34
to
70828a8
Compare
// Temporary workaround: allow inherent impls on `std::storage::StorageKey<_>`. | ||
let is_storage_key_in_std = match &*type_engine.get(implementing_for.type_id()) { | ||
TypeInfo::Struct(decl_id) => { | ||
let s = decl_engine.get_struct(decl_id); | ||
s.call_path.suffix.as_str() == "StorageKey" | ||
&& s.call_path.prefixes.len() >= 2 | ||
&& s.call_path.prefixes[0].as_str() == "std" | ||
&& s.call_path.prefixes[1].as_str() == "storage" | ||
} | ||
_ => false, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that, as we discussed, this whitelisting wasn't actually necessary for licit usages of impl StorageKey<SomeType>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I will rework this PR
This improves the trait coherence checks for impl self case, and polishes a bit of the wording on the existing diagnostics.
We now enforce package-level check in impl-self type checking, which means we now reject inherent impls for external nominal types (struct/enum).
There is still is a specialized whitelist for
StorageKey
so current code that uses this pattern keeps working.Checklist
Breaking*
orNew Feature
labels where relevant.