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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions lib/IDE/CodeCompletionResultType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,38 @@ const USRBasedType *USRBasedType::fromType(Type Ty, USRBasedTypeArena &Arena) {
Conformance->getProtocol()->getDeclaredInterfaceType(), Arena));
}
}
Type Superclass = Ty->getSuperclass();

// You would think that superclass + conformances form a DAG. You are wrong!
// We can achieve a circular supertype hierarcy with
//
// 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.

/// If `Ty` is a class type and has a superclass, return that. In all other
/// cases, return null.
auto getSuperclass = [](Type Ty) -> Type {
if (isa_and_nonnull<ClassDecl>(Ty->getAnyNominal())) {
return Ty->getSuperclass();
} else {
return Type();
}
};

Type Superclass = getSuperclass(Ty);
while (Superclass) {
Supertypes.push_back(USRBasedType::fromType(Superclass, Arena));
Superclass = Superclass->getSuperclass();
Superclass = getSuperclass(Superclass);
}

assert(llvm::all_of(Supertypes, [&USR](const USRBasedType *Ty) {
Expand Down
23 changes: 23 additions & 0 deletions validation-test/IDE/crashers_2_fixed/rdar91765262.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/ImportPath)
// RUN: %{python} %utils/split_file.py -o %t %s

// RUN: %target-swift-frontend -disable-availability-checking -emit-module %t/Lib.swift -o %t/ImportPath/Lib.swiftmodule -emit-module-interface-path %t/ImportPath/Lib.swiftinterface

// BEGIN Lib.swift

// Proto and Class have a circular supertype relationship.

public protocol Proto : Class {}
public class Class : Proto {}

// BEGIN test.swift

import Lib

// RUN: %empty-directory(%t/completion-cache)
// RUN: %target-swift-ide-test -code-completion -source-filename %t/test.swift -code-completion-token COMPLETE -I %t/ImportPath

func test() -> Proto {
return #^COMPLETE^#
}