Skip to content

Commit

Permalink
Fix redundant public accessibility analysis false-positive for inheri…
Browse files Browse the repository at this point in the history
…ted and default associated types, closes #689
  • Loading branch information
ileitch committed Dec 19, 2023
1 parent 89bc021 commit 3d56f8d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Fix redundant public accessibility analysis false-positive for property wrappers.
- Fix redundant public accessibility analysis false-positive for declarations referenced from a public `@inlinable` function.
- Fix redundant public accessibility analysis false-positive for function parameter default values.
- Fix redundant public accessibility analysis false-positive for inherited and default associated types.

## 2.17.1 (2023-12-04)

Expand Down
5 changes: 3 additions & 2 deletions Sources/PeripheryKit/Indexer/Reference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ final class Reference {
case parameterType
case genericParameterType
case genericRequirementType
case inheritedClassType
case inheritedType
case refinedProtocolType
case initializerType
case variableInitFunctionCall
case functionCallMetatypeArgument
case unknown

var isPubliclyExposable: Bool {
switch self {
case .varType, .returnType, .parameterType, .genericParameterType, .genericRequirementType, .inheritedClassType, .refinedProtocolType, .variableInitFunctionCall, .functionCallMetatypeArgument:
case .varType, .returnType, .parameterType, .genericParameterType, .genericRequirementType, .inheritedType, .refinedProtocolType, .initializerType, .variableInitFunctionCall, .functionCallMetatypeArgument:
return true
default:
return false
Expand Down
7 changes: 5 additions & 2 deletions Sources/PeripheryKit/Indexer/SwiftIndexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,9 @@ public final class SwiftIndexer: Indexer {

for ref in decl.references.union(decl.related) {
if result.inheritedTypeLocations.contains(ref.location) {
if decl.kind == .class, ref.kind == .class {
ref.role = .inheritedClassType
if (decl.kind == .class && ref.kind == .class) ||
(decl.kind == .associatedtype && ref.kind == .protocol) {
ref.role = .inheritedType
} else if decl.kind == .protocol, ref.kind == .protocol {
ref.role = .refinedProtocolType
}
Expand All @@ -411,6 +412,8 @@ public final class SwiftIndexer: Indexer {
ref.role = .variableInitFunctionCall
} else if result.functionCallMetatypeArgumentLocations.contains(ref.location) {
ref.role = .functionCallMetatypeArgument
} else if result.typeInitializerLocations.contains(ref.location) {
ref.role = .initializerType
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/PeripheryKit/Syntax/DeclarationSyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
genericConformanceRequirementLocations: Set<SourceLocation>,
variableInitFunctionCallLocations: Set<SourceLocation>,
functionCallMetatypeArgumentLocations: Set<SourceLocation>,
typeInitializerLocations: Set<SourceLocation>,
hasCapitalSelfFunctionCall: Bool,
hasGenericFunctionReturnedMetatypeParameters: Bool
)
Expand Down Expand Up @@ -246,9 +247,9 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
modifiers: node.modifiers,
attributes: node.attributes,
trivia: node.leadingTrivia,
variableType: node.initializer.value,
genericParameterClause: node.genericParameterClause,
genericWhereClause: node.genericWhereClause,
typeInitializerClause: node.initializer,
at: node.name.positionAfterSkippingLeadingTrivia
)
}
Expand All @@ -260,6 +261,7 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
trivia: node.leadingTrivia,
inheritanceClause: node.inheritanceClause,
genericWhereClause: node.genericWhereClause,
typeInitializerClause: node.initializer,
at: node.name.positionAfterSkippingLeadingTrivia
)
}
Expand Down Expand Up @@ -304,6 +306,7 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
genericParameterClause: GenericParameterClauseSyntax? = nil,
genericWhereClause: GenericWhereClauseSyntax? = nil,
variableInitFunctionCallExpr: FunctionCallExprSyntax? = nil,
typeInitializerClause: TypeInitializerClauseSyntax? = nil,
consumeCapitalSelfFunctionCalls: Bool = false,
at position: AbsolutePosition
) {
Expand Down Expand Up @@ -350,6 +353,7 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
genericConformanceRequirementLocations: typeLocations(for: genericWhereClause),
variableInitFunctionCallLocations: locations(for: variableInitFunctionCallExpr),
functionCallMetatypeArgumentLocations: functionCallMetatypeArgumentLocations(for: variableInitFunctionCallExpr),
typeInitializerLocations: typeLocations(for: typeInitializerClause?.value),
hasCapitalSelfFunctionCall: didVisitCapitalSelfFunctionCall,
hasGenericFunctionReturnedMetatypeParameters: hasGenericFunctionReturnedMetatypeParameters
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
),
],
targets: [
.target(
.executableTarget(
name: "MainTarget",
dependencies: ["TargetA"]),
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ _ = InternalProtocolRefiningPublicProtocolRetainer()
let _ = PublicTypeUsedInPublicClosureRetainer().closure

// Async

Task {
await PublicActor().someFunc()
}

// Property wrappers

_ = PublicWrappedProperty().wrappedProperty

// Inline

// Inlining
inlinableFunction()

// Associated types
_ = PublicInheritedAssociatedTypeClass().items
_ = PublicInheritedAssociatedTypeDefaultTypeClass().items
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

public protocol PublicInheritedAssociatedTypeDefaultType {}

public protocol PublicInheritedAssociatedTypeDefaultTypeProtocol {
associatedtype Value = PublicInheritedAssociatedTypeDefaultType

var items: [Value] { get }
}

public class PublicInheritedAssociatedTypeDefaultTypeClass: PublicInheritedAssociatedTypeDefaultTypeProtocol {
public init() {}
public let items: [Int] = []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation

public protocol PublicInheritedAssociatedType {}

public protocol PublicInheritedAssociatedTypeProtocol {
associatedtype Value: PublicInheritedAssociatedType

var items: [Value] { get }
}

public class PublicInheritedAssociatedTypeClass: PublicInheritedAssociatedTypeProtocol {
public init() {}
public let items: [Int] = []
}

extension Int: PublicInheritedAssociatedType {}
12 changes: 12 additions & 0 deletions Tests/AccessibilityTests/RedundantPublicAccessibilityTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,16 @@ class RedundantPublicAccessibilityTest: SourceGraphTestCase {
assertNotRedundantPublicAccessibility(.class("ClassReferencedFromPublicInlinableFunction"))
assertNotRedundantPublicAccessibility(.class("ClassReferencedFromPublicInlinableFunction_UsableFromInline"))
}

func testPublicInheritedAssociatedType() {
Self.index()

assertNotRedundantPublicAccessibility(.protocol("PublicInheritedAssociatedType"))
}

func testPublicAssociatedTypeDefaultType() {
Self.index()

assertNotRedundantPublicAccessibility(.protocol("PublicInheritedAssociatedTypeDefaultType"))
}
}

0 comments on commit 3d56f8d

Please sign in to comment.