Skip to content

Commit 8280606

Browse files
authored
Merge pull request #31458 from CodaFi/incidental-main-motion
Define ResolveTypeRequest
2 parents ea526c6 + b68540f commit 8280606

21 files changed

+135
-91
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ WARNING(warn_property_wrapper_module_scope,none,
152152
"wrapper %0; please qualify the reference with %1",
153153
(DeclNameRef, Identifier))
154154

155+
NOTE(circular_type_resolution_note,none,
156+
"while resolving type %0", (TypeRepr *))
157+
155158
//------------------------------------------------------------------------------
156159
// MARK: Cross-import overlay loading diagnostics
157160
//------------------------------------------------------------------------------

include/swift/AST/TypeCheckRequests.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class TrailingWhereClause;
5050
class TypeAliasDecl;
5151
class TypeLoc;
5252
class Witness;
53+
class TypeResolution;
5354
struct TypeWitnessAndDecl;
5455
class ValueDecl;
5556
enum class OpaqueReadOwnership: uint8_t;
@@ -2406,6 +2407,28 @@ class HasImplementationOnlyImportsRequest
24062407
bool isCached() const { return true; }
24072408
};
24082409

2410+
class ResolveTypeRequest
2411+
: public SimpleRequest<ResolveTypeRequest,
2412+
Type(TypeResolution *, TypeRepr *),
2413+
RequestFlags::Uncached> {
2414+
public:
2415+
using SimpleRequest::SimpleRequest;
2416+
2417+
public:
2418+
// Cycle handling.
2419+
void noteCycleStep(DiagnosticEngine &diags) const;
2420+
2421+
private:
2422+
friend SimpleRequest;
2423+
2424+
// Evaluation.
2425+
Type evaluate(Evaluator &evaluator, TypeResolution *resolution,
2426+
TypeRepr *repr) const;
2427+
};
2428+
2429+
void simple_display(llvm::raw_ostream &out, const TypeResolution *resolution);
2430+
SourceLoc extractNearestSourceLoc(const TypeRepr *repr);
2431+
24092432
// Allow AnyValue to compare two Type values, even though Type doesn't
24102433
// support ==.
24112434
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ SWIFT_REQUEST(TypeChecker, ResolveImplicitMemberRequest,
226226
SWIFT_REQUEST(TypeChecker, ResolveTypeEraserTypeRequest,
227227
Type(ProtocolDecl *, TypeEraserAttr *),
228228
SeparatelyCached, NoLocationInfo)
229+
SWIFT_REQUEST(TypeChecker, ResolveTypeRequest,
230+
Type (TypeResolution *, TypeRepr *), Uncached, NoLocationInfo)
229231
SWIFT_REQUEST(TypeChecker, SPIGroupsRequest,
230232
llvm::ArrayRef<Identifier>(Decl *),
231233
Cached, NoLocationInfo)

lib/AST/TypeCheckRequests.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,3 +1501,23 @@ void swift::simple_display(llvm::raw_ostream &out,
15011501
out << "implicit import of ";
15021502
simple_display(out, import.Module);
15031503
}
1504+
1505+
//----------------------------------------------------------------------------//
1506+
// ResolveTypeRequest computation.
1507+
//----------------------------------------------------------------------------//
1508+
1509+
void ResolveTypeRequest::noteCycleStep(DiagnosticEngine &diags) const {
1510+
auto *repr = std::get<1>(getStorage());
1511+
diags.diagnose(repr->getLoc(), diag::circular_type_resolution_note, repr);
1512+
}
1513+
1514+
void swift::simple_display(llvm::raw_ostream &out,
1515+
const TypeResolution *resolution) {
1516+
out << "while resolving type ";
1517+
}
1518+
1519+
SourceLoc swift::extractNearestSourceLoc(const TypeRepr *repr) {
1520+
if (!repr)
1521+
return SourceLoc();
1522+
return repr->getLoc();
1523+
}

lib/Sema/CSGen.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/ParameterList.h"
2424
#include "swift/AST/PrettyStackTrace.h"
2525
#include "swift/AST/SubstitutionMap.h"
26+
#include "swift/AST/TypeCheckRequests.h"
2627
#include "swift/Sema/IDETypeChecking.h"
2728
#include "swift/Subsystems.h"
2829
#include "llvm/ADT/APInt.h"
@@ -1453,8 +1454,7 @@ namespace {
14531454
TypeResolutionOptions options(TypeResolverContext::InExpression);
14541455
options |= TypeResolutionFlags::AllowUnboundGenerics;
14551456
bool hadError = TypeChecker::validateType(
1456-
CS.getASTContext(), loc,
1457-
TypeResolution::forContextual(CS.DC, options));
1457+
loc, TypeResolution::forContextual(CS.DC, options));
14581458
return hadError ? Type() : loc.getType();
14591459
}
14601460

