1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
//===--- Concurrency.cpp --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 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
//
//===----------------------------------------------------------------------===//
#include "swift/AST/Concurrency.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SourceFile.h"
using namespace swift;
std::optional<DiagnosticBehavior>
swift::getConcurrencyDiagnosticBehaviorLimit(NominalTypeDecl *nominal,
const DeclContext *fromDC,
bool ignoreExplicitConformance) {
ModuleDecl *importedModule = nullptr;
if (nominal->getAttrs().hasAttribute<PreconcurrencyAttr>()) {
// If the declaration itself has the @preconcurrency attribute,
// respect it.
importedModule = nominal->getParentModule();
} else {
// Determine whether this nominal type is visible via a @preconcurrency
// import.
auto import = nominal->findImport(fromDC);
auto sourceFile = fromDC->getParentSourceFile();
if (!import || !import->options.contains(ImportFlags::Preconcurrency))
return std::nullopt;
if (sourceFile)
sourceFile->setImportUsedPreconcurrency(*import);
importedModule = import->module.importedModule;
}
// When the type is explicitly non-Sendable, @preconcurrency imports
// downgrade the diagnostic to a warning in Swift 6.
if (!ignoreExplicitConformance && hasExplicitSendableConformance(nominal))
return DiagnosticBehavior::Warning;
// When the type is implicitly non-Sendable, `@preconcurrency` suppresses
// diagnostics until the imported module enables Swift 6.
return importedModule->isConcurrencyChecked() ? DiagnosticBehavior::Warning
: DiagnosticBehavior::Ignore;
}
/// Determine whether the given nominal type has an explicit Sendable
/// conformance (regardless of its availability).
bool swift::hasExplicitSendableConformance(NominalTypeDecl *nominal,
bool applyModuleDefault) {
ASTContext &ctx = nominal->getASTContext();
auto nominalModule = nominal->getParentModule();
// In a concurrency-checked module, a missing conformance is equivalent to
// an explicitly unavailable one. If we want to apply this rule, do so now.
if (applyModuleDefault && nominalModule->isConcurrencyChecked())
return true;
// Look for any conformance to `Sendable`.
auto proto = ctx.getProtocol(KnownProtocolKind::Sendable);
if (!proto)
return false;
// Look for a conformance. If it's present and not (directly) missing,
// we're done.
auto conformance = lookupConformance(nominal->getDeclaredInterfaceType(),
proto, /*allowMissing=*/true);
return conformance &&
!(isa<BuiltinProtocolConformance>(conformance.getConcrete()) &&
cast<BuiltinProtocolConformance>(conformance.getConcrete())
->isMissing());
}
|