Sema: Improve the 'protocol method can't impose new requirements on Self' check #82690
+174
−31
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.
This check had two problems. First, it would assert upon encountering a layout requirement, due to an unimplemented code path.
A more fundamental issue is that the logic wasn't fully sound, because it would miss certain cases, for example:
Here, the reduced type of
Self.AisB, and at first glance, the requirementB: Equatableappears to be fine. However, this is actually a new requirement onSelf, and the protocol be rejected.Now that we can change the reduction order by assigning weights to generic parameters (#81171), this check can be implemented in a better way, by building a new generic signature first, where all generic parameters introduced by the protocol method, like 'B' above, are assigned a non-zero weight.
With this reduction order, any type that is equivalent to a member type of
Selfwill have a reduced type rooted inSelf, at which point the previous syntactic check becomes sound.Since this may cause us to reject code we accepted previously, the type checker now performs the check twice: first on the original signature, which may miss certain cases, and then again on the new signature built with the weighted reduction order.
If the first check fails, we diagnose an error. If the second check fails, we only diagnose a warning.
However, I'd like to rip out the first check and turn the warning from the second check into an error soon. It really can cause compiler crashes and miscompiles to have a malformed protocol like this.
Fixes rdar://116938972.