Skip to content

Commit 7bcc4ba

Browse files
committed
[NFC] TypeLoc: Rid performTypeLocChecking off TypeLoc
1 parent 1420a38 commit 7bcc4ba

File tree

5 files changed

+73
-86
lines changed

5 files changed

+73
-86
lines changed

include/swift/Subsystems.h

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ namespace swift {
6565
struct TBDGenOptions;
6666
class Token;
6767
class TopLevelContext;
68+
class Type;
6869
class TypeCheckerOptions;
69-
class TypeLoc;
70+
class TypeRepr;
7071
class UnifiedStatsReporter;
7172

7273
namespace Lowering {
@@ -140,28 +141,15 @@ namespace swift {
140141
/// emitted.
141142
void performWholeModuleTypeChecking(SourceFile &SF);
142143

143-
/// Recursively validate the specified type.
144+
/// Resolve the given \c TypeRepr to a contextual type.
144145
///
145146
/// This is used when dealing with partial source files (e.g. SIL parsing,
146147
/// code completion).
147148
///
148-
/// \returns false on success, true on error.
149-
bool performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,
150-
DeclContext *DC,
151-
bool ProduceDiagnostics = true);
152-
153-
/// Recursively validate the specified type.
154-
///
155-
/// This is used when dealing with partial source files (e.g. SIL parsing,
156-
/// code completion).
157-
///
158-
/// \returns false on success, true on error.
159-
bool performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,
160-
bool isSILMode,
161-
bool isSILType,
162-
GenericEnvironment *GenericEnv,
163-
DeclContext *DC,
164-
bool ProduceDiagnostics = true);
149+
/// \returns A well-formed type on success, or an \c ErrorType.
150+
Type performTypeResolution(TypeRepr *TyR, ASTContext &Ctx, bool isSILMode,
151+
bool isSILType, GenericEnvironment *GenericEnv,
152+
DeclContext *DC, bool ProduceDiagnostics = true);
165153

166154
/// Expose TypeChecker's handling of GenericParamList to SIL parsing.
167155
GenericEnvironment *handleSILGenericParams(GenericParamList *genericParams,

lib/IDE/CodeCompletion.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,9 +1613,20 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
16131613
/// \returns true on success, false on failure.
16141614
bool typecheckParsedType() {
16151615
assert(ParsedTypeLoc.getTypeRepr() && "should have a TypeRepr");
1616-
if (!performTypeLocChecking(P.Context, ParsedTypeLoc,
1617-
CurDeclContext, false))
1616+
if (ParsedTypeLoc.wasValidated() && !ParsedTypeLoc.isError()) {
16181617
return true;
1618+
}
1619+
1620+
const auto ty = swift::performTypeResolution(
1621+
ParsedTypeLoc.getTypeRepr(), P.Context,
1622+
/*isSILMode=*/false,
1623+
/*isSILType=*/false, CurDeclContext->getGenericEnvironmentOfContext(),
1624+
CurDeclContext,
1625+
/*ProduceDiagnostics=*/false);
1626+
ParsedTypeLoc.setType(ty);
1627+
if (!ParsedTypeLoc.isError()) {
1628+
return true;
1629+
}
16191630

16201631
// It doesn't type check as a type, so see if it's a qualifying module name.
16211632
if (auto *ITR = dyn_cast<IdentTypeRepr>(ParsedTypeLoc.getTypeRepr())) {

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,14 @@ void swift::ide::collectPossibleReturnTypesFromContext(
325325
candidates.push_back(ty);
326326
return;
327327
} else {
328-
auto typeLoc = TypeLoc{CE->getExplicitResultTypeRepr()};
329-
if (!swift::performTypeLocChecking(
330-
DC->getASTContext(), typeLoc, /*isSILMode=*/false,
331-
/*isSILType=*/false, DC->getGenericEnvironmentOfContext(),
332-
const_cast<DeclContext *>(DC), /*diagnostics=*/false)) {
333-
candidates.push_back(typeLoc.getType());
328+
const auto type = swift::performTypeResolution(
329+
CE->getExplicitResultTypeRepr(), DC->getASTContext(),
330+
/*isSILMode=*/false, /*isSILType=*/false,
331+
DC->getGenericEnvironmentOfContext(),
332+
const_cast<DeclContext *>(DC), /*diagnostics=*/false);
333+
334+
if (!type->hasError()) {
335+
candidates.push_back(type);
334336
return;
335337
}
336338
}

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ namespace {
173173
/// A callback to be invoked every time a type was deserialized.
174174
std::function<void(Type)> ParsedTypeCallback;
175175

176-
Type performTypeLocChecking(TypeRepr *TyR, bool IsSILType,
177-
GenericEnvironment *GenericEnv = nullptr);
176+
Type performTypeResolution(TypeRepr *TyR, bool IsSILType,
177+
GenericEnvironment *GenericEnv);
178178

179179
void convertRequirements(SILFunction *F, ArrayRef<RequirementRepr> From,
180180
SmallVectorImpl<Requirement> &To);
@@ -867,9 +867,10 @@ void SILParser::convertRequirements(SILFunction *F,
867867
IdentTypeReprLookup PerformLookup(P);
868868
// Use parser lexical scopes to resolve references
869869
// to the generic parameters.
870-
auto ResolveToInterfaceType = [&](TypeRepr *Ty) -> Type {
871-
Ty->walk(PerformLookup);
872-
return performTypeLocChecking(Ty, /* IsSIL */ false)->mapTypeOutOfContext();
870+
auto ResolveToInterfaceType = [&](TypeRepr *TyR) -> Type {
871+
TyR->walk(PerformLookup);
872+
return performTypeResolution(TyR, /*IsSILType=*/false, ContextGenericEnv)
873+
->mapTypeOutOfContext();
873874
};
874875

875876
for (auto &Req : From) {
@@ -1091,16 +1092,14 @@ static bool parseDeclSILOptional(bool *isTransparent,
10911092
return false;
10921093
}
10931094

1094-
Type SILParser::performTypeLocChecking(TypeRepr *T, bool IsSILType,
1095-
GenericEnvironment *GenericEnv) {
1095+
Type SILParser::performTypeResolution(TypeRepr *TyR, bool IsSILType,
1096+
GenericEnvironment *GenericEnv) {
10961097
if (GenericEnv == nullptr)
10971098
GenericEnv = ContextGenericEnv;
10981099

1099-
TypeLoc loc(T);
1100-
(void) swift::performTypeLocChecking(P.Context, loc,
1101-
/*isSILMode=*/true, IsSILType,
1102-
GenericEnv, &P.SF);
1103-
return loc.getType();
1100+
return swift::performTypeResolution(TyR, P.Context,
1101+
/*isSILMode=*/true, IsSILType, GenericEnv,
1102+
&P.SF);
11041103
}
11051104

11061105
/// Find the top-level ValueDecl or Module given a name.
@@ -1154,7 +1153,8 @@ static ValueDecl *lookupMember(Parser &P, Type Ty, DeclBaseName Name,
11541153
bool SILParser::parseASTType(CanType &result, GenericEnvironment *env) {
11551154
ParserResult<TypeRepr> parsedType = P.parseType();
11561155
if (parsedType.isNull()) return true;
1157-
auto resolvedType = performTypeLocChecking(parsedType.get(), /*IsSILType=*/ false, env);
1156+
const auto resolvedType =
1157+
performTypeResolution(parsedType.get(), /*isSILType=*/false, env);
11581158
if (resolvedType->hasError())
11591159
return true;
11601160

@@ -1243,8 +1243,10 @@ bool SILParser::parseSILType(SILType &Result,
12431243
ParsedGenericEnv = env;
12441244

12451245
// Apply attributes to the type.
1246-
auto *attrRepr = P.applyAttributeToType(TyR.get(), attrs, specifier, specifierLoc);
1247-
auto Ty = performTypeLocChecking(attrRepr, /*IsSILType=*/true, OuterGenericEnv);
1246+
auto *attrRepr =
1247+
P.applyAttributeToType(TyR.get(), attrs, specifier, specifierLoc);
1248+
const auto Ty =
1249+
performTypeResolution(attrRepr, /*IsSILType=*/true, OuterGenericEnv);
12481250
if (Ty->hasError())
12491251
return true;
12501252

@@ -1696,7 +1698,8 @@ bool SILParser::parseSubstitutions(SmallVectorImpl<ParsedSubstitution> &parsed,
16961698
if (defaultForProto)
16971699
bindProtocolSelfInTypeRepr(TyR.get(), defaultForProto);
16981700

1699-
auto Ty = performTypeLocChecking(TyR.get(), /*IsSILType=*/ false, GenericEnv);
1701+
const auto Ty =
1702+
performTypeResolution(TyR.get(), /*IsSILType=*/false, GenericEnv);
17001703
if (Ty->hasError())
17011704
return true;
17021705
parsed.push_back({Loc, Ty});
@@ -2105,7 +2108,8 @@ bool SILParser::parseSILDeclRef(SILDeclRef &Member, bool FnTypeRequired) {
21052108
}
21062109
}
21072110

2108-
auto Ty = performTypeLocChecking(TyR.get(), /*IsSILType=*/ false, genericEnv);
2111+
const auto Ty =
2112+
performTypeResolution(TyR.get(), /*IsSILType=*/false, genericEnv);
21092113
if (Ty->hasError())
21102114
return true;
21112115

@@ -6321,7 +6325,8 @@ ProtocolConformanceRef SILParser::parseProtocolConformanceHelper(
63216325
bindProtocolSelfInTypeRepr(TyR.get(), defaultForProto);
63226326
}
63236327

6324-
auto ConformingTy = performTypeLocChecking(TyR.get(), /*IsSILType=*/ false, witnessEnv);
6328+
const auto ConformingTy =
6329+
performTypeResolution(TyR.get(), /*IsSILType=*/false, witnessEnv);
63256330
if (ConformingTy->hasError())
63266331
return ProtocolConformanceRef();
63276332

@@ -6437,17 +6442,18 @@ static bool parseSILVTableEntry(
64376442
ParserResult<TypeRepr> TyR = P.parseType();
64386443
if (TyR.isNull())
64396444
return true;
6440-
TypeLoc Ty = TyR.get();
6445+
64416446
if (isDefaultWitnessTable)
64426447
bindProtocolSelfInTypeRepr(TyR.get(), proto);
6443-
if (swift::performTypeLocChecking(P.Context, Ty,
6444-
/*isSILMode=*/false,
6445-
/*isSILType=*/false,
6446-
witnessEnv,
6447-
&P.SF))
6448+
6449+
const auto Ty =
6450+
swift::performTypeResolution(TyR.get(), P.Context,
6451+
/*isSILMode=*/false,
6452+
/*isSILType=*/false, witnessEnv, &P.SF);
6453+
if (Ty->hasError())
64486454
return true;
64496455

6450-
assocOrSubject = Ty.getType()->getCanonicalType();
6456+
assocOrSubject = Ty->getCanonicalType();
64516457
}
64526458
if (!assocOrSubject)
64536459
return true;
@@ -6498,19 +6504,19 @@ static bool parseSILVTableEntry(
64986504
ParserResult<TypeRepr> TyR = P.parseType();
64996505
if (TyR.isNull())
65006506
return true;
6501-
TypeLoc Ty = TyR.get();
6507+
65026508
if (isDefaultWitnessTable)
65036509
bindProtocolSelfInTypeRepr(TyR.get(), proto);
6504-
if (swift::performTypeLocChecking(P.Context, Ty,
6505-
/*isSILMode=*/false,
6506-
/*isSILType=*/false,
6507-
witnessEnv,
6508-
&P.SF))
6510+
6511+
const auto Ty =
6512+
swift::performTypeResolution(TyR.get(), P.Context,
6513+
/*isSILMode=*/false,
6514+
/*isSILType=*/false, witnessEnv, &P.SF);
6515+
if (Ty->hasError())
65096516
return true;
65106517

6511-
witnessEntries.push_back(SILWitnessTable::AssociatedTypeWitness{
6512-
assoc, Ty.getType()->getCanonicalType()
6513-
});
6518+
witnessEntries.push_back(
6519+
SILWitnessTable::AssociatedTypeWitness{assoc, Ty->getCanonicalType()});
65146520
return false;
65156521
}
65166522

lib/Sema/TypeChecker.cpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -358,23 +358,10 @@ bool swift::isAdditiveArithmeticConformanceDerivationEnabled(SourceFile &SF) {
358358
return isDifferentiableProgrammingEnabled(SF);
359359
}
360360

361-
bool swift::performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,
362-
DeclContext *DC,
363-
bool ProduceDiagnostics) {
364-
return performTypeLocChecking(
365-
Ctx, T,
366-
/*isSILMode=*/false,
367-
/*isSILType=*/false,
368-
/*GenericEnv=*/DC->getGenericEnvironmentOfContext(),
369-
DC, ProduceDiagnostics);
370-
}
371-
372-
bool swift::performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,
373-
bool isSILMode,
374-
bool isSILType,
375-
GenericEnvironment *GenericEnv,
376-
DeclContext *DC,
377-
bool ProduceDiagnostics) {
361+
Type swift::performTypeResolution(TypeRepr *TyR, ASTContext &Ctx,
362+
bool isSILMode, bool isSILType,
363+
GenericEnvironment *GenericEnv,
364+
DeclContext *DC, bool ProduceDiagnostics) {
378365
TypeResolutionOptions options = None;
379366
if (isSILMode) {
380367
options |= TypeResolutionFlags::SILMode;
@@ -394,14 +381,7 @@ bool swift::performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,
394381
if (!ProduceDiagnostics)
395382
suppression.emplace(Ctx.Diags);
396383

397-
// If we've already validated this type, don't do so again.
398-
if (T.wasValidated()) {
399-
return T.isError();
400-
}
401-
402-
auto type = resolution.resolveType(T.getTypeRepr());
403-
T.setType(type);
404-
return type->hasError();
384+
return resolution.resolveType(TyR);
405385
}
406386

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

0 commit comments

Comments
 (0)