Skip to content
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 @@ -20,19 +20,26 @@ extension DeclBuilder {

public var inheritedAccessControlLevel: TokenSyntax? {
let settings = settings.accessControlLevel
return basicDeclaration.accessControlLevel(
return basicDeclaration.inlinableAccessControlLevel(
inheritedBy: settings.inheritingDeclaration,
maxAllowed: settings.maxAllowed
)
}
}

extension DeclBuilder {

public var inheritedGlobalActorIsolation: GlobalActorIsolation? {
if let preferred = settings.preferredGlobalActorIsolation {
return preferred
if let explicit = settings.explicitGlobalActorIsolation {
return explicit
}
if let inferredType = basicDeclaration.globalActor?.attributeName.trimmed {
return .isolated(trimmedType: inferredType)
if let inherited = basicDeclaration.globalActor?.attributeName {
return .isolated(trimmedType: inherited.trimmed)
}
return .nonisolated
}

public var inheritedGlobalActorAttribute: AttributeSyntax? {
inheritedGlobalActorIsolation?.inlinableAttribute
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import SwiftSyntax
public struct DeclBuilderSettings {

public var accessControlLevel: AccessControlLevel
public var preferredGlobalActorIsolation: GlobalActorIsolation?
public var explicitGlobalActorIsolation: GlobalActorIsolation?

public init(
accessControlLevel: AccessControlLevel,
preferredGlobalActorIsolation: GlobalActorIsolation? = nil
explicitGlobalActorIsolation: GlobalActorIsolation? = nil
) {
self.accessControlLevel = accessControlLevel
self.preferredGlobalActorIsolation = preferredGlobalActorIsolation
self.explicitGlobalActorIsolation = explicitGlobalActorIsolation
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ extension TypeDeclBuilder {

extension TypeDeclBuilder {

public var trimmedTypeName: TypeSyntax {
public var trimmedType: TypeSyntax {
switch TypeDeclBuilderContext.current {
case let .extension(extendedType):
extendedType.trimmed
case let .extension(trimmedType):
trimmedType
case .declaration:
"\(typeDeclaration.name.trimmed)"
}
}

public func buildExtension(of extendedType: some TypeSyntaxProtocol) throws -> MemberBlockSyntax {
try TypeDeclBuilderContext.$current.withValue(
.extension(TypeSyntax(extendedType)),
.extension(trimmedType: TypeSyntax(extendedType.trimmed)),
operation: {
try MemberBlockSyntax(
members: MemberBlockItemListSyntax(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftSyntax
internal enum TypeDeclBuilderContext {

case declaration
case `extension`(TypeSyntax)
case `extension`(trimmedType: TypeSyntax)
}

extension TypeDeclBuilderContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension WithModifiersSyntax {

extension WithModifiersSyntax {

public func accessControlLevel(
public func inlinableAccessControlLevel(
inheritedBy inheritingDeclaration: InheritingDeclaration,
maxAllowed: Keyword
) -> TokenSyntax? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ public enum GlobalActorIsolation: Hashable {

public var trimmedType: TypeSyntax? {
switch self {
case let .isolated(type):
type
case let .isolated(trimmedType):
trimmedType
case .nonisolated:
nil
}
}
Comment on lines 16 to 23

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Bug: missing return statements in switch within computed property

The switch inside trimmedType omits return in each case and will not compile. Add explicit returns or rewrite without switch.

Apply one of the following:

Option A (minimal change):

     public var trimmedType: TypeSyntax? {
         switch self {
-        case let .isolated(trimmedType):
-            trimmedType
+        case let .isolated(trimmedType):
+            return trimmedType
         case .nonisolated:
-            nil
+            return nil
         }
     }

Option B (more concise):

-    public var trimmedType: TypeSyntax? {
-        switch self {
-        case let .isolated(trimmedType):
-            trimmedType
-        case .nonisolated:
-            nil
-        }
-    }
+    public var trimmedType: TypeSyntax? {
+        if case let .isolated(trimmedType) = self {
+            return trimmedType
+        }
+        return nil
+    }
🤖 Prompt for AI Agents
In Sources/PrincipleMacros/Syntax/Helpers/GlobalActorIsolation.swift around
lines 16 to 23, the computed property trimmedType uses a switch where each case
lacks an explicit return causing a compile error; fix by either adding explicit
returns (e.g., return trimmedType and return nil) in each case, or replace the
switch with a concise expression (for example use if case let .isolated(t) =
self { return t } else { return nil } or a single-line computed return using a
pattern match) so the property returns a TypeSyntax? value.


public var trimmedAttribute: AttributeSyntax? {
public var inlinableAttribute: AttributeSyntax? {
guard let trimmedType else {
return nil
}
return AttributeSyntax(
attributeName: trimmedType
)
let attribute = AttributeSyntax(attributeName: trimmedType)
return attribute.withTrailingSpace
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extension ParameterExtractorTests {
func testIsolatedPreferredGlobalActorExtraction(isolation: String) throws {
let extractor = try makeExtractor(from: "#MyMacro(isolation: \(raw: isolation).self)")
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
#expect(extracted?.trimmedAttribute?.description == "@\(isolation)")
#expect(extracted?.trimmedType?.description == isolation)
}

@Test
Expand Down
Loading