Skip to content

[SymbolGraphGen] only recurse public-private type aliases from Clang #79996

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 1 commit into from
Mar 31, 2025
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
17 changes: 15 additions & 2 deletions lib/SymbolGraphGen/SymbolGraphASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,28 @@ bool SymbolGraphASTWalker::walkToDeclPre(Decl *D, CharSourceRange Range) {
}
}

// If this is a Clang typedef of an underlying type that is being hidden (e.g. `typedef struct
// _MyStruct { ... } MyStruct`) then copy in the child symbols from the underlying type to the
// type alias.
if (const auto *TD = dyn_cast_or_null<TypeAliasDecl>(VD)) {
const auto InnerType = TD->getUnderlyingType();
if (NominalTypeDecl *NTD = InnerType->getAnyNominal()) {
// Only fold typedefs together if the inner type is from our module and it
// otherwise isn't being shown
if (isOurModule(NTD->getModuleContext()) &&
!SG->canIncludeDeclAsNode(NTD)) {
PublicPrivateTypeAliases.insert_or_assign(NTD, TD);
synthesizeChildSymbols(NTD, TD);
// We specifically only want to look for underlying types that are "embedded" in the typedef
// definition, so let's pull out the Clang decl and check for that
if (NTD->hasClangNode()) {
if (const auto *ClangDecl = NTD->getClangNode().getAsDecl()) {
if (const auto *ClangTagDecl = dyn_cast<clang::TagDecl>(ClangDecl)) {
if (ClangTagDecl->isEmbeddedInDeclarator()) {
PublicPrivateTypeAliases.insert_or_assign(NTD, TD);
synthesizeChildSymbols(NTD, TD);
}
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,7 @@
// _InnerType's type name should only appear in quotes like this once, in the declaration for OuterType
// INNER-COUNT-1: "_InnerType"

// _InnerType.someFunc() as synthesized on OuterType
// CHECK-DAG: "precise": "s:15HiddenTypeAlias06_InnerB0C8someFuncyyF::SYNTHESIZED::s:15HiddenTypeAlias05OuterB0a"

// someFunc is a member of OuterType
// CHECK-DAG: "kind": "memberOf",{{[[:space:]]*}}"source": "s:15HiddenTypeAlias06_InnerB0C8someFuncyyF::SYNTHESIZED::s:15HiddenTypeAlias05OuterB0a",{{[[:space:]]*}}"target": "s:15HiddenTypeAlias05OuterB0a"

// OuterType conforms to SomeProtocol
// CHECK-DAG: "kind": "conformsTo",{{[[:space:]]*}}"source": "s:15HiddenTypeAlias05OuterB0a",{{[[:space:]]*}}"target": "s:15HiddenTypeAlias12SomeProtocolP"

// OuterType "inherits from" BaseType
// CHECK-DAG: "kind": "inheritsFrom",{{[[:space:]]*}}"source": "s:15HiddenTypeAlias05OuterB0a",{{[[:space:]]*}}"target": "s:15HiddenTypeAlias04BaseB0C"

// bonusFunc as a synthesized member of OuterType
// CHECK-DAG: "precise": "s:15HiddenTypeAlias12SomeProtocolPAAE9bonusFuncyyF::SYNTHESIZED::s:15HiddenTypeAlias05OuterB0a",
// CHECK-DAG: "kind": "memberOf",{{[[:space:]]*}}"source": "s:15HiddenTypeAlias12SomeProtocolPAAE9bonusFuncyyF::SYNTHESIZED::s:15HiddenTypeAlias05OuterB0a",{{[[:space:]]*}}"target": "s:15HiddenTypeAlias05OuterB0a",
// CHECK-NOT: someFunc

public protocol SomeProtocol {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name HiddenTypeAliasRecursive -emit-module -emit-module-path %t/
// RUN: %target-swift-symbolgraph-extract -module-name HiddenTypeAliasRecursive -I %t -pretty-print -output-dir %t -v
// RUN: %FileCheck %s --input-file %t/HiddenTypeAliasRecursive.symbols.json

// Ensure that mutually-recursive public-private type aliases don't cause infinite recursion in
// SymbolGraphGen when generating symbol graphs. (rdar://145980187)

// HiddenClassB is not referenced by any public symbol, so it shouldn't appear in any symbol graph
// CHECK-NOT: HiddenClassB

// HiddenClassA should only appear one time: in the declaration for PublicAlias
// CHECK-COUNT-1: HiddenClassA
// CHECK-NOT: HiddenClassA

@_documentation(visibility: private)
public class HiddenClassA {
public typealias ProblematicA = HiddenClassB
public func funcA() {}
}

@_documentation(visibility: private)
public class HiddenClassB {
public typealias ProblematicB = HiddenClassA
public func funcB() {}
}

public typealias PublicAlias = HiddenClassA