Skip to content
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

[SR-14510] Unexpected generic signature diagnostics on making non-trivial commutative diagram #56862

Closed
typesanitizer opened this issue Apr 20, 2021 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@typesanitizer
Copy link

Previous ID SR-14510
Radar rdar://problem/76883924
Original Reporter @typesanitizer
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 55b448ae297128651310173da3638aa0

Issue Description:

protocol Adjoint {
  associatedtype Dual: Adjoint where Self.Dual.Dual == Self
}

protocol Diffable {
  associatedtype Patch
}

protocol AdjointDiffable: Adjoint & Diffable
where Self.Patch: Adjoint, Self.Dual: AdjointDiffable,
      Self.Patch.Dual == Self.Dual.Patch {
}

This is the commutative diagram.

   T    <---->             T.Dual
   |                         |
   v                         v
T.Patch <----> (T.Patch.Dual == T.Dual.Patch)

Unfortunately, the compiler doesn't seem too pleased about it.

tmp.swift:10:17: warning: redundant conformance constraint 'Self.Patch' : 'Adjoint'
where Self.Patch: Adjoint, Self.Dual: AdjointDiffable,
                ^
tmp.swift:10:37: note: conformance constraint 'Self.Patch' : 'Adjoint' implied here
where Self.Patch: Adjoint, Self.Dual: AdjointDiffable,
                                    ^
tmp.swift:11:18: error: 'Dual' is not a member type of type 'Self.Patch'
      Self.Patch.Dual == Self.Dual.Patch {
      ~~~~~~~~~~ ^

Maybe it's removing the Self.Patch: Adjoint when it incorrectly diagnoses redundancy and that's why it's falling over when trying to access Self.Patch.Dual?

I don't see why this code should have any errors or warnings.

Tested with recent-ish main @ a93593a.

@typesanitizer
Copy link
Author

@swift-ci create

@typesanitizer
Copy link
Author

Splitting AdjointDiffable up into two protocols seems to work around the issue.

protocol Adjoint {
  associatedtype Dual: Adjoint where Self.Dual.Dual == Self
}

protocol Diffable {
  associatedtype Patch
}

protocol PreAdjointDiffable: Adjoint & Diffable
where Self.Patch: Adjoint, Self.Dual: PreAdjointDiffable {
}

protocol AdjointDiffable: PreAdjointDiffable
where Self.Dual: AdjointDiffable, Self.Patch.Dual == Self.Dual.Patch {
}

@slavapestov
Copy link
Member

Thanks for the test case! I've been working in this area lately. Unfortunately my fixes on the main branch didn't fix this particular issue but I'll investigate and see what's going wrong. Your intuition that the type checker is incorrectly concluding that a requirement is redundant, and then dropping it, is exactly what's going wrong here.

@slavapestov
Copy link
Member

Here is the fix: #37154

@slavapestov
Copy link
Member

#37154

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants