Files
swift-mirror/test/Unsafe/unsafe_stdlib.swift
Gabor Horvath 402ad33463 [StrictMemorySafety] Check the safety of return types of calls
Previously, we skipped checking the return type of a function for safety
as we expected to warn at the use of the returned value:

  let x = returnsUnsafe()
  usesUnsafe(x) // warn here

Unfortunately, this resulted in missing some unsafe constructs that can
introduce memory safety issues when the use of the return value had a
different shape resulting in false negatives for cases like:

  return returnsUnsafe()

or

  usesUnsafe(returnsUnsafe())

This PR changes the analysis to always take return types of function
calls into account.

rdar://157237301
2025-08-05 12:16:44 +01:00

21 lines
982 B
Swift

// RUN: %target-typecheck-verify-swift -strict-memory-safety
// Make sure everything compiles without error when unsafe code is allowed.
// RUN: %target-swift-frontend -typecheck -warnings-as-errors %s
func test(
x: OpaquePointer,
other: UnsafeMutablePointer<Int>
) {
var array = [1, 2, 3]
// expected-warning@+3{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{3-3=unsafe }}
// expected-note@+2{{argument #0 in call to instance method 'withUnsafeBufferPointer' has unsafe type '(UnsafeBufferPointer<Int>) -> ()'}}
// expected-note@+1{{reference to instance method 'withUnsafeBufferPointer' involves unsafe type 'UnsafeBufferPointer<Int>'}}
array.withUnsafeBufferPointer{ buffer in
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{5-5=unsafe }}
print(buffer) // expected-note{{reference to parameter 'buffer' involves unsafe type 'UnsafeBufferPointer<Int>'}}
}
array.append(4)
_ = array
}