|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import SwiftExtract |
| 16 | +import Testing |
| 17 | + |
| 18 | +@Suite("Function type effect specifiers") |
| 19 | +struct FunctionTypeEffectSpecifierSuite { |
| 20 | + |
| 21 | + private func closureParameterType(_ source: String) throws -> SwiftFunctionType { |
| 22 | + let result = try analyze( |
| 23 | + sources: [("/fake/Source.swift", source)], |
| 24 | + moduleName: "Test" |
| 25 | + ) |
| 26 | + |
| 27 | + let fn = try #require(result.extractedGlobalFuncs.first { $0.name == "take" }) |
| 28 | + guard case .function(let fnType) = fn.functionSignature.parameters[0].type else { |
| 29 | + throw TestError("expected .function parameter, got \(fn.functionSignature.parameters[0].type)") |
| 30 | + } |
| 31 | + return fnType |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + func asyncClosureRecordsAsync() throws { |
| 36 | + let fnType = try closureParameterType("public func take(_ cb: () async -> Void) {}") |
| 37 | + |
| 38 | + #expect(fnType.effectSpecifiers == [.async]) |
| 39 | + #expect(fnType.isAsync) |
| 40 | + #expect(!fnType.isThrowing) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + func throwingClosureRecordsThrows() throws { |
| 45 | + let fnType = try closureParameterType("public func take(_ cb: () throws -> Void) {}") |
| 46 | + |
| 47 | + #expect(fnType.effectSpecifiers == [.throws]) |
| 48 | + #expect(!fnType.isAsync) |
| 49 | + #expect(fnType.isThrowing) |
| 50 | + #expect(!fnType.isTypedThrowing) |
| 51 | + #expect(fnType.thrownTypedError == nil) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + func typedThrowsClosureRecordsThrownErrorType() throws { |
| 56 | + let fnType = try closureParameterType( |
| 57 | + """ |
| 58 | + public struct FishTankError: Error {} |
| 59 | + public func take(_ cb: () throws(FishTankError) -> Void) {} |
| 60 | + """ |
| 61 | + ) |
| 62 | + |
| 63 | + #expect(fnType.effectSpecifiers == [.throws]) |
| 64 | + #expect(fnType.isThrowing) |
| 65 | + #expect(fnType.isTypedThrowing) |
| 66 | + #expect(fnType.thrownTypedError?.description == "FishTankError") |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + func unresolvableThrownErrorTypeStillRecordsThrows() throws { |
| 71 | + let fnType = try closureParameterType("public func take(_ cb: () throws(NoSuchError) -> Void) {}") |
| 72 | + |
| 73 | + #expect(fnType.effectSpecifiers == [.throws]) |
| 74 | + #expect(fnType.isThrowing) |
| 75 | + #expect(fnType.thrownTypedError == nil) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + func asyncThrowingClosureRecordsBoth() throws { |
| 80 | + let fnType = try closureParameterType("public func take(_ cb: () async throws -> Void) {}") |
| 81 | + |
| 82 | + #expect(fnType.effectSpecifiers == [.async, .throws]) |
| 83 | + #expect(fnType.isAsync) |
| 84 | + #expect(fnType.isThrowing) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + func descriptionRendersEffectSpecifiers() throws { |
| 89 | + let fnType = try closureParameterType( |
| 90 | + "public func take(_ cb: @escaping (Int) async throws -> Void) {}" |
| 91 | + ) |
| 92 | + |
| 93 | + #expect(fnType.description == "@escaping (Int) async throws -> Void") |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + func descriptionRendersThrownErrorType() throws { |
| 98 | + let fnType = try closureParameterType( |
| 99 | + """ |
| 100 | + public struct FishTankError: Error {} |
| 101 | + public func take(_ cb: @escaping (Int) async throws(FishTankError) -> Void) {} |
| 102 | + """ |
| 103 | + ) |
| 104 | + |
| 105 | + #expect(fnType.description == "@escaping (Int) async throws(FishTankError) -> Void") |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + func effectsOnReturnedClosureAreRecorded() throws { |
| 110 | + let result = try analyze( |
| 111 | + sources: [("/fake/Source.swift", "public func get() -> () async -> Void { fatalError() }")], |
| 112 | + moduleName: "Test" |
| 113 | + ) |
| 114 | + |
| 115 | + let fn = try #require(result.extractedGlobalFuncs.first { $0.name == "get" }) |
| 116 | + guard case .function(let fnType) = fn.functionSignature.result.type else { |
| 117 | + Issue.record("expected .function result, got \(fn.functionSignature.result.type)") |
| 118 | + return |
| 119 | + } |
| 120 | + #expect(fnType.effectSpecifiers == [.async]) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +private struct TestError: Error, CustomStringConvertible { |
| 125 | + let description: String |
| 126 | + init(_ description: String) { |
| 127 | + self.description = description |
| 128 | + } |
| 129 | +} |
0 commit comments