mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
We already need to track the inverses separate from the members in a ProtocolCompositionType, since inverses aren't real types. Thus, the only purpose being served by InverseType is to be eliminated by RequirementLowering when it appears in a conformance requirement. Instead, we introduce separate type InverseRequirement just to keep track of which inverses we encounter to facilitate cancelling-out defaults and ensuring that the inverses are respected after running the RequirementMachine.
192 lines
6.7 KiB
C++
192 lines
6.7 KiB
C++
//===--- Diagnostics.h - Requirement machine diagnostics --------*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2021 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_REQUIREMENT_DIAGNOSTICS_H
|
|
#define SWIFT_REQUIREMENT_DIAGNOSTICS_H
|
|
|
|
#include "swift/AST/ASTContext.h"
|
|
#include "swift/AST/Requirement.h"
|
|
#include "swift/AST/Type.h"
|
|
|
|
namespace swift {
|
|
|
|
namespace rewriting {
|
|
|
|
/// Represents an invalid requirement, such as `T: Int`.
|
|
///
|
|
/// Invalid requirements are recorded while computing the
|
|
/// generic signature of a declaration, and diagnosed via
|
|
/// \c diagnoseRequirementErrors .
|
|
struct RequirementError {
|
|
/// The kind of requirement error.
|
|
enum class Kind {
|
|
/// A constraint to a non-protocol, non-class type, e.g. T: Int.
|
|
InvalidTypeRequirement,
|
|
/// A type requirement on a trivially invalid subject type,
|
|
/// e.g. Bool: Collection.
|
|
InvalidRequirementSubject,
|
|
/// An inverse constraint applied to an invalid subject type,
|
|
/// e.g., each T : ~Copyable
|
|
InvalidInverseSubject,
|
|
/// The inverse constraint requirement cannot applied to the subject because
|
|
/// it's an outer generic parameter, e.g.,
|
|
/// protocol P { func f() where Self: ~Copyable }
|
|
InvalidInverseOuterSubject,
|
|
/// An invalid shape requirement, e.g. T.shape == Int.shape
|
|
InvalidShapeRequirement,
|
|
/// A pair of conflicting requirements, T == Int, T == String
|
|
ConflictingRequirement,
|
|
/// An inverse requirement that conflicts with the computed requirements of
|
|
/// a generic parameter, e.g., T : Copyable, T : ~Copyable
|
|
ConflictingInverseRequirement,
|
|
/// A recursive requirement, e.g. T == G<T.A>.
|
|
RecursiveRequirement,
|
|
/// A redundant requirement, e.g. T == T.
|
|
RedundantRequirement,
|
|
/// A redundant requirement, e.g. T : ~Copyable, T : ~Copyable.
|
|
RedundantInverseRequirement,
|
|
/// A not-yet-supported same-element requirement, e.g. each T == Int.
|
|
UnsupportedSameElement,
|
|
} kind;
|
|
|
|
private:
|
|
/// The invalid requirement.
|
|
union {
|
|
Requirement requirement;
|
|
InverseRequirement inverse;
|
|
};
|
|
public:
|
|
|
|
/// A requirement that conflicts with \c requirement. Both
|
|
/// requirements will have the same subject type.
|
|
llvm::Optional<Requirement> conflictingRequirement;
|
|
|
|
SourceLoc loc;
|
|
|
|
private:
|
|
RequirementError(Kind kind, Requirement requirement, SourceLoc loc)
|
|
: kind(kind), requirement(requirement),
|
|
conflictingRequirement(llvm::None), loc(loc) {}
|
|
|
|
RequirementError(Kind kind, InverseRequirement inverse, SourceLoc loc)
|
|
: kind(kind), inverse(inverse),
|
|
conflictingRequirement(llvm::None), loc(loc) {}
|
|
|
|
RequirementError(Kind kind, Requirement requirement,
|
|
Requirement conflict,
|
|
SourceLoc loc)
|
|
: kind(kind), requirement(requirement), conflictingRequirement(conflict), loc(loc) {}
|
|
|
|
public:
|
|
Requirement getRequirement() const {
|
|
assert(!(kind == Kind::InvalidInverseOuterSubject ||
|
|
kind == Kind::RedundantInverseRequirement ||
|
|
kind == Kind::ConflictingInverseRequirement));
|
|
return requirement;
|
|
}
|
|
|
|
InverseRequirement getInverse() const {
|
|
assert(kind == Kind::InvalidInverseOuterSubject ||
|
|
kind == Kind::RedundantInverseRequirement ||
|
|
kind == Kind::ConflictingInverseRequirement);
|
|
return inverse;
|
|
}
|
|
|
|
static RequirementError forInvalidTypeRequirement(Type subjectType,
|
|
Type constraint,
|
|
SourceLoc loc) {
|
|
Requirement requirement(RequirementKind::Conformance, subjectType, constraint);
|
|
return {Kind::InvalidTypeRequirement, requirement, loc};
|
|
}
|
|
|
|
static RequirementError forInvalidRequirementSubject(Requirement req,
|
|
SourceLoc loc) {
|
|
return {Kind::InvalidRequirementSubject, req, loc};
|
|
}
|
|
|
|
static RequirementError forInvalidInverseSubject(Requirement req,
|
|
SourceLoc loc) {
|
|
return {Kind::InvalidInverseSubject, req, loc};
|
|
}
|
|
|
|
static
|
|
RequirementError forInvalidInverseOuterSubject(InverseRequirement req) {
|
|
return {Kind::InvalidInverseOuterSubject, req, req.loc};
|
|
}
|
|
|
|
static RequirementError forConflictingInverseRequirement(
|
|
InverseRequirement req,
|
|
SourceLoc loc) {
|
|
return {Kind::ConflictingInverseRequirement, req, loc};
|
|
}
|
|
|
|
static RequirementError forInvalidShapeRequirement(Requirement req,
|
|
SourceLoc loc) {
|
|
return {Kind::InvalidShapeRequirement, req, loc};
|
|
}
|
|
|
|
static RequirementError forConflictingRequirement(Requirement req,
|
|
SourceLoc loc) {
|
|
return {Kind::ConflictingRequirement, req, loc};
|
|
}
|
|
|
|
static RequirementError forConflictingRequirement(Requirement first,
|
|
Requirement second,
|
|
SourceLoc loc) {
|
|
return {Kind::ConflictingRequirement, first, second, loc};
|
|
}
|
|
|
|
static RequirementError forRedundantRequirement(Requirement req,
|
|
SourceLoc loc) {
|
|
return {Kind::RedundantRequirement, req, loc};
|
|
}
|
|
|
|
static
|
|
RequirementError forRedundantInverseRequirement(InverseRequirement req) {
|
|
return {Kind::RedundantInverseRequirement, req, req.loc};
|
|
}
|
|
|
|
static RequirementError forRecursiveRequirement(Requirement req,
|
|
SourceLoc loc) {
|
|
return {Kind::RecursiveRequirement, req, loc};
|
|
}
|
|
|
|
static RequirementError forSameElement(Requirement req, SourceLoc loc) {
|
|
return {Kind::UnsupportedSameElement, req, loc};
|
|
}
|
|
};
|
|
|
|
/// Policy for the fixit that transforms 'T : S' where 'S' is not a protocol
|
|
/// or a class into 'T == S'.
|
|
enum AllowConcreteTypePolicy {
|
|
/// Any type parameter can be concrete.
|
|
All,
|
|
|
|
/// Only associated types can be concrete.
|
|
AssocTypes,
|
|
|
|
/// Only nested associated types can be concrete. This is for protocols,
|
|
/// where we don't want to suggest making an associated type member of
|
|
/// 'Self' concrete.
|
|
NestedAssocTypes
|
|
};
|
|
|
|
bool diagnoseRequirementErrors(ASTContext &ctx,
|
|
ArrayRef<RequirementError> errors,
|
|
AllowConcreteTypePolicy concreteTypePolicy);
|
|
|
|
} // end namespace rewriting
|
|
|
|
} // end namespace swift
|
|
|
|
#endif
|