|
| 1 | +//===--- Diagnostics.h - Requirement machine diagnostics --------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_REQUIREMENT_DIAGNOSTICS_H |
| 14 | +#define SWIFT_REQUIREMENT_DIAGNOSTICS_H |
| 15 | + |
| 16 | +#include "swift/AST/Requirement.h" |
| 17 | +#include "swift/AST/Type.h" |
| 18 | + |
| 19 | +namespace swift { |
| 20 | + |
| 21 | +namespace rewriting { |
| 22 | + |
| 23 | +/// Represents an invalid requirement, such as `T: Int`. |
| 24 | +/// |
| 25 | +/// Invalid requirements are recorded while computing the |
| 26 | +/// generic signature of a declaration, and diagnosed via |
| 27 | +/// \c diagnoseRequirementErrors . |
| 28 | +struct RequirementError { |
| 29 | + /// The kind of requirement error. |
| 30 | + enum class Kind { |
| 31 | + /// A constraint to a non-protocol, non-class type, e.g. T: Int. |
| 32 | + InvalidTypeRequirement, |
| 33 | + /// A type mismatch, e.g. Int == String. |
| 34 | + ConcreteTypeMismatch, |
| 35 | + /// A requirement proven to be false, e.g. Bool: Collection |
| 36 | + ConflictingRequirement, |
| 37 | + /// A redundant requirement, e.g. T == T. |
| 38 | + RedundantRequirement, |
| 39 | + } kind; |
| 40 | + |
| 41 | + /// The invalid requirement. |
| 42 | + Requirement requirement; |
| 43 | + |
| 44 | + SourceLoc loc; |
| 45 | + |
| 46 | +private: |
| 47 | + RequirementError(Kind kind, Requirement requirement, SourceLoc loc) |
| 48 | + : kind(kind), requirement(requirement), loc(loc) {} |
| 49 | + |
| 50 | +public: |
| 51 | + static RequirementError forInvalidTypeRequirement(Type subjectType, |
| 52 | + Type constraint, |
| 53 | + SourceLoc loc) { |
| 54 | + Requirement requirement(RequirementKind::Conformance, subjectType, constraint); |
| 55 | + return {Kind::InvalidTypeRequirement, requirement, loc}; |
| 56 | + } |
| 57 | + |
| 58 | + static RequirementError forConcreteTypeMismatch(Type type1, |
| 59 | + Type type2, |
| 60 | + SourceLoc loc) { |
| 61 | + Requirement requirement(RequirementKind::SameType, type1, type2); |
| 62 | + return {Kind::ConcreteTypeMismatch, requirement, loc}; |
| 63 | + } |
| 64 | + |
| 65 | + static RequirementError forConflictingRequirement(Requirement req, |
| 66 | + SourceLoc loc) { |
| 67 | + return {Kind::ConflictingRequirement, req, loc}; |
| 68 | + } |
| 69 | + |
| 70 | + static RequirementError forRedundantRequirement(Requirement req, |
| 71 | + SourceLoc loc) { |
| 72 | + return {Kind::RedundantRequirement, req, loc}; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +} // end namespace rewriting |
| 77 | + |
| 78 | +} // end namespace swift |
| 79 | + |
| 80 | +#endif |
0 commit comments