Skip to content

Commit 9fa03cd

Browse files
committed
[Type checker] Eliminate TypeCheckExprFlags::ConvertTypeIsOnlyAHint.
This flag is recoverable from the contextual type purpose; don’t duplicate the logic.
1 parent e056d46 commit 9fa03cd

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

lib/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ using OpenedTypeMap =
789789

790790
/// Describes contextual type information about a particular expression
791791
/// within a constraint system.
792-
struct ContextualTypeInfo {
792+
struct ContextualTypeInfo {
793793
TypeLoc typeLoc;
794794
ContextualTypePurpose purpose;
795795
bool isOpaqueReturnType = false;

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,40 @@ bool GenericRequirementsCheckListener::diagnoseUnsatisfiedRequirement(
20582058
return false;
20592059
}
20602060

2061+
/// Whether the contextual type provided for the given purpose is only a
2062+
/// hint, and not a requirement.
2063+
static bool contextualTypeIsOnlyAHint(ContextualTypePurpose ctp,
2064+
TypeCheckExprOptions options) {
2065+
switch (ctp) {
2066+
case CTP_Initialization:
2067+
return !options.contains(
2068+
TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType);
2069+
case CTP_ForEachStmt:
2070+
return true;
2071+
case CTP_Unused:
2072+
case CTP_ReturnStmt:
2073+
case CTP_ReturnSingleExpr:
2074+
case CTP_YieldByValue:
2075+
case CTP_YieldByReference:
2076+
case CTP_ThrowStmt:
2077+
case CTP_EnumCaseRawValue:
2078+
case CTP_DefaultParameter:
2079+
case CTP_AutoclosureDefaultParameter:
2080+
case CTP_CalleeResult:
2081+
case CTP_CallArgument:
2082+
case CTP_ClosureResult:
2083+
case CTP_ArrayElement:
2084+
case CTP_DictionaryKey:
2085+
case CTP_DictionaryValue:
2086+
case CTP_CoerceOperand:
2087+
case CTP_AssignSource:
2088+
case CTP_SubscriptAssignSource:
2089+
case CTP_Condition:
2090+
case CTP_CannotFail:
2091+
return false;
2092+
}
2093+
}
2094+
20612095
#pragma mark High-level entry points
20622096
Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
20632097
TypeLoc convertType,
@@ -2128,7 +2162,7 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
21282162

21292163
// If the convertType is *only* provided for that hint, then null it out so
21302164
// that we don't later treat it as an actual conversion constraint.
2131-
if (options.contains(TypeCheckExprFlags::ConvertTypeIsOnlyAHint))
2165+
if (contextualTypeIsOnlyAHint(convertTypePurpose, options))
21322166
convertType = TypeLoc();
21332167

21342168
// If the client can handle unresolved type variables, leave them in the
@@ -2655,7 +2689,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
26552689

26562690
TypeLoc contextualType;
26572691
auto contextualPurpose = CTP_Unused;
2658-
TypeCheckExprOptions flags = TypeCheckExprFlags::ConvertTypeIsOnlyAHint;
2692+
TypeCheckExprOptions flags = None;
26592693

26602694
// Set the contextual purpose even if the pattern doesn't have a type so
26612695
// if there's an error we can use that information to inform diagnostics.
@@ -2674,7 +2708,6 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
26742708
// opaque type.
26752709
if (auto opaqueType = patternType->getAs<OpaqueTypeArchetypeType>()){
26762710
flags |= TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType;
2677-
flags -= TypeCheckExprFlags::ConvertTypeIsOnlyAHint;
26782711
}
26792712

26802713
// Only provide a TypeLoc if it makes sense to allow diagnostics.
@@ -3042,7 +3075,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
30423075
// Type-check the for-each loop sequence and element pattern.
30433076
auto resultTy = TypeChecker::typeCheckExpression(
30443077
seq, dc, TypeLoc::withoutLoc(sequenceProto->getDeclaredType()),
3045-
CTP_ForEachStmt, TypeCheckExprFlags::ConvertTypeIsOnlyAHint, &listener);
3078+
CTP_ForEachStmt, None, &listener);
30463079
if (!resultTy)
30473080
return true;
30483081
return false;

lib/Sema/TypeChecker.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,6 @@ enum class TypeCheckExprFlags {
174174
/// left in-tact.
175175
AllowUnresolvedTypeVariables = 0x08,
176176

177-
/// If set, the 'convertType' specified to typeCheckExpression should not
178-
/// produce a conversion constraint, but it should be used to guide the
179-
/// solution in terms of performance optimizations of the solver, and in terms
180-
/// of guiding diagnostics.
181-
ConvertTypeIsOnlyAHint = 0x10,
182-
183177
/// If set, this expression isn't embedded in a larger expression or
184178
/// statement. This should only be used for syntactic restrictions, and should
185179
/// not affect type checking itself.
@@ -821,11 +815,9 @@ class TypeChecker final {
821815
/// to be possible.
822816
///
823817
/// \param convertType The type that the expression is being converted to,
824-
/// or null if the expression is standalone. If the 'ConvertTypeIsOnlyAHint'
825-
/// option is specified, then this is only a hint, it doesn't produce a full
826-
/// conversion constraint. The location information is only used for
827-
/// diagnostics should the conversion fail; it is safe to pass a TypeLoc
828-
/// without location information.
818+
/// or null if the expression is standalone. The location information is
819+
/// only used for diagnostics should the conversion fail; it is safe to pass
820+
/// a TypeLoc without location information.
829821
///
830822
/// \param options Options that control how type checking is performed.
831823
///

0 commit comments

Comments
 (0)