-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Improved trait coherence checking #6844
Draft
tritao
wants to merge
3
commits into
FuelLabs:master
Choose a base branch
from
tritao:trait-coherence
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0bed6b2
to
ec4e13e
Compare
ec4e13e
to
7ab7033
Compare
7ab7033
to
cbf79c8
Compare
CodSpeed Performance ReportMerging #6844 will degrade performances by 50.73%Comparing Summary
Benchmarks breakdown
|
cbf79c8
to
242bddb
Compare
68ffce6
to
9f1adb3
Compare
9f1adb3
to
e4ea1d3
Compare
e4ea1d3
to
1ae9493
Compare
1ae9493
to
142e685
Compare
This commit refines trait coherence checking to guarantee that there is only one valid implementation for each trait. It introduces improved orphan rules and more rigorous overlap checks to prevent conflicting trait implementations. With these changes, the system now more reliably enforces coherence, ensuring that traits are implemented uniquely, and reducing potential ambiguities. This approach mirrors Rust’s design by enforcing two distinct safety and coherence checks on trait implementations: 1. Orphan Rules Check The orphan rules require that for any trait implementation, either the trait or the type must be defined within the current package. This restriction prevents external packages from implementing foreign traits for foreign types, which could otherwise lead to conflicting implementations and ambiguities across different parts of a codebase. Essentially, it helps maintain clear ownership and boundaries of trait implementations. 2. Overlap Impl Check The overlap impl check ensures that no two trait implementations can apply to the same type in an ambiguous manner. If two implementations could potentially match the same type, it would be unclear which one should be used, leading to coherence issues. By enforcing that implementations do not overlap, the system guarantees that trait resolution is unambiguous and predictable. Together, these checks promote a robust and maintainable system by ensuring that trait implementations are both locally controlled (orphan rules) and non-conflicting (overlap check). The test suite has been updated to comply with the new rules. However, there is one current limitation regarding arrays. For arrays, the coherence checks have been relaxed to avoid the need for numerous concrete implementations in the standard library for traits like `Eq` and `PartialEq`. This decision was made because support for const generics is expected soon, which will allow these traits to be implemented more cleanly. Closes issue FuelLabs#5892.
142e685
to
af25915
Compare
2f559fa
to
377f415
Compare
377f415
to
f770057
Compare
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR refines trait coherence checking to guarantee that there is only one valid implementation for each trait. It introduces improved orphan rules and more rigorous overlap checks to prevent conflicting trait implementations. With these changes, the system now more reliably enforces coherence, ensuring that traits are implemented uniquely, and
reducing potential ambiguities.
This approach mirrors Rust’s design by enforcing two distinct safety and coherence checks on trait implementations:
1. Orphan Rules Check
The orphan rules require that for any trait implementation, either the trait or the type must be defined within the current package. This restriction prevents external packages from implementing foreign traits for foreign types, which could otherwise lead to conflicting implementations and ambiguities across different parts of a codebase. Essentially, it helps maintain clear ownership and boundaries of trait implementations.
2. Overlap Impl Check
The overlap impl check ensures that no two trait implementations can apply to the same type in an ambiguous manner. If two implementations could potentially match the same type, it would be unclear which one should be used, leading to coherence issues. By enforcing that implementations do not overlap, the system guarantees that trait resolution is unambiguous and predictable.
Together, these checks promote a robust and maintainable system by ensuring that trait implementations are both locally controlled (orphan rules) and non-conflicting (overlap check).
The test suite has been updated to comply with the new rules. However, there is one current limitation regarding arrays. For arrays, the coherence checks have been relaxed to avoid the need for numerous concrete implementations in the standard library for traits like
Eq
andPartialEq
. This decision was made because support for const generics is expected soon, which will allow these traits to be implemented more cleanly.Closes issue #5892.
Checklist
Breaking*
orNew Feature
labels where relevant.