Skip to content

Commit 4237bd3

Browse files
authored
Merge pull request #18546 from davezarzycki/more_type_resolution_clean_up
2 parents 0f2d2f4 + 6fc21fe commit 4237bd3

14 files changed

+402
-292
lines changed

lib/Sema/CSGen.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,8 +1417,8 @@ namespace {
14171417
}
14181418

14191419
Type resolveTypeReferenceInExpression(TypeRepr *rep) {
1420-
TypeResolutionOptions options = TypeResolutionFlags::AllowUnboundGenerics;
1421-
options |= TypeResolutionFlags::InExpression;
1420+
TypeResolutionOptions options(TypeResolverContext::InExpression);
1421+
options |= TypeResolutionFlags::AllowUnboundGenerics;
14221422
return CS.TC.resolveType(rep, CS.DC, options);
14231423
}
14241424

@@ -1634,7 +1634,7 @@ namespace {
16341634
// open type.
16351635
auto locator = CS.getConstraintLocator(expr);
16361636
for (size_t i = 0, size = specializations.size(); i < size; ++i) {
1637-
TypeResolutionOptions options = TypeResolutionFlags::InExpression;
1637+
TypeResolutionOptions options(TypeResolverContext::InExpression);
16381638
options |= TypeResolutionFlags::AllowUnboundGenerics;
16391639
if (tc.validateType(specializations[i], CS.DC, options))
16401640
return Type();
@@ -2260,8 +2260,9 @@ namespace {
22602260
pattern = pattern->getSemanticsProvidingPattern();
22612261
while (auto isp = dyn_cast<IsPattern>(pattern)) {
22622262
if (CS.TC.validateType(isp->getCastTypeLoc(), CS.DC,
2263-
TypeResolutionFlags::InExpression))
2263+
TypeResolverContext::InExpression)) {
22642264
return false;
2265+
}
22652266

22662267
if (!isp->hasSubPattern()) {
22672268
pattern = nullptr;
@@ -2278,9 +2279,10 @@ namespace {
22782279
// Pull the top-level pattern back out.
22792280
pattern = clause->getErrorPattern();
22802281
Type exnType = CS.TC.getExceptionType(CS.DC, clause->getCatchLoc());
2281-
if (!exnType ||
2282-
CS.TC.coercePatternToType(pattern, CS.DC, exnType,
2283-
TypeResolutionFlags::InExpression)) {
2282+
if (!exnType)
2283+
return false;
2284+
if (CS.TC.coercePatternToType(pattern, CS.DC, exnType,
2285+
TypeResolverContext::InExpression)) {
22842286
return false;
22852287
}
22862288

@@ -2593,9 +2595,8 @@ namespace {
25932595
return nullptr;
25942596

25952597
// Validate the resulting type.
2596-
TypeResolutionOptions options = TypeResolutionFlags::AllowUnboundGenerics;
2597-
options |= TypeResolutionFlags::InExpression;
2598-
options |= TypeResolutionFlags::InCastOrCoercionExpression;
2598+
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
2599+
options |= TypeResolutionFlags::AllowUnboundGenerics;
25992600
if (tc.validateType(expr->getCastTypeLoc(), CS.DC, options))
26002601
return nullptr;
26012602

@@ -2623,9 +2624,8 @@ namespace {
26232624
auto &tc = CS.getTypeChecker();
26242625

26252626
// Validate the resulting type.
2626-
TypeResolutionOptions options = TypeResolutionFlags::AllowUnboundGenerics;
2627-
options |= TypeResolutionFlags::InExpression;
2628-
options |= TypeResolutionFlags::InCastOrCoercionExpression;
2627+
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
2628+
options |= TypeResolutionFlags::AllowUnboundGenerics;
26292629
if (tc.validateType(expr->getCastTypeLoc(), CS.DC, options))
26302630
return nullptr;
26312631

@@ -2658,9 +2658,8 @@ namespace {
26582658
return nullptr;
26592659

26602660
// Validate the resulting type.
2661-
TypeResolutionOptions options = TypeResolutionFlags::AllowUnboundGenerics;
2662-
options |= TypeResolutionFlags::InExpression;
2663-
options |= TypeResolutionFlags::InCastOrCoercionExpression;
2661+
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
2662+
options |= TypeResolutionFlags::AllowUnboundGenerics;
26642663
if (tc.validateType(expr->getCastTypeLoc(), CS.DC, options))
26652664
return nullptr;
26662665

@@ -2687,8 +2686,8 @@ namespace {
26872686
Type visitIsExpr(IsExpr *expr) {
26882687
// Validate the type.
26892688
auto &tc = CS.getTypeChecker();
2690-
TypeResolutionOptions options = TypeResolutionFlags::AllowUnboundGenerics;
2691-
options |= TypeResolutionFlags::InExpression;
2689+
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
2690+
options |= TypeResolutionFlags::AllowUnboundGenerics;
26922691
if (tc.validateType(expr->getCastTypeLoc(), CS.DC, options))
26932692
return nullptr;
26942693

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,8 +1194,7 @@ void ConstraintSystem::shrink(Expr *expr) {
11941194
// instead of cloning representative.
11951195
auto coercionRepr = typeRepr->clone(CS.getASTContext());
11961196
// Let's try to resolve coercion type from cloned representative.
1197-
auto coercionType = CS.TC.resolveType(coercionRepr, CS.DC,
1198-
TypeResolutionOptions());
1197+
auto coercionType = CS.TC.resolveType(coercionRepr, CS.DC, None);
11991198

12001199
// Looks like coercion type is invalid, let's skip this sub-tree.
12011200
if (coercionType->hasError())

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,8 +1846,7 @@ static void maybeAddAccessorsToBehaviorStorage(TypeChecker &TC, VarDecl *var) {
18461846
};
18471847

18481848
// Try to resolve the behavior to a protocol.
1849-
auto behaviorType = TC.resolveType(behavior->ProtocolName, dc,
1850-
TypeResolutionOptions());
1849+
auto behaviorType = TC.resolveType(behavior->ProtocolName, dc, None);
18511850
if (!behaviorType) {
18521851
return makeBehaviorAccessors();
18531852
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
981981
if (auto typeDecl = dyn_cast<TypeDecl>(value)) {
982982
// Resolve the reference to this type declaration in our current context.
983983
auto type = TC.resolveTypeInContext(typeDecl, nullptr, useDC,
984-
TypeResolutionFlags::InExpression,
984+
TypeResolverContext::InExpression,
985985
/*isSpecialized=*/false);
986986

987987
// Open the type.

lib/Sema/TypeCheckAttr.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,10 +1773,6 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
17731773
SmallVector<Requirement, 4> convertedRequirements;
17741774
SmallVector<RequirementRepr, 4> resolvedRequirements;
17751775

1776-
// Add all requirements from the "where" clause to the old signature
1777-
// to check if there are any inconsistencies.
1778-
auto options = TypeResolutionOptions();
1779-
17801776
// Set of generic parameters being constrained. It is used to
17811777
// determine if a full specialization misses requirements for
17821778
// some of the generic parameters.
@@ -1785,8 +1781,8 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
17851781
// Go over the set of requirements and resolve their types.
17861782
for (auto &req : trailingWhereClause->getRequirements()) {
17871783
if (req.getKind() == RequirementReprKind::SameType) {
1788-
auto firstType = TC.resolveType(req.getFirstTypeRepr(), FD, options);
1789-
auto secondType = TC.resolveType(req.getSecondTypeRepr(), FD, options);
1784+
auto firstType = TC.resolveType(req.getFirstTypeRepr(), FD, None);
1785+
auto secondType = TC.resolveType(req.getSecondTypeRepr(), FD, None);
17901786
Type interfaceFirstType;
17911787
Type interfaceSecondType;
17921788

@@ -1836,7 +1832,7 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
18361832
}
18371833

18381834
if (req.getKind() == RequirementReprKind::LayoutConstraint) {
1839-
auto subjectType = TC.resolveType(req.getSubjectRepr(), FD, options);
1835+
auto subjectType = TC.resolveType(req.getSubjectRepr(), FD, None);
18401836
Type interfaceSubjectType;
18411837

18421838
// Map types to their interface types.
@@ -1872,9 +1868,9 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
18721868
}
18731869

18741870
if (req.getKind() == RequirementReprKind::TypeConstraint) {
1875-
auto subjectType = TC.resolveType(req.getSubjectRepr(), FD, options);
1871+
auto subjectType = TC.resolveType(req.getSubjectRepr(), FD, None);
18761872
auto constraint = TC.resolveType(
1877-
req.getConstraintLoc().getTypeRepr(), FD, options);
1873+
req.getConstraintLoc().getTypeRepr(), FD, None);
18781874

18791875
Type interfaceSubjectType;
18801876

@@ -2038,11 +2034,11 @@ void AttributeChecker::visitDiscardableResultAttr(DiscardableResultAttr *attr) {
20382034

20392035
void AttributeChecker::visitImplementsAttr(ImplementsAttr *attr) {
20402036
TypeLoc &ProtoTypeLoc = attr->getProtocolType();
2041-
auto options = TypeResolutionFlags::AllowUnboundGenerics;
2037+
TypeResolutionOptions options = None;
2038+
options |= TypeResolutionFlags::AllowUnboundGenerics;
20422039

20432040
DeclContext *DC = D->getDeclContext();
2044-
Type T = TC.resolveType(ProtoTypeLoc.getTypeRepr(),
2045-
DC, options);
2041+
Type T = TC.resolveType(ProtoTypeLoc.getTypeRepr(), DC, options);
20462042
ProtoTypeLoc.setType(T);
20472043

20482044
// Definite error-types were already diagnosed in resolveType.

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,11 +1244,9 @@ bool PreCheckExpression::walkToClosureExprPre(ClosureExpr *closure) {
12441244
auto *PL = closure->getParameters();
12451245

12461246
// Validate the parameters.
1247-
TypeResolutionOptions options;
1247+
TypeResolutionOptions options(TypeResolverContext::ClosureExpr);
12481248
options |= TypeResolutionFlags::AllowUnspecifiedTypes;
12491249
options |= TypeResolutionFlags::AllowUnboundGenerics;
1250-
options |= TypeResolutionFlags::InExpression;
1251-
12521250
bool hadParameterError = false;
12531251

12541252
GenericTypeToArchetypeResolver resolver(closure);
@@ -1264,7 +1262,7 @@ bool PreCheckExpression::walkToClosureExprPre(ClosureExpr *closure) {
12641262
// Validate the result type, if present.
12651263
if (closure->hasExplicitResultType() &&
12661264
TC.validateType(closure->getExplicitResultTypeLoc(), DC,
1267-
TypeResolutionFlags::InExpression, &resolver)) {
1265+
TypeResolverContext::InExpression, &resolver)) {
12681266
return false;
12691267
}
12701268

@@ -1346,8 +1344,8 @@ TypeExpr *PreCheckExpression::simplifyNestedTypeExpr(UnresolvedDotExpr *UDE) {
13461344
// Resolve the TypeRepr to get the base type for the lookup.
13471345
// Disable availability diagnostics here, because the final
13481346
// TypeRepr will be resolved again when generating constraints.
1349-
TypeResolutionOptions options = TypeResolutionFlags::AllowUnboundGenerics;
1350-
options |= TypeResolutionFlags::InExpression;
1347+
TypeResolutionOptions options(TypeResolverContext::InExpression);
1348+
options |= TypeResolutionFlags::AllowUnboundGenerics;
13511349
options |= TypeResolutionFlags::AllowUnavailable;
13521350
auto BaseTy = TC.resolveType(InnerTypeRepr, DC, options);
13531351

@@ -1863,9 +1861,8 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
18631861
if (typeExpr->getTypeLoc().wasValidated()) {
18641862
type = typeExpr->getTypeLoc().getType();
18651863
} else if (auto *rep = typeExpr->getTypeRepr()) {
1866-
TypeResolutionOptions options;
1864+
TypeResolutionOptions options(TypeResolverContext::InExpression);
18671865
options |= TypeResolutionFlags::AllowUnboundGenerics;
1868-
options |= TypeResolutionFlags::InExpression;
18691866
type = TC.resolveType(rep, DC, options);
18701867
typeExpr->getTypeLoc().setType(type);
18711868
}
@@ -2422,11 +2419,11 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
24222419
assert(!listener.isInOut());
24232420

24242421
if (resultTy) {
2425-
TypeResolutionOptions options = TypeResolutionFlags::OverrideType;
2426-
options |= TypeResolutionFlags::InExpression;
2427-
if (isa<EditorPlaceholderExpr>(initializer->getSemanticsProvidingExpr())) {
2428-
options |= TypeResolutionFlags::EditorPlaceholder;
2429-
}
2422+
TypeResolutionOptions options =
2423+
isa<EditorPlaceholderExpr>(initializer->getSemanticsProvidingExpr())
2424+
? TypeResolverContext::EditorPlaceholderExpr
2425+
: TypeResolverContext::InExpression;
2426+
options |= TypeResolutionFlags::OverrideType;
24302427

24312428
// FIXME: initTy should be the same as resultTy; now that typeCheckExpression()
24322429
// returns a Type and not bool, we should be able to simplify the listener
@@ -2652,9 +2649,8 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
26522649

26532650
// Apply the solution to the iteration pattern as well.
26542651
Pattern *pattern = Stmt->getPattern();
2655-
TypeResolutionOptions options = TypeResolutionFlags::OverrideType;
2656-
options |= TypeResolutionFlags::EnumerationVariable;
2657-
options |= TypeResolutionFlags::InExpression;
2652+
TypeResolutionOptions options(TypeResolverContext::ForEachStmt);
2653+
options |= TypeResolutionFlags::OverrideType;
26582654
if (tc.coercePatternToType(pattern, cs.DC, InitType, options)) {
26592655
return nullptr;
26602656
}
@@ -2823,7 +2819,7 @@ bool TypeChecker::typeCheckStmtCondition(StmtCondition &cond, DeclContext *dc,
28232819

28242820
// Check the pattern, it allows unspecified types because the pattern can
28252821
// provide type information.
2826-
TypeResolutionOptions options = TypeResolutionFlags::InExpression;
2822+
TypeResolutionOptions options(TypeResolverContext::InExpression);
28272823
options |= TypeResolutionFlags::AllowUnspecifiedTypes;
28282824
options |= TypeResolutionFlags::AllowUnboundGenerics;
28292825
if (typeCheckPattern(pattern, dc, options)) {

lib/Sema/TypeCheckDecl.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,7 @@ void TypeChecker::resolveTrailingWhereClause(ProtocolDecl *proto) {
214214

215215
void TypeChecker::validateWhereClauses(ProtocolDecl *protocol,
216216
GenericTypeResolver *resolver) {
217-
TypeResolutionOptions options;
218-
219-
options |= TypeResolutionFlags::ProtocolWhereClause;
217+
TypeResolutionOptions options(TypeResolverContext::ProtocolWhereClause);
220218

221219
if (auto whereClause = protocol->getTrailingWhereClause()) {
222220
revertGenericRequirements(whereClause->getRequirements());
@@ -242,31 +240,29 @@ void TypeChecker::validateWhereClauses(ProtocolDecl *protocol,
242240
/// to which this type declaration conforms.
243241
void TypeChecker::checkInheritanceClause(Decl *decl,
244242
GenericTypeResolver *resolver) {
245-
TypeResolutionOptions options;
243+
TypeResolutionOptions options = None;
246244
DeclContext *DC;
247245
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
248246
DC = nominal;
249-
options |= TypeResolutionFlags::GenericSignature;
250-
options |= TypeResolutionFlags::InheritanceClause;
247+
options = TypeResolutionOptions(TypeResolverContext::GenericSignature);
251248
options |= TypeResolutionFlags::AllowUnavailableProtocol;
252249
} else if (auto ext = dyn_cast<ExtensionDecl>(decl)) {
253250
DC = ext;
254-
options |= TypeResolutionFlags::GenericSignature;
255-
options |= TypeResolutionFlags::InheritanceClause;
251+
options = TypeResolutionOptions(TypeResolverContext::GenericSignature);
256252
options |= TypeResolutionFlags::AllowUnavailableProtocol;
257253
} else if (isa<GenericTypeParamDecl>(decl)) {
258254
// For generic parameters, we want name lookup to look at just the
259255
// signature of the enclosing entity.
260256
DC = decl->getDeclContext();
261257
if (auto nominal = dyn_cast<NominalTypeDecl>(DC)) {
262258
DC = nominal;
263-
options |= TypeResolutionFlags::GenericSignature;
259+
options = TypeResolutionOptions(TypeResolverContext::GenericSignature);
264260
} else if (auto ext = dyn_cast<ExtensionDecl>(DC)) {
265261
DC = ext;
266-
options |= TypeResolutionFlags::GenericSignature;
262+
options = TypeResolutionOptions(TypeResolverContext::GenericSignature);
267263
} else if (auto func = dyn_cast<AbstractFunctionDecl>(DC)) {
268264
DC = func;
269-
options |= TypeResolutionFlags::GenericSignature;
265+
options = TypeResolutionOptions(TypeResolverContext::GenericSignature);
270266
} else if (!DC->isModuleScopeContext()) {
271267
// Skip the generic parameter's context entirely.
272268
DC = DC->getParent();
@@ -1036,9 +1032,7 @@ static void validatePatternBindingEntry(TypeChecker &tc,
10361032
// In particular, it's /not/ correct to check the PBD's DeclContext because
10371033
// top-level variables in a script file are accessible from other files,
10381034
// even though the PBD is inside a TopLevelCodeDecl.
1039-
TypeResolutionOptions options = TypeResolutionFlags::PatternBindingEntry;
1040-
options |= TypeResolutionFlags::InExpression;
1041-
options |= TypeResolutionFlags::Direct;
1035+
TypeResolutionOptions options(TypeResolverContext::PatternBindingDecl);
10421036

10431037
if (binding->getInit(entryNumber)) {
10441038
// If we have an initializer, we can also have unknown types.
@@ -3418,7 +3412,7 @@ void TypeChecker::typeCheckDecl(Decl *D) {
34183412

34193413
/// Validate the underlying type of the given typealias.
34203414
static void validateTypealiasType(TypeChecker &tc, TypeAliasDecl *typeAlias) {
3421-
TypeResolutionOptions options = TypeResolutionFlags::TypeAliasUnderlyingType;
3415+
TypeResolutionOptions options(TypeResolverContext::TypeAliasDecl);
34223416
if (!typeAlias->getDeclContext()->isCascadingContextForLookup(
34233417
/*functionsAreNonCascading*/true)) {
34243418
options |= TypeResolutionFlags::KnownNonCascadingDependency;
@@ -4376,7 +4370,7 @@ void TypeChecker::validateDecl(ValueDecl *D) {
43764370
CompleteGenericTypeResolver resolver(*this, ED->getGenericSignature());
43774371

43784372
typeCheckParameterList(PL, EED->getParentEnum(),
4379-
TypeResolutionFlags::EnumCase, resolver);
4373+
TypeResolverContext::EnumElementDecl, resolver);
43804374
checkDefaultArguments(PL, EED);
43814375
}
43824376

@@ -4482,8 +4476,7 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
44824476
(void) typealias->getFormalAccess();
44834477

44844478
ProtocolRequirementTypeResolver resolver;
4485-
TypeResolutionOptions options =
4486-
TypeResolutionFlags::TypeAliasUnderlyingType;
4479+
TypeResolutionOptions options(TypeResolverContext::TypeAliasDecl);
44874480
if (validateType(typealias->getUnderlyingTypeLoc(),
44884481
typealias, options, &resolver)) {
44894482
typealias->setInvalid();

0 commit comments

Comments
 (0)