Skip to content

[5.7][CodeCompletion] Avoid creating circles in the USRBasedType supertype hierarchy #58636

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

Merged

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented May 3, 2022

Cherry-pick #58522 to release/5.7


  • Explanation: You are able to create circles in the superclass + conformance supertype hierarchy using
protocol Proto : Class {}
class Class : Proto {}

USRBasedType, which we use to model types for cached code completion results from other modules isn’t set up for this, it assumes that the supertype hierarchy forms a DAG (see main PR for more detail) and crashes with a stack overflow if an imported module contains such a circular type structure. To fix, ignore any superclass requirements on protocols, which eliminates the possibility of this super type circle.

… hierarchy

You would think that superclass + conformances form a DAG. You are wrong!
We can achieve a circular supertype hierarcy with

```swift
protocol Proto : Class {}
class Class : Proto {}
```

USRBasedType is not set up for this. Serialization of code completion results from global modules can't handle cycles in the supertype hierarchy
because it writes the DAG leaf to root(s) and needs to know the type offsets. To get consistent results independent of where we start
constructing USRBasedTypes, ignore superclasses of protocols. If we kept track of already visited types, we would get different results depending on
whether we start constructing the USRBasedType hierarchy from Proto or Class.
Ignoring superclasses of protocols is safe to do because USRBasedType is an under-approximation anyway.

rdar://91765262
@ahoppen ahoppen added the r5.7 label May 3, 2022
@ahoppen ahoppen requested a review from airspeedswift May 3, 2022 07:51
@ahoppen ahoppen requested a review from a team as a code owner May 3, 2022 07:51
@ahoppen
Copy link
Member Author

ahoppen commented May 3, 2022

@swift-ci Please test

@ahoppen ahoppen merged commit 309543b into swiftlang:release/5.7 May 6, 2022
@ahoppen ahoppen deleted the pr-5.7/no-circular-supertypes branch May 6, 2022 17:31
@AnthonyLatsis AnthonyLatsis added 🍒 release cherry pick Flag: Release branch cherry picks swift 5.7 labels Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks swift 5.7
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants