Skip to content

[TaskLocal] TL macro must handle force unwrapped optional types #73693

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

Merged
merged 2 commits into from
May 17, 2024
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
13 changes: 13 additions & 0 deletions lib/Macros/Sources/SwiftMacros/SyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ extension VariableDeclSyntax {
modifier.isAccessControl
}
}
}

extension ImplicitlyUnwrappedOptionalTypeSyntax {
internal var asOptionalTypeSyntax: any TypeSyntaxProtocol {
OptionalTypeSyntax(
leadingTrivia: leadingTrivia,
unexpectedBeforeWrappedType,
wrappedType: wrappedType,
self.unexpectedBetweenWrappedTypeAndExclamationMark,
self.unexpectedAfterExclamationMark,
trailingTrivia: self.trailingTrivia
)
}
}
19 changes: 13 additions & 6 deletions lib/Macros/Sources/SwiftMacros/TaskLocalMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,24 @@ extension TaskLocalMacro: PeerMacro {
message: "'@TaskLocal' property must have name", id: .incompatibleDecl)
}

let type = firstBinding.typeAnnotation?.type
let originalType = firstBinding.typeAnnotation?.type
let explicitTypeAnnotation: TypeAnnotationSyntax?
if let type {
explicitTypeAnnotation = TypeAnnotationSyntax(type: TypeSyntax("TaskLocal<\(type.trimmed)>"))
let taskLocalWrappedType: TypeSyntax?
if let forceUnwrappedOptionalType = firstBinding.typeAnnotation?.type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) {
taskLocalWrappedType = TypeSyntax("\(forceUnwrappedOptionalType.wrappedType.trimmed)?")
} else {
taskLocalWrappedType = originalType?.trimmed
}
if let taskLocalWrappedType {
explicitTypeAnnotation = TypeAnnotationSyntax(type: TypeSyntax("TaskLocal<\(taskLocalWrappedType)>"))
} else {
explicitTypeAnnotation = nil
}

let initialValue: ExprSyntax
if let initializerValue = firstBinding.initializer?.value {
initialValue = ExprSyntax(initializerValue)
} else if let type, type.isOptional {
} else if let originalType, originalType.isOptional {
initialValue = ExprSyntax(NilLiteralExprSyntax())
} else {
throw DiagnosticsError(
Expand Down Expand Up @@ -162,7 +168,7 @@ extension TypeSyntax {
// and T? we can detect the optional.
fileprivate var isOptional: Bool {
switch self.as(TypeSyntaxEnum.self) {
case .optionalType:
case .optionalType, .implicitlyUnwrappedOptionalType:
return true
case .identifierType(let identifierType):
return identifierType.name.text == "Optional"
Expand All @@ -172,7 +178,8 @@ extension TypeSyntax {
return false
}
return memberType.name.text == "Optional"
default: return false
default:
return false
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/Concurrency/Runtime/async_task_locals_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ enum TL {

@TaskLocal
static var clazz: ClassTaskLocal?

@TaskLocal
static var force: ForceUnwrapMe!
}

@TaskLocal
Expand All @@ -43,6 +46,8 @@ var globalTaskLocal: StringLike = StringLike("<not-set>")
@available(SwiftStdlib 5.10, *)
struct LessAvailable {}

struct ForceUnwrapMe {}

@TaskLocal
@available(SwiftStdlib 5.10, *)
var globalLessAvailable: LessAvailable?
Expand Down