Skip to content

Conversation

tritao
Copy link
Contributor

@tritao tritao commented Sep 13, 2025

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

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@tritao tritao self-assigned this Sep 13, 2025
@tritao tritao added compiler General compiler. Should eventually become more specific as the issue is triaged compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels Sep 13, 2025
@tritao tritao force-pushed the improve-trait-coherence-checks branch from 0d7b96a to 4c72c34 Compare September 13, 2025 14:58
Copy link

codspeed-hq bot commented Sep 13, 2025

CodSpeed Performance Report

Merging #7385 will not alter performance

Comparing tritao:improve-trait-coherence-checks (e4a6040) with master (5e22d64)

Summary

✅ 25 untouched

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.
@tritao tritao force-pushed the improve-trait-coherence-checks branch from 4c72c34 to 70828a8 Compare September 16, 2025 09:18
@tritao tritao marked this pull request as ready for review September 16, 2025 10:01
@tritao tritao requested a review from a team as a code owner September 16, 2025 10:01
Comment on lines +551 to +561
// 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,
};
Copy link
Contributor

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>

Copy link
Contributor Author

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

@tritao tritao marked this pull request as draft September 22, 2025 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler General compiler. Should eventually become more specific as the issue is triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants