Files
swift-mirror/include/swift/AST/ASTContextGlobalCache.h
Doug Gregor 9ea735b9ed Rework diagnostics for conformance isolation failures
A protocol conformance can be ill-formed due to isolation mismatches
between witnesses and requirements, or with associated conformances.
Previously, such failures would be emitted as a number of separate
errors (downgraded to warnings in Swift 5), one for each witness and
potentially an extra for associated conformances. The rest was a
potential flood of diagnostics that was hard to sort through.

Collect all of the isolation-related problems for a given conformance
together and produce a single error (downgraded to a warning when
appropriate) that describes the overall issue. That error will have up
to three notes suggesting specific courses of action:
* Isolating the conformance (when the experimental feature is enabled)
* Marking the witnesses as 'nonisolated' where needed
*

The diagnostic also has notes to point out the witnesses/associated
conformances that have isolation problems. There is a new educational
note that also describes these options.

We give the same treatment to missing 'distributed' on witnesses to a
distributed protocol.
2025-03-20 21:23:16 -07:00

89 lines
3.2 KiB
C++

//===--- ASTContextGlobalCache.h - AST Context Cache ------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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 defines the ASTContext::GlobalCache type. DO NOT include this
// header from any other header: it should only be included in those .cpp
// files that need to access the side tables. There are no include guards to
// force the issue.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTContext.h"
#include "swift/AST/ActorIsolation.h"
#include <variant>
namespace swift {
class NormalProtocolConformance;
class ValueDecl;
/// Describes an isolation error where the witness has actor isolation that
/// conflicts with the requirement's expectation.
struct WitnessIsolationError {
/// The requirement declaration.
ValueDecl *requirement;
/// The witness.
ValueDecl *witness;
/// The pre-Swift-6 diagnostic behavior.
DiagnosticBehavior behavior;
/// Whether the witness is missing "distributed".
bool isMissingDistributed;
/// The isolation of the requirement, mapped into the conformance.
ActorIsolation requirementIsolation;
/// The isolation domain that the witness would need to go into for it
/// to be suitable for the requirement.
ActorIsolation referenceIsolation;
/// Diagnose this witness isolation error.
void diagnose(const NormalProtocolConformance *conformance) const;
};
/// Describes an isolation error involving an associated conformance.
struct AssociatedConformanceIsolationError {
ProtocolConformance *isolatedConformance;
/// Diagnose this associated conformance isolation error.
void diagnose(const NormalProtocolConformance *conformance) const;
};
/// Describes an isolation error that occurred during conformance checking.
using ConformanceIsolationError = std::variant<
WitnessIsolationError, AssociatedConformanceIsolationError>;
/// A collection of side tables associated with the ASTContext itself, meant
/// as a way to keep sparse information out of the AST nodes themselves and
/// not require one to touch ASTContext.h to add such information.
struct ASTContext::GlobalCache {
/// Mapping from normal protocol conformances to the explicitly-specified
/// global actor isolations, e.g., when the conformance was spelled
/// `@MainActor P` or similar.
llvm::DenseMap<const NormalProtocolConformance *, TypeExpr *>
conformanceExplicitGlobalActorIsolation;
/// Mapping from normal protocol conformances to the set of actor isolation
/// problems that occur within the conformances.
///
/// This map will be empty for well-formed code, and is used to accumulate
/// information so that the diagnostics can be coalesced.
llvm::DenseMap<
const NormalProtocolConformance *,
std::vector<ConformanceIsolationError>
> conformanceIsolationErrors;
};
} // end namespace