Skip to content

Commit 060cbb2

Browse files
committed
[NFC] Downgrade The TypeLoc in VarDecl to a TypeRepr
TypeCheckPattern used to splat the interface type into this, and different parts of the compiler would check one or the other. There is now one source of truth: The interface type. The type repr is now just a signal that the user has written an explicit type annotation on a parameter. For variables, we will eventually be able to just grab this information from the parent pattern.
1 parent 171ff44 commit 060cbb2

23 files changed

+176
-174
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4804,8 +4804,7 @@ class VarDecl : public AbstractStorageDecl {
48044804
bool issCaptureList, SourceLoc nameLoc, Identifier name,
48054805
DeclContext *dc, StorageIsMutable_t supportsMutation);
48064806

4807-
/// This is the type specified, including location information.
4808-
TypeLoc typeLoc;
4807+
TypeRepr *ParentRepr = nullptr;
48094808

48104809
Type typeInContext;
48114810

@@ -4825,8 +4824,10 @@ class VarDecl : public AbstractStorageDecl {
48254824
return hasName() ? getBaseName().getIdentifier().str() : "_";
48264825
}
48274826

4828-
TypeLoc &getTypeLoc() { return typeLoc; }
4829-
TypeLoc getTypeLoc() const { return typeLoc; }
4827+
/// Retrieve the TypeRepr corresponding to the parsed type of the parent
4828+
/// pattern, if it exists.
4829+
TypeRepr *getTypeRepr() const { return ParentRepr; }
4830+
void setTypeRepr(TypeRepr *repr) { ParentRepr = repr; }
48304831

48314832
bool hasType() const {
48324833
// We have a type if either the type has been computed already or if

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,8 +2523,10 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
25232523
});
25242524
if (decl->hasInterfaceType()) {
25252525
Printer << ": ";
2526-
auto tyLoc = decl->getTypeLoc();
2527-
if (!tyLoc.getTypeRepr())
2526+
TypeLoc tyLoc;
2527+
if (auto *repr = tyLoc.getTypeRepr())
2528+
tyLoc = TypeLoc(repr, decl->getInterfaceType());
2529+
else
25282530
tyLoc = TypeLoc::withoutLoc(decl->getInterfaceType());
25292531

25302532
Printer.printDeclResultTypePre(decl, tyLoc);
@@ -2592,13 +2594,15 @@ void PrintAST::printOneParameter(const ParamDecl *param,
25922594
Printer << ": ";
25932595
};
25942596

2595-
auto TheTypeLoc = param->getTypeLoc();
2596-
25972597
printAttributes(param);
25982598
printArgName();
25992599

2600-
if (!TheTypeLoc.getTypeRepr() && param->hasInterfaceType())
2600+
TypeLoc TheTypeLoc;
2601+
if (auto *repr = param->getTypeRepr()) {
2602+
TheTypeLoc = TypeLoc(repr, param->getInterfaceType());
2603+
} else {
26012604
TheTypeLoc = TypeLoc::withoutLoc(param->getInterfaceType());
2605+
}
26022606

26032607
// If the parameter is variadic, we will print the "..." after it, but we have
26042608
// to strip off the added array type.

lib/AST/ASTWalker.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,9 +1158,13 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
11581158

11591159
// Don't walk into the type if the decl is implicit, or if the type is
11601160
// implicit.
1161-
if (!P->isImplicit() && !P->isTypeLocImplicit() &&
1162-
doIt(P->getTypeLoc()))
1163-
return true;
1161+
if (!P->isImplicit() && !P->isTypeLocImplicit()) {
1162+
if (auto *repr = P->getTypeRepr()) {
1163+
if (doIt(repr)) {
1164+
return true;
1165+
}
1166+
}
1167+
}
11641168

11651169
if (auto *E = P->getDefaultValue()) {
11661170
auto res = doIt(E);

lib/AST/Decl.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,7 +2579,7 @@ void ValueDecl::setOverriddenDecls(ArrayRef<ValueDecl *> overridden) {
25792579
}
25802580

25812581
OpaqueReturnTypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
2582-
TypeLoc returnLoc;
2582+
TypeRepr *returnRepr = nullptr;
25832583
if (auto *VD = dyn_cast<VarDecl>(this)) {
25842584
if (auto *P = VD->getParentPattern()) {
25852585
while (auto *PP = dyn_cast<ParenPattern>(P))
@@ -2591,19 +2591,19 @@ OpaqueReturnTypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
25912591
assert(NP->getDecl() == VD);
25922592
(void) NP;
25932593

2594-
returnLoc = TP->getTypeLoc();
2594+
returnRepr = TP->getTypeLoc().getTypeRepr();
25952595
}
25962596
}
25972597
} else {
2598-
returnLoc = VD->getTypeLoc();
2598+
returnRepr = VD->getTypeRepr();
25992599
}
26002600
} else if (auto *FD = dyn_cast<FuncDecl>(this)) {
2601-
returnLoc = FD->getBodyResultTypeLoc();
2601+
returnRepr = FD->getBodyResultTypeLoc().getTypeRepr();
26022602
} else if (auto *SD = dyn_cast<SubscriptDecl>(this)) {
2603-
returnLoc = SD->getElementTypeLoc();
2603+
returnRepr = SD->getElementTypeLoc().getTypeRepr();
26042604
}
26052605

