Skip to content

Commit 35b5c81

Browse files
authored
Merge pull request #83520 from Xazax-hun/check-safety-function-types
2 parents 7149122 + 402ad33 commit 35b5c81

40 files changed

+126
-98
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8543,7 +8543,7 @@ static bool hasUnsafeType(Evaluator &evaluator, clang::QualType clangType) {
85438543
// All other pointers are considered unsafe.
85448544
return true;
85458545
}
8546-
8546+
85478547
// Handle records recursively.
85488548
if (auto recordDecl = clangType->getAsTagDecl()) {
85498549
// If we reached this point the types is not imported as a shared reference,
@@ -8620,7 +8620,7 @@ ClangDeclExplicitSafety::evaluate(Evaluator &evaluator,
86208620
if (hasUnsafeType(evaluator, field->getType()))
86218621
return ExplicitSafety::Unsafe;
86228622
}
8623-
8623+
86248624
// Okay, call it safe.
86258625
return ExplicitSafety::Safe;
86268626
}

lib/Sema/TypeCheckUnsafe.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
#include "TypeCheckUnsafe.h"
2222

2323
#include "swift/AST/ASTContext.h"
24-
#include "swift/AST/Effects.h"
25-
#include "swift/AST/UnsafeUse.h"
2624
#include "swift/AST/DiagnosticsSema.h"
25+
#include "swift/AST/Effects.h"
2726
#include "swift/AST/PackConformance.h"
2827
#include "swift/AST/SourceFile.h"
28+
#include "swift/AST/Types.h"
29+
#include "swift/AST/UnsafeUse.h"
2930

3031
using namespace swift;
3132

@@ -240,19 +241,29 @@ bool swift::enumerateUnsafeUses(ConcreteDeclRef declRef,
240241
// If the type of this declaration involves unsafe types, diagnose that.
241242
ASTContext &ctx = decl->getASTContext();
242243
auto subs = declRef.getSubstitutions();
243-
if (!skipTypeCheck) {
244-
auto type = decl->getInterfaceType();
245-
if (subs) {
246-
if (auto *genericFnType = type->getAs<GenericFunctionType>())
247-
type = genericFnType->substGenericArgs(subs);
248-
else
249-
type = type.subst(subs);
250-
}
244+
auto type = decl->getInterfaceType();
245+
if (subs) {
246+
if (auto *genericFnType = type->getAs<GenericFunctionType>())
247+
type = genericFnType->substGenericArgs(subs);
248+
else
249+
type = type.subst(subs);
250+
}
251251

252+
if (skipTypeCheck) {
253+
// We check the arguements instead of the funcion type for function calls.
254+
// On the other hand, we still need to check the return type as we might not
255+
// have a declRef with the type of the return type. E.g. in
256+
// `return funcionCall()`.
257+
if (auto *fnTy = type->getAs<AnyFunctionType>())
258+
type = fnTy->getResult();
259+
else
260+
type = Type();
261+
}
262+
if (type) {
252263
bool shouldReturnTrue = false;
253264
diagnoseUnsafeType(ctx, loc, type, [&](Type unsafeType) {
254265
if (fn(UnsafeUse::forReferenceToUnsafe(
255-
decl, isCall && !isa<ParamDecl>(decl), unsafeType, loc)))
266+
decl, isCall && !isa<ParamDecl>(decl), unsafeType, loc)))
256267
shouldReturnTrue = true;
257268
});
258269

