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

don't crash on an extension with an empty declaration #635

Merged
merged 1 commit into from
Jun 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ extension ExtendedTypeFormatTransformation {
newMixins[SymbolGraph.Symbol.Swift.Extension.mixinKey] = swiftExtension
}

if let declarationFragments = extensionBlockSymbol[mixin: SymbolGraph.Symbol.DeclarationFragments.self]?.declarationFragments {
if let declarationFragments = extensionBlockSymbol[mixin: SymbolGraph.Symbol.DeclarationFragments.self]?.declarationFragments, declarationFragments.count >= 3 {
var prefixWithoutWhereClause: [SymbolGraph.Symbol.DeclarationFragments.Fragment] = Array(declarationFragments[..<3])

outer: for fragment in declarationFragments[3...] {
Expand Down
14 changes: 13 additions & 1 deletion Tests/SwiftDocCTests/Infrastructure/ReferenceResolverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,19 @@ class ReferenceResolverTests: XCTestCase {
let renderReference = try XCTUnwrap(renderNode.references[boolReference])
XCTAssert(renderReference is UnresolvedRenderReference)
}


func testExtensionWithEmptyDeclarationFragments() throws {
let (bundle, context) = try testBundleAndContext(named: "ModuleWithEmptyDeclarationFragments")

let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/ModuleWithEmptyDeclarationFragments", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode

// Despite having an extension to Float, there are no symbols added by that extension, so
// the resulting documentation should be empty
XCTAssertEqual(renderNode.topicSections.count, 0)
}

struct TestExternalReferenceResolver: ExternalReferenceResolver {
var bundleIdentifier = "com.external.testbundle"
var expectedReferencePath = "/externally/resolved/path"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleVersion</key>
<string>0.1.0</string>
<key>CFBundleIdentifier</key>
<string>org.swift.docc.example</string>
<key>CFBundleDisplayName</key>
<string>Module with empty declaration fragments</string>
<key>CFBundleName</key>
<string>ModuleWithEmptyDeclarationFragments</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ``ModuleWithEmptyDeclarationFragments``

This module contains a single extension to `Float`, which was generated with an empty declaration.

## Overview

The purpose of this test fixture is to ensure that Swift-DocC does not crash when encountering these
erroneous symbol graphs on extension block symbols.

<!-- Copyright (c) 2023 Apple Inc and the Swift Project authors. All Rights Reserved. -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"metadata": {
"formatVersion": {
"major": 0,
"minor": 6,
"patch": 0
},
"generator": "Swift 5.9"
},
"module": {
"name": "ModuleWithEmptyDeclarationFragments",
"platform": {
"architecture": "x86_64",
"vendor": "apple",
"operatingSystem": {
"name": "macosx",
"minimumVersion": {
"major": 12,
"minor": 4
}
}
}
},
"symbols": [],
"relationships": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"metadata": {
"formatVersion": {
"major": 0,
"minor": 6,
"patch": 0
},
"generator": "Swift 5.9"
},
"module": {
"name": "ModuleWithEmptyDeclarationFragments",
"platform": {
"architecture": "x86_64",
"vendor": "apple",
"operatingSystem": {
"name": "macosx",
"minimumVersion": {
"major": 12,
"minor": 4
}
}
}
},
"symbols": [
{
"kind": {
"identifier": "swift.extension",
"displayName": "Extension"
},
"identifier": {
"precise": "s:e:s:Sf10FoundationE4_argSfvp",
"interfaceLanguage": "swift"
},
"pathComponents": [
"Float"
],
"names": {
"title": "Float",
"navigator": [
{
"kind": "identifier",
"spelling": "Float"
}
],
"subHeading": []
},
"swiftExtension": {
"extendedModule": "Swift",
"typeKind": "swift.struct"
},
"declarationFragments": [],
"accessLevel": "public",
"availability": [
{
"domain": "macOS",
"introduced": {
"major": 12
}
},
{
"domain": "watchOS",
"introduced": {
"major": 8
}
},
{
"domain": "iOS",
"introduced": {
"major": 15
}
},
{
"domain": "tvOS",
"introduced": {
"major": 15
}
}
]
}
],
"relationships": [
{
"kind": "extensionTo",
"source": "s:e:s:Sf10FoundationE4_argSfvp",
"target": "s:Sf",
"targetFallback": "Swift.Float"
},
{
"kind": "conformsTo",
"source": "s:e:s:Sf10FoundationE4_argSfvp",
"target": "s:SQ",
"targetFallback": "Swift.Equatable"
}
]
}