2606-
return dyn_cast_or_null<OpaqueReturnTypeRepr>(returnLoc.getTypeRepr());
2606+
return dyn_cast_or_null<OpaqueReturnTypeRepr>(returnRepr);
26072607
}
26082608

26092609
OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
@@ -5175,7 +5175,7 @@ SourceRange VarDecl::getSourceRange() const {
51755175
SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
51765176
// For a parameter, map back to its parameter to get the TypeLoc.
51775177
if (auto *PD = dyn_cast<ParamDecl>(this)) {
5178-
if (auto typeRepr = PD->getTypeLoc().getTypeRepr())
5178+
if (auto typeRepr = PD->getTypeRepr())
51795179
return typeRepr->getSourceRange();
51805180
}
51815181

@@ -5744,9 +5744,8 @@ ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
57445744
DefaultValueAndFlags(nullptr, PD->DefaultValueAndFlags.getInt()) {
57455745
Bits.ParamDecl.IsTypeLocImplicit = PD->Bits.ParamDecl.IsTypeLocImplicit;
57465746
Bits.ParamDecl.defaultArgumentKind = PD->Bits.ParamDecl.defaultArgumentKind;
5747-
typeLoc = PD->getTypeLoc().clone(PD->getASTContext());
5748-
if (!withTypes && typeLoc.getTypeRepr())
5749-
typeLoc.setType(Type());
5747+
if (auto *repr = PD->getTypeRepr())
5748+
setTypeRepr(repr->clone(PD->getASTContext()));
57505749

57515750
if (withTypes && PD->hasInterfaceType())
57525751
setInterfaceType(PD->getInterfaceType());
@@ -5794,9 +5793,9 @@ SourceRange ParamDecl::getSourceRange() const {
57945793
startLoc = APINameLoc;
57955794
else if (nameLoc.isValid())
57965795
startLoc = nameLoc;
5797-
else {
5798-
startLoc = getTypeLoc().getSourceRange().Start;
5799-
}
5796+
else if (auto *repr = getTypeRepr())
5797+
startLoc = repr->getStartLoc();
5798+
58005799
if (startLoc.isInvalid())
58015800
return SourceRange();
58025801

@@ -5810,7 +5809,7 @@ SourceRange ParamDecl::getSourceRange() const {
58105809
}
58115810

58125811
// If the typeloc has a valid location, use it to end the range.
5813-
if (auto typeRepr = getTypeLoc().getTypeRepr()) {
5812+
if (auto typeRepr = getTypeRepr()) {
58145813
auto endLoc = typeRepr->getEndLoc();
58155814
if (endLoc.isValid() && !isTypeLocImplicit())
58165815
return SourceRange(startLoc, endLoc);

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5116,9 +5116,8 @@ void GenericSignatureBuilder::inferRequirements(
51165116
ModuleDecl &module,
51175117
ParameterList *params) {
51185118
for (auto P : *params) {
5119-
inferRequirements(module, P->getTypeLoc().getType(),
5120-
FloatingRequirementSource::forInferred(
5121-
P->getTypeLoc().getTypeRepr()));
5119+
inferRequirements(module, P->getInterfaceType(),
5120+
FloatingRequirementSource::forInferred(P->getTypeRepr()));
51225121
}
51235122
}
51245123

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ static std::vector<CharSourceRange> getEnumParamListInfo(SourceManager &SM,
139139
for (ParamDecl *Param: *PL) {
140140
if (Param->isImplicit())
141141
continue;
142-
143-
SourceLoc LabelStart(Param->getTypeLoc().getLoc());
142+
143+
SourceLoc LabelStart;
144+
if (auto *repr = Param->getTypeRepr())
145+
LabelStart = repr->getLoc();
144146
SourceLoc LabelEnd(LabelStart);
145147

146148
if (Param->getNameLoc().isValid()) {

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
7373

7474
for (auto *Param: *Parent->getParameters()) {
7575
if (!--NextIndex) {
76-
return findChild(Param->getTypeLoc());
76+
return findChild(Param->getTypeRepr());
7777
}
7878
}
7979
llvm_unreachable("child index out of bounds");

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,8 +2673,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
26732673
auto isTupleDestructuring = [](ParamDecl *param) -> bool {
26742674
if (!param->isInvalid())
26752675
return false;
2676-
auto &typeLoc = param->getTypeLoc();
2677-
if (auto typeRepr = typeLoc.getTypeRepr())
2676+
if (auto *typeRepr = param->getTypeRepr())
26782677
return !param->hasName() && isa<TupleTypeRepr>(typeRepr);
26792678
return false;
26802679
};
@@ -2685,7 +2684,6 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
26852684
continue;
26862685

26872686
auto argName = "arg" + std::to_string(i);
2688-
auto typeLoc = param->getTypeLoc();
26892687

26902688
SmallString<64> fixIt;
26912689
llvm::raw_svector_ostream OS(fixIt);
@@ -2695,7 +2693,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
26952693
OS << '\n' << indent;
26962694

26972695
OS << "let ";
2698-
printTupleNames(typeLoc.getTypeRepr(), OS);
2696+
printTupleNames(param->getTypeRepr(), OS);
26992697
OS << " = " << argName << (isMultiLine ? "\n" + indent : "; ");
27002698

27012699
diagnose(param->getStartLoc(), diag::anon_closure_tuple_param_destructuring)

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ mapParsedParameters(Parser &parser,
481481
auto setInvalid = [&]{
482482
if (param->isInvalid())
483483
return;
484-
param->getTypeLoc().setInvalidType(ctx);
484+
param->getTypeRepr()->setInvalid();
485485
param->setInvalid();
486486
};
487487

@@ -512,7 +512,7 @@ mapParsedParameters(Parser &parser,
512512
"__owned",
513513
parsingEnumElt);
514514
}
515-
param->getTypeLoc() = TypeLoc(type);
515+
param->setTypeRepr(type);
516516

517517
// If there is `@autoclosure` attribute associated with the type
518518
// let's mark that in the declaration as well, because it
@@ -629,7 +629,7 @@ mapParsedParameters(Parser &parser,
629629
.fixItRemove(param.EllipsisLoc);
630630

631631
param.EllipsisLoc = SourceLoc();
632-
} else if (!result->getTypeLoc().getTypeRepr()) {
632+
} else if (!result->getTypeRepr()) {
633633
parser.diagnose(param.EllipsisLoc, diag::untyped_pattern_ellipsis)
634634
.highlight(result->getSourceRange());
635635

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
985985
emitDiagnostic(PD->getLoc(), diag::noescape_parameter, PD->getName());
986986

987987
if (!PD->isAutoClosure()) {
988-
note.fixItInsert(PD->getTypeLoc().getSourceRange().Start, "@escaping ");
988+
note.fixItInsert(PD->getTypeRepr()->getStartLoc(), "@escaping ");
989989
} // TODO: add in a fixit for autoclosure
990990

991991
return true;
@@ -4125,9 +4125,8 @@ bool ClosureParamDestructuringFailure::diagnoseAsError() {
41254125
// with parameters, if there are, we'll have to add
41264126
// type information to the replacement argument.
41274127
bool explicitTypes =
4128-
llvm::any_of(params->getArray(), [](const ParamDecl *param) {
4129-
return param->getTypeLoc().getTypeRepr();
4130-
});
4128+
llvm::any_of(params->getArray(),
4129+
[](const ParamDecl *param) { return param->getTypeRepr(); });
41314130

41324131
if (isMultiLineClosure)
41334132
OS << '\n' << indent;

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,8 +2027,8 @@ namespace {
20272027
Type paramType, internalType;
20282028

20292029
// If a type was explicitly specified, use its opened type.
2030-
if (auto type = param->getTypeLoc().getType()) {
2031-
paramType = closureExpr->mapTypeIntoContext(type);
2030+
if (param->getTypeRepr()) {
2031+
paramType = closureExpr->mapTypeIntoContext(param->getInterfaceType());
20322032
// FIXME: Need a better locator for a pattern as a base.
20332033
paramType = CS.openUnboundGenericType(paramType, locator);
20342034
internalType = paramType;

lib/Sema/CodeSynthesis.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,6 @@ createDesignatedInitOverride(ClassDecl *classDecl,
670670
auto substTy = paramTy.subst(subMap);
671671

672672
bodyParam->setInterfaceType(substTy);
673-
bodyParam->getTypeLoc() = TypeLoc::withoutLoc(substTy);
674673
}
675674

676675
// Create the initializer declaration, inheriting the name,

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void diagnoseFailedDerivation(DeclContext *DC, NominalTypeDecl *nominal,
141141
associatedValuesNotConformingToProtocol(DC, enumDecl, protocol);
142142
for (auto *typeToDiagnose : nonconformingAssociatedTypes) {
143143
ctx.Diags.diagnose(
144-
typeToDiagnose->getTypeLoc().getLoc(),
144+
typeToDiagnose->getTypeRepr()->getLoc(),
145145
diag::missing_member_type_conformance_prevents_synthesis,
146146
NonconformingMemberKind::AssociatedValue,
147147
typeToDiagnose->getInterfaceType(), protocol->getDeclaredType(),

0 commit comments

Comments
 (0)