stdlib/public/Concurrency/CheckedContinuation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
162162
/// the caller. The task continues executing when its executor is
163163
/// able to reschedule it.
164164
public func resume(returning value: sending T) {
165-
if let c: UnsafeContinuation<T, E> = canary.takeContinuation() {
165+
if let c: UnsafeContinuation<T, E> = unsafe canary.takeContinuation() {
166166
unsafe c.resume(returning: value)
167167
} else {
168168
#if !$Embedded
@@ -186,7 +186,7 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
186186
/// the caller. The task continues executing when its executor is
187187
/// able to reschedule it.
188188
public func resume(throwing error: __owned E) {
189-
if let c: UnsafeContinuation<T, E> = canary.takeContinuation() {
189+
if let c: UnsafeContinuation<T, E> = unsafe canary.takeContinuation() {
190190
unsafe c.resume(throwing: error)
191191
} else {
192192
#if !$Embedded

stdlib/public/Concurrency/Executor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ internal func _task_serialExecutor_getExecutorRef<E>(_ executor: E) -> Builtin.E
934934
@_silgen_name("_task_taskExecutor_getTaskExecutorRef")
935935
internal func _task_taskExecutor_getTaskExecutorRef<E>(_ taskExecutor: E) -> Builtin.Executor
936936
where E: TaskExecutor {
937-
return taskExecutor.asUnownedTaskExecutor().executor
937+
return unsafe taskExecutor.asUnownedTaskExecutor().executor
938938
}
939939

940940
#if $Embedded

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public struct ExecutorJob: Sendable, ~Copyable {
327327
/// Returns the result of executing the closure.
328328
@available(StdlibDeploymentTarget 6.2, *)
329329
public func withUnsafeExecutorPrivateData<R, E>(body: (UnsafeMutableRawBufferPointer) throws(E) -> R) throws(E) -> R {
330-
let base = _jobGetExecutorPrivateData(self.context)
330+
let base = unsafe _jobGetExecutorPrivateData(self.context)
331331
let size = unsafe 2 * MemoryLayout<OpaquePointer>.stride
332332
return unsafe try body(UnsafeMutableRawBufferPointer(start: base,
333333
count: size))
@@ -479,21 +479,21 @@ extension ExecutorJob {
479479

480480
/// Allocate a specified number of bytes of uninitialized memory.
481481
public func allocate(capacity: Int) -> UnsafeMutableRawBufferPointer {
482-
let base = _jobAllocate(context, capacity)
482+
let base = unsafe _jobAllocate(context, capacity)
483483
return unsafe UnsafeMutableRawBufferPointer(start: base, count: capacity)
484484
}
485485

486486
/// Allocate uninitialized memory for a single instance of type `T`.
487487
public func allocate<T>(as type: T.Type) -> UnsafeMutablePointer<T> {
488-
let base = _jobAllocate(context, MemoryLayout<T>.size)
488+
let base = unsafe _jobAllocate(context, MemoryLayout<T>.size)
489489
return unsafe base.bindMemory(to: type, capacity: 1)
490490
}
491491

492492
/// Allocate uninitialized memory for the specified number of
493493
/// instances of type `T`.
494494
public func allocate<T>(capacity: Int, as type: T.Type)
495495
-> UnsafeMutableBufferPointer<T> {
496-
let base = _jobAllocate(context, MemoryLayout<T>.stride * capacity)
496+
let base = unsafe _jobAllocate(context, MemoryLayout<T>.stride * capacity)
497497
let typedBase = unsafe base.bindMemory(to: T.self, capacity: capacity)
498498
return unsafe UnsafeMutableBufferPointer<T>(start: typedBase, count: capacity)
499499
}

stdlib/public/Concurrency/Task+PriorityEscalation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func __withTaskPriorityEscalationHandler0<T, E>(
125125
onPriorityEscalated handler0: @Sendable (UInt8, UInt8) -> Void,
126126
isolation: isolated (any Actor)? = #isolation
127127
) async throws(E) -> T {
128-
let record = _taskAddPriorityEscalationHandler(handler: handler0)
128+
let record = unsafe _taskAddPriorityEscalationHandler(handler: handler0)
129129
defer { unsafe _taskRemovePriorityEscalationHandler(record: record) }
130130

131131
return try await operation()

stdlib/public/Concurrency/Task+TaskExecutor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public func withTaskExecutorPreference<T, Failure>(
146146
}
147147

148148
let taskExecutorBuiltin: Builtin.Executor =
149-
taskExecutor.asUnownedTaskExecutor().executor
149+
unsafe taskExecutor.asUnownedTaskExecutor().executor
150150

151-
let record = _pushTaskExecutorPreference(taskExecutorBuiltin)
151+
let record = unsafe _pushTaskExecutorPreference(taskExecutorBuiltin)
152152
defer {
153153
unsafe _popTaskExecutorPreference(record: record)
154154
}
@@ -177,9 +177,9 @@ public func _unsafeInheritExecutor_withTaskExecutorPreference<T: Sendable>(
177177
}
178178

179179
let taskExecutorBuiltin: Builtin.Executor =
180-
taskExecutor.asUnownedTaskExecutor().executor
180+
unsafe taskExecutor.asUnownedTaskExecutor().executor
181181

182-
let record = _pushTaskExecutorPreference(taskExecutorBuiltin)
182+
let record = unsafe _pushTaskExecutorPreference(taskExecutorBuiltin)
183183
defer {
184184
unsafe _popTaskExecutorPreference(record: record)
185185
}

stdlib/public/Concurrency/Task.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ internal func _getCurrentTaskName() -> UnsafePointer<UInt8>?
984984

985985
@available(SwiftStdlib 6.2, *)
986986
internal func _getCurrentTaskNameString() -> String? {
987-
if let stringPtr = _getCurrentTaskName() {
987+
if let stringPtr = unsafe _getCurrentTaskName() {
988988
unsafe String(cString: stringPtr)
989989
} else {
990990
nil

stdlib/public/Concurrency/TaskCancellation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public func withTaskCancellationHandler<T>(
7777
) async rethrows -> T {
7878
// unconditionally add the cancellation record to the task.
7979
// if the task was already cancelled, it will be executed right away.
80-
let record = _taskAddCancellationHandler(handler: handler)
80+
let record = unsafe _taskAddCancellationHandler(handler: handler)
8181
defer { unsafe _taskRemoveCancellationHandler(record: record) }
8282

8383
return try await operation()
@@ -98,7 +98,7 @@ public func _unsafeInheritExecutor_withTaskCancellationHandler<T>(
9898
) async rethrows -> T {
9999
// unconditionally add the cancellation record to the task.
100100
// if the task was already cancelled, it will be executed right away.
101-
let record = _taskAddCancellationHandler(handler: handler)
101+
let record = unsafe _taskAddCancellationHandler(handler: handler)
102102
defer { unsafe _taskRemoveCancellationHandler(record: record) }
103103

104104
return try await operation()

stdlib/public/Concurrency/TaskLocal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
178178
/// or if the task-local has no value bound, this will return the `defaultValue`
179179
/// of the task local.
180180
public func get() -> Value {
181-
guard let rawValue = _taskLocalValueGet(key: key) else {
181+
guard let rawValue = unsafe _taskLocalValueGet(key: key) else {
182182
return self.defaultValue
183183
}
184184

0 commit comments

Comments
 (0)