mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Implement an unsafe expression to cover uses of unsafe constructs
Introduce an `unsafe` expression akin to `try` and `await` that notes
that there are unsafe constructs in the expression to the right-hand
side. Extend the effects checker to also check for unsafety along with
throwing and async operations. This will result in diagnostics like
the following:
10 | func sum() -> Int {
11 | withUnsafeBufferPointer { buffer in
12 | let value = buffer[0]
| | `- note: reference to unsafe subscript 'subscript(_:)'
| |- warning: expression uses unsafe constructs but is not marked with 'unsafe'
| `- note: reference to parameter 'buffer' involves unsafe type 'UnsafeBufferPointer<Int>'
13 | tryWithP(X())
14 | return fastAdd(buffer.baseAddress, buffer.count)
These will come with a Fix-It that inserts `unsafe` into the proper
place. There's also a warning that appears when `unsafe` doesn't cover
any unsafe code, making it easier to clean up extraneous `unsafe`.
This approach requires that `@unsafe` be present on any declaration
that involves unsafe constructs within its signature. Outside of the
signature, the `unsafe` expression is used to identify unsafe code.
This commit is contained in:
@@ -1707,12 +1707,12 @@ private:
|
||||
auto SR = E->getSourceRange();
|
||||
if (SR.isValid() && SM.rangeContainsTokenLoc(SR, TargetLoc) &&
|
||||
!checkCallExpr(E) && !EnclosingCallAndArg.first) {
|
||||
if (!isa<TryExpr>(E) && !isa<AwaitExpr>(E) &&
|
||||
if (!isa<TryExpr>(E) && !isa<AwaitExpr>(E) && !isa<UnsafeExpr>(E) &&
|
||||
!isa<PrefixUnaryExpr>(E)) {
|
||||
// We don't want to expand to trailing closures if the call is
|
||||
// nested inside another expression that has closing characters,
|
||||
// like a `)` for a function call. This is not the case for
|
||||
// `try`, `await` and prefix operator applications.
|
||||
// `try`, `await`, `unsafe` and prefix operator applications.
|
||||
OuterExpr = E;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user