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.

Note

Enforces package-level coherence by rejecting inherent impl blocks for external nominal types (with a whitelist for std::storage::StorageKey<_>), adds a dedicated diagnostic, and updates tests and orphan rule wording from module to package.

  • Semantic analysis:
    • Disallow inherent impl for external nominal types in type_check_impl_self (sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs).
    • Temporary whitelist for std::storage::StorageKey<_>.
    • Emit new error CompileError::InherentImplForExternalType with spans.
  • Diagnostics (sway-error/src/error.rs):
    • Add InherentImplForExternalType variant and full diagnostic with hints/help.
    • Change orphan rule wording from "module" to "package" in messages and help.
  • Tests:
    • Update failing snapshots and checks to expect package-level wording and the new inherent-impl error.
    • Refactor should_pass/language/generic_transpose to use a new trait OptionTranspose instead of an inherent impl on Option<_>.
    • Adjust impl_self_overlap and orphan rule snapshots accordingly.

Written by Cursor Bugbot for commit 308d097. This will update automatically on new commits. Configure here.

@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
@codspeed-hq
Copy link

codspeed-hq bot commented Sep 13, 2025

CodSpeed Performance Report

Merging #7385 will improve performances by 23.51%

Comparing tritao:improve-trait-coherence-checks (308d097) with master (be2d6ae)

Summary

⚡ 1 improvement
✅ 24 untouched

Benchmarks breakdown

Benchmark BASE HEAD Change
format 474.5 ms 384.2 ms +23.51%

@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
@tritao tritao marked this pull request as draft September 22, 2025 12:38
@tritao tritao force-pushed the improve-trait-coherence-checks branch from e4a6040 to e1285ed Compare October 12, 2025 18:56
@tritao tritao marked this pull request as ready for review October 12, 2025 19:19
IGI-111
IGI-111 previously approved these changes Oct 20, 2025
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 bc27b99 to e22e7ec Compare October 20, 2025 15:22
@tritao tritao requested a review from IGI-111 October 20, 2025 15:51
@tritao
Copy link
Contributor Author

tritao commented Oct 20, 2025

Rebased due to conflicts.

@tritao tritao enabled auto-merge (squash) October 20, 2025 15:52
@tritao tritao requested a review from a team October 23, 2025 08:45
@tritao tritao requested review from a team and ironcev and removed request for IGI-111 October 24, 2025 07:46
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.

4 participants