Skip to content

Empty array initialization [TypeName]() causes build error in Swift 6 with cross-module types #331

@kuritatu18

Description

@kuritatu18

Problem

When mockolo generates a mock for a protocol method that returns an array, it uses the syntax [TypeName]() for the default return value. This causes a build error in Swift 6 when the type is defined in another module and doesn't have a publicly accessible initializer.

Environment

  • mockolo version: 2.4.0
  • Xcode version: 26.0
  • Swift version: 6.2

Steps to Reproduce

  1. Define a struct in Module A without a public parameterless initializer:
// Module A
public struct SomeModel: Sendable {
    public let id: String

    public init(entity: SomeEntity) {
        self.id = entity.id
    }
}
  1. Define a protocol in Module B that returns an array of this type:
// Module B
/// @mockable
public protocol SomeRepository: Sendable {
    func fetchItems(ids: [String]) -> [SomeModel]
}
  1. Run mockolo to generate mocks (Mock module imports Module A and Module B)
  2. Build the Mock module

Generated Code

public func fetchItems(ids: [String]) -> [SomeModel] {
    let handler = handlerState.withLock { state in
        state.callCount += 1
        return state.handler
    }
    if let handler = handler {
        return handler(ids)
    }
    return [SomeModel]()  // ❌ This line causes the error
}

Build Error

error: cannot call value of non-function type '[SomeModel.Type]' (aka 'Array<SomeModel.Type>')

Expected Behavior

mockolo should generate [] instead of [TypeName]() for empty array default values:

return []  // ✅ Works correctly - type is inferred from return type

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions