Skip to content

Commit 93585a9

Browse files
authored
Merge pull request #41397 from hborla/requirement-machine-early-diagnostics
[RequirementMachine] Diagnose invalid requirements detected before rule construction.
2 parents bdaf537 + e966c6a commit 93585a9

File tree

6 files changed

+443
-51
lines changed

6 files changed

+443
-51
lines changed

lib/AST/RequirementMachine/ConcreteTypeWitness.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ void PropertyMap::inferConditionalRequirements(
527527
return;
528528

529529
SmallVector<Requirement, 2> desugaredRequirements;
530+
// FIXME: Store errors in the rewrite system to be diagnosed
531+
// from the top-level generic signature requests.
532+
SmallVector<RequirementError, 2> errors;
530533

531534
// First, desugar all conditional requirements.
532535
for (auto req : conditionalRequirements) {
@@ -536,7 +539,7 @@ void PropertyMap::inferConditionalRequirements(
536539
llvm::dbgs() << "\n";
537540
}
538541

539-
desugarRequirement(req, desugaredRequirements);
542+
desugarRequirement(req, desugaredRequirements, errors);
540543
}
541544

542545
// Now, convert desugared conditional requirements to rules.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)