@@ -1714,14 +1714,12 @@ namespace {
17141714
for (size_t i = 0, size = specializations.size(); i < size; ++i) {
17151715
TypeResolutionOptions options(TypeResolverContext::InExpression);
17161716
options |= TypeResolutionFlags::AllowUnboundGenerics;
1717-
auto tyLoc = TypeLoc{specializations[i]};
1718-
if (TypeChecker::validateType(
1719-
CS.getASTContext(), tyLoc,
1720-
TypeResolution::forContextual(CS.DC, options)))
1717+
auto result = TypeResolution::forContextual(CS.DC, options)
1718+
.resolveType(specializations[i]);
1719+
if (result->hasError())
17211720
return Type();
17221721

1723-
CS.addConstraint(ConstraintKind::Bind,
1724-
typeVars[i], tyLoc.getType(),
1722+
CS.addConstraint(ConstraintKind::Bind, typeVars[i], result,
17251723
locator);
17261724
}
17271725

@@ -2660,7 +2658,7 @@ namespace {
26602658
pattern = pattern->getSemanticsProvidingPattern();
26612659
while (auto isp = dyn_cast<IsPattern>(pattern)) {
26622660
if (TypeChecker::validateType(
2663-
CS.getASTContext(), isp->getCastTypeLoc(),
2661+
isp->getCastTypeLoc(),
26642662
TypeResolution::forContextual(
26652663
CS.DC, TypeResolverContext::InExpression))) {
26662664
return false;
@@ -2986,7 +2984,7 @@ namespace {
29862984
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
29872985
options |= TypeResolutionFlags::AllowUnboundGenerics;
29882986
if (TypeChecker::validateType(
2989-
CS.getASTContext(), expr->getCastTypeLoc(),
2987+
expr->getCastTypeLoc(),
29902988
TypeResolution::forContextual(CS.DC, options)))
29912989
return nullptr;
29922990

@@ -3015,7 +3013,7 @@ namespace {
30153013
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
30163014
options |= TypeResolutionFlags::AllowUnboundGenerics;
30173015
if (TypeChecker::validateType(
3018-
CS.getASTContext(), expr->getCastTypeLoc(),
3016+
expr->getCastTypeLoc(),
30193017
TypeResolution::forContextual(CS.DC, options)))
30203018
return nullptr;
30213019

@@ -3042,7 +3040,6 @@ namespace {
30423040
}
30433041

30443042
Type visitConditionalCheckedCastExpr(ConditionalCheckedCastExpr *expr) {
3045-
auto &ctx = CS.getASTContext();
30463043
auto fromExpr = expr->getSubExpr();
30473044
if (!fromExpr) // Either wasn't constructed correctly or wasn't folded.
30483045
return nullptr;
@@ -3051,7 +3048,7 @@ namespace {
30513048
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
30523049
options |= TypeResolutionFlags::AllowUnboundGenerics;
30533050
if (TypeChecker::validateType(
3054-
ctx, expr->getCastTypeLoc(),
3051+
expr->getCastTypeLoc(),
30553052
TypeResolution::forContextual(CS.DC, options)))
30563053
return nullptr;
30573054

@@ -3081,7 +3078,7 @@ namespace {
30813078
TypeResolutionOptions options(TypeResolverContext::ExplicitCastExpr);
30823079
options |= TypeResolutionFlags::AllowUnboundGenerics;
30833080
if (TypeChecker::validateType(
3084-
ctx, expr->getCastTypeLoc(),
3081+
expr->getCastTypeLoc(),
30853082
TypeResolution::forContextual(CS.DC, options)))
30863083
return nullptr;
30873084

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,12 +1916,11 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
19161916
TypeResolutionOptions options(TypeResolverContext::InExpression);
19171917
options |= TypeResolutionFlags::AllowUnboundGenerics;
19181918

1919-
typeLoc = TypeLoc(typeExpr->getTypeRepr(), Type());
1920-
bool hadError = TypeChecker::validateType(
1921-
getASTContext(), typeLoc, TypeResolution::forContextual(DC, options));
1922-
1923-
if (hadError)
1919+
auto result = TypeResolution::forContextual(DC, options)
1920+
.resolveType(typeExpr->getTypeRepr());
1921+
if (result->hasError())
19241922
return nullptr;
1923+
typeLoc = TypeLoc{typeExpr->getTypeRepr(), result};
19251924
}
19261925

19271926
if (!typeLoc.getType() || !typeLoc.getType()->getAnyNominal())

lib/Sema/TypeCheckDecl.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,15 +1431,13 @@ static NominalTypeDecl *resolveSingleNominalTypeDecl(
14311431
TypeResolutionFlags flags = TypeResolutionFlags(0)) {
14321432
auto *TyR = new (Ctx) SimpleIdentTypeRepr(DeclNameLoc(loc),
14331433
DeclNameRef(ident));
1434-
TypeLoc typeLoc = TypeLoc(TyR);
14351434

14361435
TypeResolutionOptions options = TypeResolverContext::TypeAliasDecl;
14371436
options |= flags;
1438-
if (TypeChecker::validateType(
1439-
Ctx, typeLoc, TypeResolution::forInterface(DC, options)))
1437+
auto result = TypeResolution::forInterface(DC, options).resolveType(TyR);
1438+
if (result->hasError())
14401439
return nullptr;
1441-
1442-
return typeLoc.getType()->getAnyNominal();
1440+
return result->getAnyNominal();
14431441
}
14441442

14451443
bool swift::checkDesignatedTypes(OperatorDecl *OD,
@@ -1768,14 +1766,13 @@ UnderlyingTypeRequest::evaluate(Evaluator &evaluator,
17681766
return ErrorType::get(typeAlias->getASTContext());
17691767
}
17701768

1771-
auto underlyingLoc = TypeLoc(typeAlias->getUnderlyingTypeRepr());
1772-
if (TypeChecker::validateType(
1773-
typeAlias->getASTContext(), underlyingLoc,
1774-
TypeResolution::forInterface(typeAlias, options))) {
1769+
auto result = TypeResolution::forInterface(typeAlias, options)
1770+
.resolveType(underlyingRepr);
1771+
if (result->hasError()) {
17751772
typeAlias->setInvalid();
17761773
return ErrorType::get(typeAlias->getASTContext());
17771774
}
1778-
return underlyingLoc.getType();
1775+
return result;
17791776
}
17801777

17811778
/// Bind the given function declaration, which declares an operator, to the
@@ -2120,16 +2117,14 @@ static Type validateParameterType(ParamDecl *decl) {
21202117
TypeResolverContext::FunctionInput);
21212118
options |= TypeResolutionFlags::Direct;
21222119

2123-
auto TL = TypeLoc(decl->getTypeRepr());
2124-
21252120
auto &ctx = dc->getASTContext();
2126-
auto resolution = TypeResolution::forInterface(dc, options);
2127-
if (TypeChecker::validateType(ctx, TL, resolution)) {
2121+
auto Ty = TypeResolution::forInterface(dc, options)
2122+
.resolveType(decl->getTypeRepr());
2123+
if (Ty->hasError()) {
21282124
decl->setInvalid();
21292125
return ErrorType::get(ctx);
21302126
}
21312127

2132-
Type Ty = TL.getType();
21332128
if (decl->isVariadic()) {
21342129
Ty = TypeChecker::getArraySliceType(decl->getStartLoc(), Ty);
21352130
if (Ty.isNull()) {
@@ -2146,7 +2141,7 @@ static Type validateParameterType(ParamDecl *decl) {
21462141

21472142
return Ty;
21482143
}
2149-
return TL.getType();
2144+
return Ty;
21502145
}
21512146

21522147
Type

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,11 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
131131
// Try to resolve the constraint repr. It should be some kind of existential
132132
// type.
133133
TypeResolutionOptions options(TypeResolverContext::GenericRequirement);
134-
TypeLoc constraintTypeLoc(repr->getConstraint());
135134
// Pass along the error type if resolving the repr failed.
136-
auto resolution = TypeResolution::forInterface(
137-
dc, dc->getGenericSignatureOfContext(), options);
138-
bool validationError
139-
= TypeChecker::validateType(ctx, constraintTypeLoc, resolution);
140-
auto constraintType = constraintTypeLoc.getType();
141-
if (validationError)
135+
auto constraintType = TypeResolution::forInterface(
136+
dc, dc->getGenericSignatureOfContext(), options)
137+
.resolveType(repr->getConstraint());
138+
if (constraintType->hasError())
142139
return nullptr;
143140

144141
// Error out if the constraint type isn't a class or existential type.

lib/Sema/TypeCheckPattern.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ static Type validateTypedPattern(TypeResolution resolution,
694694
hadError = true;
695695
}
696696
} else {
697-
hadError = TypeChecker::validateType(Context, TL, resolution);
697+
hadError = TypeChecker::validateType(TL, resolution);
698698
}
699699

700700
if (hadError) {
@@ -1211,7 +1211,7 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
12111211
// Type-check the type parameter.
12121212
TypeResolutionOptions paramOptions(TypeResolverContext::InExpression);
12131213
TypeResolution resolution = TypeResolution::forContextual(dc, paramOptions);
1214-
if (validateType(Context, IP->getCastTypeLoc(), resolution))
1214+
if (validateType(IP->getCastTypeLoc(), resolution))
12151215
return nullptr;
12161216

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

lib/Sema/TypeCheckPropertyWrapper.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,7 @@ Type AttachedPropertyWrapperTypeRequest::evaluate(Evaluator &evaluator,
561561

562562
auto resolution =
563563
TypeResolution::forContextual(var->getDeclContext(), options);
564-
if (TypeChecker::validateType(var->getASTContext(),
565-
customAttr->getTypeLoc(),
566-
resolution)) {
564+
if (TypeChecker::validateType(customAttr->getTypeLoc(), resolution)) {
567565
return ErrorType::get(var->getASTContext());
568566
}
569567

0 commit comments

Comments
 (0)