2424#include " ConstraintGraphScope.h"
2525#include " ConstraintLocator.h"
2626#include " OverloadChoice.h"
27- #include " TypeChecker.h"
27+ #include " SolutionResult.h"
28+ #include " swift/AST/ASTContext.h"
2829#include " swift/AST/ASTNode.h"
2930#include " swift/AST/ASTVisitor.h"
3031#include " swift/AST/ASTWalker.h"
32+ #include " swift/AST/AnyFunctionRef.h"
33+ #include " swift/AST/DiagnosticsSema.h"
3134#include " swift/AST/NameLookup.h"
3235#include " swift/AST/PropertyWrappers.h"
3336#include " swift/AST/Types.h"
3437#include " swift/Basic/Debug.h"
3538#include " swift/Basic/LLVM.h"
3639#include " swift/Basic/OptionSet.h"
40+ #include " llvm/ADT/MapVector.h"
3741#include " llvm/ADT/PointerUnion.h"
3842#include " llvm/ADT/STLExtras.h"
3943#include " llvm/ADT/SetOperations.h"
4953namespace swift {
5054
5155class Expr ;
56+ class FuncDecl ;
57+ class BraseStmt ;
58+ enum class TypeCheckExprFlags ;
5259
5360namespace constraints {
5461
5562class ConstraintGraph ;
5663class ConstraintGraphNode ;
5764class ConstraintSystem ;
65+ class SolutionApplicationTarget ;
5866
5967} // end namespace constraints
6068
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+
6182} // end namespace swift
6283
6384// / Allocate memory within the given constraint system.
@@ -66,6 +87,57 @@ void *operator new(size_t bytes, swift::constraints::ConstraintSystem& cs,
6687
6788namespace swift {
6889
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+
69141namespace constraints {
70142
71143// / Describes the algorithm to use for trailing closure matching.
@@ -857,6 +929,14 @@ struct ContextualTypeInfo {
857929 TypeLoc typeLoc;
858930 ContextualTypePurpose purpose;
859931
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+
860940 Type getType () const { return typeLoc.getType (); }
861941};
862942
@@ -2671,9 +2751,10 @@ class ConstraintSystem {
26712751 friend Optional<BraceStmt *>
26722752 swift::TypeChecker::applyFunctionBuilderBodyTransform (FuncDecl *func,
26732753 Type builderType);
2754+
26742755 friend Optional<SolutionApplicationTarget>
2675- swift::TypeChecker::typeCheckExpression (SolutionApplicationTarget &target,
2676- TypeCheckExprOptions options);
2756+ swift::TypeChecker::typeCheckExpression (
2757+ SolutionApplicationTarget &target, OptionSet<TypeCheckExprFlags> options);
26772758
26782759 // / Emit the fixes computed as part of the solution, returning true if we were
26792760 // / able to emit an error message, or false if none of the fixits worked out.
@@ -4912,41 +4993,7 @@ class ConstraintSystem {
49124993 llvm::function_ref<bool (Constraint *)> pred);
49134994
49144995 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);
49504997
49514998public:
49524999 // Given a type variable, attempt to find the disjunction of
0 commit comments