Files
swift-mirror/lib/AST/Effects.cpp
Doug Gregor 8bb5bbedbc 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.
2025-01-10 10:39:14 -08:00

143 lines
4.3 KiB
C++

//===--- Effects.cpp - Effect Checking ASTs -------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements some logic for rethrows and reasync checking.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTContext.h"
#include "swift/AST/Effects.h"
#include "swift/AST/Evaluator.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/AST/Type.h"
#include "swift/AST/Types.h"
#include "swift/AST/TypeCheckRequests.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
bool AnyFunctionType::hasEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws: return getExtInfo().isThrowing();
case EffectKind::Async: return getExtInfo().isAsync();
case EffectKind::Unsafe: return false;
}
llvm_unreachable("Bad effect kind");
}
void swift::simple_display(llvm::raw_ostream &out, const EffectKind kind) {
switch (kind) {
case EffectKind::Throws: out << "throws"; return;
case EffectKind::Async: out << "async"; return;
case EffectKind::Unsafe: out << "@unsafe"; return;
}
llvm_unreachable("Bad effect kind");
}
void swift::simple_display(llvm::raw_ostream &out,
const PolymorphicEffectRequirementList list) {
for (auto req : list.getRequirements()) {
simple_display(out, req);
out << "\n";
}
for (auto conf : list.getConformances()) {
simple_display(out, conf.first);
out << " : ";
simple_display(out, conf.second);
llvm::errs() << "\n";
}
}
PolymorphicEffectRequirementList
ProtocolDecl::getPolymorphicEffectRequirements(EffectKind kind) const {
return evaluateOrDefault(getASTContext().evaluator,
PolymorphicEffectRequirementsRequest{kind, const_cast<ProtocolDecl *>(this)},
PolymorphicEffectRequirementList());
}
bool ProtocolDecl::hasPolymorphicEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws:
return getAttrs().hasAttribute<swift::AtRethrowsAttr>();
case EffectKind::Async:
return getAttrs().hasAttribute<swift::AtReasyncAttr>();
case EffectKind::Unsafe:
return false;
}
llvm_unreachable("Bad effect kind");
}
bool AbstractFunctionDecl::hasEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws:
return hasThrows();
case EffectKind::Async:
return hasAsync();
case EffectKind::Unsafe:
return isUnsafe();
}
llvm_unreachable("Bad effect kind");
}
bool AbstractFunctionDecl::hasPolymorphicEffect(EffectKind kind) const {
switch (kind) {
case EffectKind::Throws:
return getAttrs().hasAttribute<swift::RethrowsAttr>();
case EffectKind::Async:
return getAttrs().hasAttribute<swift::ReasyncAttr>();
case EffectKind::Unsafe:
return false;
}
llvm_unreachable("Bad effect kind");
}
PolymorphicEffectKind
AbstractFunctionDecl::getPolymorphicEffectKind(EffectKind kind) const {
return evaluateOrDefault(getASTContext().evaluator,
PolymorphicEffectKindRequest{kind, const_cast<AbstractFunctionDecl *>(this)},
PolymorphicEffectKind::Invalid);
}
void swift::simple_display(llvm::raw_ostream &out,
PolymorphicEffectKind kind) {
switch (kind) {
case PolymorphicEffectKind::None:
out << "none";
break;
case PolymorphicEffectKind::ByClosure:
out << "by closure";
break;
case PolymorphicEffectKind::ByConformance:
out << "by conformance";
break;
case PolymorphicEffectKind::AsyncSequenceRethrows:
out << "by async sequence implicit @rethrows";
break;
case PolymorphicEffectKind::Always:
out << "always";
break;
case PolymorphicEffectKind::Invalid:
out << "invalid";
break;
}
}
bool ProtocolConformanceRef::hasEffect(EffectKind kind) const {
if (!isConcrete()) { return kind != EffectKind::Unsafe; }
return evaluateOrDefault(getRequirement()->getASTContext().evaluator,
ConformanceHasEffectRequest{kind, getConcrete()},
true);
}