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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
//===--- 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 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.
std::optional<Requirement> conflictingRequirement;
SourceLoc loc;
private:
RequirementError(Kind kind, Requirement requirement, SourceLoc loc)
: kind(kind), requirement(requirement),
conflictingRequirement(std::nullopt), loc(loc) {}
RequirementError(Kind kind, InverseRequirement inverse, SourceLoc loc)
: kind(kind), inverse(inverse), conflictingRequirement(std::nullopt),
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::InvalidInverseSubject &&
kind != Kind::ConflictingInverseRequirement);
return requirement;
}
InverseRequirement getInverse() const {
assert(kind == Kind::InvalidInverseOuterSubject ||
kind == Kind::InvalidInverseSubject ||
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(InverseRequirement inv) {
return {Kind::InvalidInverseSubject, inv, inv.loc};
}
static
RequirementError forInvalidInverseOuterSubject(InverseRequirement inv) {
return {Kind::InvalidInverseOuterSubject, inv, inv.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 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
|