Skip to content

Commit 5944d36

Browse files
authored
Merge pull request #27179 from CodaFi/expanding-the-old-utility-belt
Make validateType a utility method
2 parents 5ee5dee + 30f4df3 commit 5944d36

10 files changed

+71
-50
lines changed

lib/Sema/CSGen.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,8 +1382,8 @@ namespace {
13821382
Type resolveTypeReferenceInExpression(TypeLoc &loc) {
13831383
TypeResolutionOptions options(TypeResolverContext::InExpression);
13841384
options |= TypeResolutionFlags::AllowUnboundGenerics;
1385-
bool hadError = CS.TC.validateType(
1386-
loc, TypeResolution::forContextual(CS.DC), options);
1385+
bool hadError = TypeChecker::validateType(
1386+
CS.TC.Context, loc, TypeResolution::forContextual(CS.DC), options);
13871387
return hadError ? Type() : loc.getType();
13881388
}
13891389

@@ -1640,7 +1640,8 @@ namespace {
16401640
for (size_t i = 0, size = specializations.size(); i < size; ++i) {
16411641
TypeResolutionOptions options(TypeResolverContext::InExpression);
16421642
options |= TypeResolutionFlags::AllowUnboundGenerics;
1643-
if (tc.validateType(specializations[i],
1643+
if (TypeChecker::validateType(tc.Context,
1644+
specializations[i],
16441645
TypeResolution::forContextual(CS.DC),
16451646
options))
16461647
return Type();
@@ -2280,9 +2281,10 @@ namespace {
22802281
// of is-patterns applied to an irrefutable pattern.
22812282
pattern = pattern->getSemanticsProvidingPattern();
22822283
while (auto isp = dyn_cast<IsPattern>(pattern)) {
2283-
if (CS.TC.validateType(isp->getCastTypeLoc(),
2284-
TypeResolution::forContextual(CS.DC),
2285-
TypeResolverContext::InExpression)) {
2284+
if (TypeChecker::validateType(CS.TC.Context,
2285+
isp->getCastTypeLoc(),
2286+
TypeResolution::forContextual(CS.DC),
2287+
TypeResolverContext::InExpression)) {
22862288
return false;
22872289
}
22882290

@@ -2618,8 +2620,10 @@ namespace {
26182620
// Validate the resulting type.
26192621
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
26202622
options |= TypeResolutionFlags::AllowUnboundGenerics;
2621-
if (tc.validateType(expr->getCastTypeLoc(),
2622-
TypeResolution::forContextual(CS.DC), options))
2623+
if (TypeChecker::validateType(tc.Context,
2624+
expr->getCastTypeLoc(),
2625+
TypeResolution::forContextual(CS.DC),
2626+
options))
26232627
return nullptr;
26242628

26252629
// Open the type we're casting to.
@@ -2648,9 +2652,10 @@ namespace {
26482652
// Validate the resulting type.
26492653
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
26502654
options |= TypeResolutionFlags::AllowUnboundGenerics;
2651-
if (tc.validateType(expr->getCastTypeLoc(),
2652-
TypeResolution::forContextual(CS.DC),
2653-
options))
2655+
if (TypeChecker::validateType(tc.Context,
2656+
expr->getCastTypeLoc(),
2657+
TypeResolution::forContextual(CS.DC),
2658+
options))
26542659
return nullptr;
26552660

26562661
// Open the type we're casting to.
@@ -2684,8 +2689,10 @@ namespace {
26842689
// Validate the resulting type.
26852690
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
26862691
options |= TypeResolutionFlags::AllowUnboundGenerics;
2687-
if (tc.validateType(expr->getCastTypeLoc(),
2688-
TypeResolution::forContextual(CS.DC), options))
2692+
if (TypeChecker::validateType(tc.Context,
2693+
expr->getCastTypeLoc(),
2694+
TypeResolution::forContextual(CS.DC),
2695+
options))
26892696
return nullptr;
26902697

26912698
// Open the type we're casting to.
@@ -2713,8 +2720,10 @@ namespace {
27132720
auto &tc = CS.getTypeChecker();
27142721
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
27152722
options |= TypeResolutionFlags::AllowUnboundGenerics;
2716-
if (tc.validateType(expr->getCastTypeLoc(),
2717-
TypeResolution::forContextual(CS.DC), options))
2723+
if (TypeChecker::validateType(tc.Context,
2724+
expr->getCastTypeLoc(),
2725+
TypeResolution::forContextual(CS.DC),
2726+
options))
27182727
return nullptr;
27192728

27202729
// Open up the type we're checking.

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ void TypeChecker::diagnoseGenericTypeExportability(const TypeLoc &TL,
266266
GenericTypeFinder(const SourceFile &SF, Callback callback)
267267
: SF(SF), callback(callback) {}
268268

269+
269270
Action visitBoundGenericType(BoundGenericType *ty) override {
270271
ModuleDecl *useModule = SF.getParentModule();
271272
SubstitutionMap subs = ty->getContextSubstitutionMap(useModule,

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,8 +1140,9 @@ namespace {
11401140

11411141
if (auto PlaceholderE = dyn_cast<EditorPlaceholderExpr>(expr)) {
11421142
if (!PlaceholderE->getTypeLoc().isNull()) {
1143-
if (!TC.validateType(PlaceholderE->getTypeLoc(),
1144-
TypeResolution::forContextual(DC), None))
1143+
if (!TypeChecker::validateType(TC.Context, PlaceholderE->getTypeLoc(),
1144+
TypeResolution::forContextual(DC),
1145+
None))
11451146
expr->setType(PlaceholderE->getTypeLoc().getType());
11461147
}
11471148
return finish(true, expr);
@@ -1375,8 +1376,9 @@ bool PreCheckExpression::walkToClosureExprPre(ClosureExpr *closure) {
13751376

13761377
// Validate the result type, if present.
13771378
if (closure->hasExplicitResultType() &&
1378-
TC.validateType(closure->getExplicitResultTypeLoc(), resolution,
1379-
TypeResolverContext::InExpression)) {
1379+
TypeChecker::validateType(TC.Context, closure->getExplicitResultTypeLoc(),
1380+
resolution,
1381+
TypeResolverContext::InExpression)) {
13801382
return false;
13811383
}
13821384

@@ -1392,7 +1394,7 @@ bool PreCheckExpression::walkToClosureExprPre(ClosureExpr *closure) {
13921394
// recurse into.
13931395
assert((closure->getParent() == DC ||
13941396
closure->getParent()->isChildContextOf(DC)) &&
1395-
"Decl context isn't correct");
1397+
"Decl context isn't correct");
13961398
DC = closure;
13971399
return true;
13981400
}
@@ -1987,7 +1989,8 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
19871989

19881990
auto &typeLoc = typeExpr->getTypeLoc();
19891991
bool hadError =
1990-
TC.validateType(typeLoc, TypeResolution::forContextual(DC), options);
1992+
TypeChecker::validateType(TC.Context, typeLoc,
1993+
TypeResolution::forContextual(DC), options);
19911994

19921995
if (hadError)
19931996
return nullptr;

lib/Sema/TypeCheckDecl.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,8 @@ static NominalTypeDecl *resolveSingleNominalTypeDecl(
18361836

18371837
TypeResolutionOptions options = TypeResolverContext::TypeAliasDecl;
18381838
options |= flags;
1839-
if (tc.validateType(typeLoc, TypeResolution::forInterface(DC), options))
1839+
if (TypeChecker::validateType(tc.Context, typeLoc,
1840+
TypeResolution::forInterface(DC), options))
18401841
return nullptr;
18411842

18421843
return typeLoc.getType()->getAnyNominal();
@@ -3477,8 +3478,9 @@ static void validateTypealiasType(TypeChecker &tc, TypeAliasDecl *typeAlias) {
34773478
return;
34783479
}
34793480

3480-
if (tc.validateType(typeAlias->getUnderlyingTypeLoc(),
3481-
TypeResolution::forInterface(typeAlias, &tc), options)) {
3481+
if (TypeChecker::validateType(tc.Context, typeAlias->getUnderlyingTypeLoc(),
3482+
TypeResolution::forInterface(typeAlias, &tc),
3483+
options)) {
34823484
typeAlias->setInvalid();
34833485
typeAlias->getUnderlyingTypeLoc().setInvalidType(tc.Context);
34843486
}
@@ -4202,8 +4204,10 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
42024204
TypeResolverContext::TypeAliasDecl));
42034205
auto &underlyingTL = typealias->getUnderlyingTypeLoc();
42044206
if (underlyingTL.isNull() ||
4205-
validateType(underlyingTL,
4206-
TypeResolution::forStructural(typealias), options)) {
4207+
TypeChecker::validateType(Context,
4208+
underlyingTL,
4209+
TypeResolution::forStructural(typealias),
4210+
options)) {
42074211
typealias->setInvalid();
42084212
underlyingTL.setInvalidType(Context);
42094213
}

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Type TypeChecker::getOrCreateOpaqueResultType(TypeResolution resolution,
129129
TypeLoc constraintTypeLoc(repr->getConstraint());
130130
// Pass along the error type if resolving the repr failed.
131131
bool validationError
132-
= validateType(constraintTypeLoc, resolution, options);
132+
= validateType(Context, constraintTypeLoc, resolution, options);
133133
auto constraintType = constraintTypeLoc.getType();
134134
if (validationError)
135135
return constraintType;
@@ -569,7 +569,7 @@ void TypeChecker::validateGenericFuncOrSubscriptSignature(
569569
resultTyLoc.setType(
570570
getOrCreateOpaqueResultType(resolution, decl, opaqueTy));
571571
} else {
572-
validateType(resultTyLoc, resolution,
572+
validateType(Context, resultTyLoc, resolution,
573573
TypeResolverContext::FunctionResult);
574574
}
575575
}

lib/Sema/TypeCheckPattern.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ static bool validateTypedPattern(TypeChecker &TC,
705705
hadError = true;
706706
}
707707
} else {
708-
hadError = TC.validateType(TL, resolution, options);
708+
hadError = TypeChecker::validateType(TC.Context, TL, resolution, options);
709709
}
710710

711711
if (hadError) {
@@ -765,7 +765,7 @@ static bool validateParameterType(ParamDecl *decl, TypeResolution resolution,
765765
// We might have a null typeLoc if this is a closure parameter list,
766766
// where parameters are allowed to elide their types.
767767
if (!TL.isNull()) {
768-
hadError |= TC.validateType(TL, resolution, options);
768+
hadError |= TypeChecker::validateType(TC.Context, TL, resolution, options);
769769
}
770770

771771
Type Ty = TL.getType();
@@ -1296,7 +1296,7 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
12961296

12971297
// Type-check the type parameter.
12981298
TypeResolutionOptions paramOptions(TypeResolverContext::InExpression);
1299-
if (validateType(IP->getCastTypeLoc(), resolution, paramOptions))
1299+
if (validateType(Context, IP->getCastTypeLoc(), resolution, paramOptions))
13001300
return true;
13011301

13021302
auto castType = IP->getCastTypeLoc().getType();

lib/Sema/TypeCheckPropertyWrapper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ AttachedPropertyWrapperTypeRequest::evaluate(Evaluator &evaluator,
494494
options |= TypeResolutionFlags::AllowUnboundGenerics;
495495

496496
auto &tc = *static_cast<TypeChecker *>(ctx.getLazyResolver());
497-
if (tc.validateType(customAttr->getTypeLoc(), resolution, options))
497+
if (TypeChecker::validateType(tc.Context, customAttr->getTypeLoc(),
498+
resolution, options))
498499
return ErrorType::get(ctx);
499500

500501
return customAttr->getTypeLoc().getType();

lib/Sema/TypeCheckType.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,25 +1715,26 @@ Type TypeChecker::resolveIdentifierType(
17151715
/// Validate whether type associated with @autoclosure attribute is correct,
17161716
/// it supposed to be a function type with no parameters.
17171717
/// \returns true if there was an error, false otherwise.
1718-
static bool validateAutoClosureAttr(TypeChecker &TC, const SourceLoc &loc,
1718+
static bool validateAutoClosureAttr(DiagnosticEngine &Diags, const SourceLoc &loc,
17191719
Type paramType) {
17201720
if (auto *fnType = paramType->getAs<FunctionType>()) {
17211721
if (fnType->getNumParams() != 0) {
1722-
TC.diagnose(loc, diag::autoclosure_function_input_nonunit);
1722+
Diags.diagnose(loc, diag::autoclosure_function_input_nonunit);
17231723
return true;
17241724
}
17251725
// A function type with no parameters.
17261726
return false;
17271727
}
17281728

1729-
TC.diagnose(loc, diag::autoclosure_function_type);
1729+
Diags.diagnose(loc, diag::autoclosure_function_type);
17301730
return true;
17311731
}
17321732

17331733
/// Check whether the type associated with particular source location
17341734
/// has `@autoclosure` attribute, and if so, validate that such use is correct.
17351735
/// \returns true if there was an error, false otherwise.
1736-
static bool validateAutoClosureAttributeUse(TypeChecker &TC, const TypeLoc &loc,
1736+
static bool validateAutoClosureAttributeUse(DiagnosticEngine &Diags,
1737+
const TypeLoc &loc,
17371738
Type type,
17381739
TypeResolutionOptions options) {
17391740
auto *TR = loc.getTypeRepr();
@@ -1745,7 +1746,7 @@ static bool validateAutoClosureAttributeUse(TypeChecker &TC, const TypeLoc &loc,
17451746
if (auto *ATR = dyn_cast<AttributedTypeRepr>(TR)) {
17461747
const auto attrLoc = ATR->getAttrs().getLoc(TAK_autoclosure);
17471748
if (attrLoc.isValid())
1748-
return validateAutoClosureAttr(TC, attrLoc, type);
1749+
return validateAutoClosureAttr(Diags, attrLoc, type);
17491750
}
17501751
}
17511752

@@ -1759,7 +1760,7 @@ static bool validateAutoClosureAttributeUse(TypeChecker &TC, const TypeLoc &loc,
17591760
isValid &= llvm::none_of(
17601761
fnType->getParams(), [&](const FunctionType::Param &param) {
17611762
return param.isAutoClosure() &&
1762-
validateAutoClosureAttr(TC, loc.getLoc(),
1763+
validateAutoClosureAttr(Diags, loc.getLoc(),
17631764
param.getPlainType());
17641765
});
17651766
}
@@ -1768,7 +1769,8 @@ static bool validateAutoClosureAttributeUse(TypeChecker &TC, const TypeLoc &loc,
17681769
return !isValid;
17691770
}
17701771

1771-
bool TypeChecker::validateType(TypeLoc &Loc, TypeResolution resolution,
1772+
bool TypeChecker::validateType(ASTContext &Context, TypeLoc &Loc,
1773+
TypeResolution resolution,
17721774
TypeResolutionOptions options) {
17731775
// If we've already validated this type, don't do so again.
17741776
if (Loc.wasValidated())
@@ -1785,10 +1787,11 @@ bool TypeChecker::validateType(TypeLoc &Loc, TypeResolution resolution,
17851787
// Diagnose types that are illegal in SIL.
17861788
} else if (options.contains(TypeResolutionFlags::SILType)
17871789
&& !type->isLegalSILType()) {
1788-
diagnose(Loc.getLoc(), diag::illegal_sil_type, type);
1790+
Context.Diags.diagnose(Loc.getLoc(), diag::illegal_sil_type, type);
17891791
Loc.setInvalidType(Context);
17901792
return true;
1791-
} else if (validateAutoClosureAttributeUse(*this, Loc, type, options)) {
1793+
} else if (validateAutoClosureAttributeUse(Context.Diags, Loc,
1794+
type, options)) {
17921795
type = ErrorType::get(Context);
17931796
}
17941797
}
@@ -1798,7 +1801,7 @@ bool TypeChecker::validateType(TypeLoc &Loc, TypeResolution resolution,
17981801
const DeclContext *DC = resolution.getDeclContext();
17991802
if (options.isAnyExpr() || DC->getParent()->isLocalContext())
18001803
if (DC->getResilienceExpansion() == ResilienceExpansion::Minimal)
1801-
diagnoseGenericTypeExportability(Loc, DC);
1804+
TypeChecker::diagnoseGenericTypeExportability(Loc, DC);
18021805
}
18031806

18041807
return type->hasError();
@@ -3618,8 +3621,7 @@ Type swift::resolveCustomAttrType(CustomAttr *attr, DeclContext *dc,
36183621
options |= TypeResolutionFlags::AllowUnboundGenerics;
36193622

36203623
ASTContext &ctx = dc->getASTContext();
3621-
auto &tc = *static_cast<TypeChecker *>(ctx.getLazyResolver());
3622-
if (tc.validateType(attr->getTypeLoc(), resolution, options))
3624+
if (TypeChecker::validateType(ctx, attr->getTypeLoc(), resolution, options))
36233625
return Type();
36243626

36253627
// We always require the type to resolve to a nominal type.

lib/Sema/TypeChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ bool swift::performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,
590590
if (!ProduceDiagnostics)
591591
suppression.emplace(Ctx.Diags);
592592
TypeChecker &TC = createTypeChecker(Ctx);
593-
return TC.validateType(T, resolution, options);
593+
return TypeChecker::validateType(TC.Context, T, resolution, options);
594594
}
595595

596596
/// Expose TypeChecker's handling of GenericParamList to SIL parsing.

lib/Sema/TypeChecker.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,9 @@ class TypeChecker final : public LazyResolver {
757757
/// \param options Options that alter type resolution.
758758
///
759759
/// \returns true if type validation failed, or false otherwise.
760-
bool validateType(TypeLoc &Loc, TypeResolution resolution,
761-
TypeResolutionOptions options);
760+
static bool validateType(ASTContext &Ctx, TypeLoc &Loc,
761+
TypeResolution resolution,
762+
TypeResolutionOptions options);
762763

763764
/// Check for unsupported protocol types in the given declaration.
764765
void checkUnsupportedProtocolType(Decl *decl);
@@ -1747,17 +1748,17 @@ class TypeChecker final : public LazyResolver {
17471748
const DeclContext *DC,
17481749
FragileFunctionKind fragileKind);
17491750

1750-
public:
17511751
/// Given that a type is used from a particular context which
17521752
/// exposes it in the interface of the current module, diagnose if its
17531753
/// generic arguments require the use of conformances that cannot reasonably
17541754
/// be shared.
17551755
///
17561756
/// This method \e only checks how generic arguments are used; it is assumed
17571757
/// that the declarations involved have already been checked elsewhere.
1758-
void diagnoseGenericTypeExportability(const TypeLoc &TL,
1759-
const DeclContext *DC);
1758+
static void diagnoseGenericTypeExportability(const TypeLoc &TL,
1759+
const DeclContext *DC);
17601760

1761+
public:
17611762
/// Given that \p DC is within a fragile context for some reason, describe
17621763
/// why.
17631764
///

0 commit comments

Comments
 (0)