Files
swift-mirror/test/Unsafe/safe_argument_suppression.swift
Doug Gregor ee9487b86f [Strict memory safety] Provide argument-specific diagnostics for calls
Similar to what we do for 'throws' checking, perform argument-specific
checking for unsafe call arguments. This provides more detailed failures:

```
example.swift:18:3: warning: expression uses unsafe constructs but is not
marked with 'unsafe' [#StrictMemorySafety]
16 |   x.f(a: 0, b: 17, c: nil)
17 |
18 |   x.f(a: 0, b: 17, c: &i)
   |   |                   `- note: argument 'c' in call to instance
method 'f' has unsafe type 'UnsafePointer<Int>?'
   |   `- warning: expression uses unsafe constructs but is not marked
with 'unsafe' [#StrictMemorySafety]
19 |   unsafeF()
20 | }
```

It also means that we won't complain for `nil` or `Optional.none`
arguments passed to unsafe types, which eliminates some false
positives, and won't complain about unsafe result types when there is
a call---because we'd still get complaints later about the
actually-unsafe bit, which is using those results.

Fixes rdar://149629670.
2025-04-25 21:54:19 -07:00

51 lines
1.2 KiB
Swift

// RUN: %target-typecheck-verify-swift -strict-memory-safety
@unsafe
class NotSafe {
@safe var okay: Int { 0 }
@safe var safeSelf: NotSafe { unsafe self }
@safe func memberFunc(_: NotSafe) { }
@safe subscript(ns: NotSafe) -> Int { 5 }
@safe static func doStatically(_: NotSafe.Type) { }
@safe static subscript(ns: NotSafe) -> Int { 5 }
@safe init(_: NotSafe) { }
func stillUnsafe() { }
}
@unsafe
class NotSafeSubclass: NotSafe {
}
@safe func okayFunc(_ ns: NotSafe) { }
@safe func testImpliedSafety(ns: NotSafe) {
_ = ns.okay
_ = ns.safeSelf.okay
ns.memberFunc(ns)
okayFunc(ns)
_ = ns[ns]
_ = NotSafe(ns)
_ = NotSafe[ns]
NotSafe.doStatically(NotSafe.self)
ns.stillUnsafe() // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}
// expected-note@-1{{reference to parameter 'ns' involves unsafe type 'NotSafe'}}
// expected-note@-2{{argument 'self' in call to instance method 'stillUnsafe' has unsafe type 'NotSafe'}}
}
@safe func testImpliedSafetySubclass(ns: NotSafeSubclass) {
_ = ns.okay
_ = ns.safeSelf.okay
ns.memberFunc(ns)
okayFunc(ns)
_ = ns[ns]
}