24
24
#include " ConstraintGraphScope.h"
25
25
#include " ConstraintLocator.h"
26
26
#include " OverloadChoice.h"
27
- #include " TypeChecker.h"
27
+ #include " SolutionResult.h"
28
+ #include " swift/AST/ASTContext.h"
28
29
#include " swift/AST/ASTNode.h"
29
30
#include " swift/AST/ASTVisitor.h"
30
31
#include " swift/AST/ASTWalker.h"
32
+ #include " swift/AST/AnyFunctionRef.h"
33
+ #include " swift/AST/DiagnosticsSema.h"
31
34
#include " swift/AST/NameLookup.h"
32
35
#include " swift/AST/PropertyWrappers.h"
33
36
#include " swift/AST/Types.h"
34
37
#include " swift/Basic/Debug.h"
35
38
#include " swift/Basic/LLVM.h"
36
39
#include " swift/Basic/OptionSet.h"
40
+ #include " llvm/ADT/MapVector.h"
37
41
#include " llvm/ADT/PointerUnion.h"
38
42
#include " llvm/ADT/STLExtras.h"
39
43
#include " llvm/ADT/SetOperations.h"
49
53
namespace swift {
50
54
51
55
class Expr ;
56
+ class FuncDecl ;
57
+ class BraseStmt ;
58
+ enum class TypeCheckExprFlags ;
52
59
53
60
namespace constraints {
54
61
55
62
class ConstraintGraph ;
56
63
class ConstraintGraphNode ;
57
64
class ConstraintSystem ;
65
+ class SolutionApplicationTarget ;
58
66
59
67
} // end namespace constraints
60
68
69
+ // Forward declare some TypeChecker related functions
70
+ // so they could be made friends of ConstraintSystem.
71
+ namespace TypeChecker {
72
+
73
+ Optional<BraceStmt *> applyFunctionBuilderBodyTransform (FuncDecl *func,
74
+ Type builderType);
75
+
76
+ Optional<constraints::SolutionApplicationTarget>
77
+ typeCheckExpression (constraints::SolutionApplicationTarget &target,
78
+ OptionSet<TypeCheckExprFlags> options);
79
+
80
+ } // end namespace TypeChecker
81
+
61
82
} // end namespace swift
62
83
63
84
// / Allocate memory within the given constraint system.
@@ -66,6 +87,57 @@ void *operator new(size_t bytes, swift::constraints::ConstraintSystem& cs,
66
87
67
88
namespace swift {
68
89
90
+ // / This specifies the purpose of the contextual type, when specified to
91
+ // / typeCheckExpression. This is used for diagnostic generation to produce more
92
+ // / specified error messages when the conversion fails.
93
+ // /
94
+ enum ContextualTypePurpose {
95
+ CTP_Unused, // /< No contextual type is specified.
96
+ CTP_Initialization, // /< Pattern binding initialization.
97
+ CTP_ReturnStmt, // /< Value specified to a 'return' statement.
98
+ CTP_ReturnSingleExpr, // /< Value implicitly returned from a function.
99
+ CTP_YieldByValue, // /< By-value yield operand.
100
+ CTP_YieldByReference, // /< By-reference yield operand.
101
+ CTP_ThrowStmt, // /< Value specified to a 'throw' statement.
102
+ CTP_EnumCaseRawValue, // /< Raw value specified for "case X = 42" in enum.
103
+ CTP_DefaultParameter, // /< Default value in parameter 'foo(a : Int = 42)'.
104
+
105
+ // / Default value in @autoclosure parameter
106
+ // / 'foo(a : @autoclosure () -> Int = 42)'.
107
+ CTP_AutoclosureDefaultParameter,
108
+
109
+ CTP_CalleeResult, // /< Constraint is placed on the result of a callee.
110
+ CTP_CallArgument, // /< Call to function or operator requires type.
111
+ CTP_ClosureResult, // /< Closure result expects a specific type.
112
+ CTP_ArrayElement, // /< ArrayExpr wants elements to have a specific type.
113
+ CTP_DictionaryKey, // /< DictionaryExpr keys should have a specific type.
114
+ CTP_DictionaryValue, // /< DictionaryExpr values should have a specific type.
115
+ CTP_CoerceOperand, // /< CoerceExpr operand coerced to specific type.
116
+ CTP_AssignSource, // /< AssignExpr source operand coerced to result type.
117
+ CTP_SubscriptAssignSource, // /< AssignExpr source operand coerced to subscript
118
+ // /< result type.
119
+ CTP_Condition, // /< Condition expression of various statements e.g.
120
+ // /< `if`, `for`, `while` etc.
121
+ CTP_ForEachStmt, // /< "expression/sequence" associated with 'for-in' loop
122
+ // /< is expected to conform to 'Sequence' protocol.
123
+ CTP_WrappedProperty, // /< Property type expected to match 'wrappedValue' type
124
+ CTP_ComposedPropertyWrapper, // /< Composed wrapper type expected to match
125
+ // /< former 'wrappedValue' type
126
+
127
+ CTP_CannotFail, // /< Conversion can never fail. abort() if it does.
128
+ };
129
+
130
+ // / Specify how we handle the binding of underconstrained (free) type variables
131
+ // / within a solution to a constraint system.
132
+ enum class FreeTypeVariableBinding {
133
+ // / Disallow any binding of such free type variables.
134
+ Disallow,
135
+ // / Allow the free type variables to persist in the solution.
136
+ Allow,
137
+ // / Bind the type variables to UnresolvedType to represent the ambiguity.
138
+ UnresolvedType
139
+ };
140
+
69
141
namespace constraints {
70
142
71
143
// / Describes the algorithm to use for trailing closure matching.
@@ -857,6 +929,14 @@ struct ContextualTypeInfo {
857
929
TypeLoc typeLoc;
858
930
ContextualTypePurpose purpose;
859
931
932
+ ContextualTypeInfo () : typeLoc(TypeLoc()), purpose(CTP_Unused) {}
933
+
934
+ ContextualTypeInfo (Type contextualTy, ContextualTypePurpose purpose)
935
+ : typeLoc(TypeLoc::withoutLoc(contextualTy)), purpose(purpose) {}
936
+
937
+ ContextualTypeInfo (TypeLoc typeLoc, ContextualTypePurpose purpose)
938
+ : typeLoc(typeLoc), purpose(purpose) {}
939
+
860
940
Type getType () const { return typeLoc.getType (); }
861
941
};
862
942
@@ -2671,9 +2751,10 @@ class ConstraintSystem {
2671
2751
friend Optional<BraceStmt *>
2672
2752
swift::TypeChecker::applyFunctionBuilderBodyTransform (FuncDecl *func,
2673
2753
Type builderType);
2754
+
2674
2755
friend Optional<SolutionApplicationTarget>
2675
- swift::TypeChecker::typeCheckExpression (SolutionApplicationTarget &target,
2676
- TypeCheckExprOptions options);
2756
+ swift::TypeChecker::typeCheckExpression (
2757
+ SolutionApplicationTarget &target, OptionSet<TypeCheckExprFlags> options);
2677
2758
2678
2759
// / Emit the fixes computed as part of the solution, returning true if we were
2679
2760
// / able to emit an error message, or false if none of the fixits worked out.
@@ -4912,41 +4993,7 @@ class ConstraintSystem {
4912
4993
llvm::function_ref<bool (Constraint *)> pred);
4913
4994
4914
4995
bool isReadOnlyKeyPathComponent (const AbstractStorageDecl *storage,
4915
- SourceLoc referenceLoc) {
4916
- // See whether key paths can store to this component. (Key paths don't
4917
- // get any special power from being formed in certain contexts, such
4918
- // as the ability to assign to `let`s in initialization contexts, so
4919
- // we pass null for the DC to `isSettable` here.)
4920
- if (!getASTContext ().isSwiftVersionAtLeast (5 )) {
4921
- // As a source-compatibility measure, continue to allow
4922
- // WritableKeyPaths to be formed in the same conditions we did
4923
- // in previous releases even if we should not be able to set
4924
- // the value in this context.
4925
- if (!storage->isSettable (DC)) {
4926
- // A non-settable component makes the key path read-only, unless
4927
- // a reference-writable component shows up later.
4928
- return true ;
4929
- }
4930
- } else if (!storage->isSettable (nullptr ) ||
4931
- !storage->isSetterAccessibleFrom (DC)) {
4932
- // A non-settable component makes the key path read-only, unless
4933
- // a reference-writable component shows up later.
4934
- return true ;
4935
- }
4936
-
4937
- // If the setter is unavailable, then the keypath ought to be read-only
4938
- // in this context.
4939
- if (auto setter = storage->getOpaqueAccessor (AccessorKind::Set)) {
4940
- auto maybeUnavail = TypeChecker::checkDeclarationAvailability (setter,
4941
- referenceLoc,
4942
- DC);
4943
- if (maybeUnavail.hasValue ()) {
4944
- return true ;
4945
- }
4946
- }
4947
-
4948
- return false ;
4949
- }
4996
+ SourceLoc referenceLoc);
4950
4997
4951
4998
public:
4952
4999
// Given a type variable, attempt to find the disjunction of
0 commit comments