Skip to content

Commit 01ecd81

Browse files
committed
AST: Introduce conveniences for inheritance clause source locations.
Replace the `front()` and `back()` accessors on `InheritedTypes` with dedicated functions for accessing the start and end source locations of the inheritance clause. NFC.
1 parent c884632 commit 01ecd81

11 files changed

+28
-38
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,10 +1565,6 @@ class InheritedTypes {
15651565
size_t size() const { return Entries.size(); }
15661566
IntRange<size_t> const getIndices() { return indices(Entries); }
15671567

1568-
// FIXME: Remove these in favor of more targeted conveniences
1569-
const InheritedEntry &front() const { return Entries.front(); }
1570-
const InheritedEntry &back() const { return Entries.back(); }
1571-
15721568
/// Returns the `TypeRepr *` for the entry of the inheritance clause at the
15731569
/// given index.
15741570
TypeRepr *getTypeRepr(unsigned i) const { return Entries[i].getTypeRepr(); }
@@ -1589,12 +1585,12 @@ class InheritedTypes {
15891585
const InheritedEntry &getEntry(unsigned i) const { return Entries[i]; }
15901586

15911587
/// Returns the source location of the beginning of the inheritance clause.
1592-
SourceLoc getSourceRangeStart() const {
1588+
SourceLoc getStartLoc() const {
15931589
return getEntries().front().getSourceRange().Start;
15941590
}
15951591

15961592
/// Returns the source location of the end of the inheritance clause.
1597-
SourceLoc getSourceRangeEnd() const {
1593+
SourceLoc getEndLoc() const {
15981594
return getEntries().back().getSourceRange().End;
15991595
}
16001596
};

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5223,7 +5223,7 @@ SourceRange GenericTypeParamDecl::getSourceRange() const {
52235223
startLoc = eachLoc;
52245224

52255225
if (!getInherited().empty())
5226-
endLoc = getInherited().back().getSourceRange().End;
5226+
endLoc = getInherited().getEndLoc();
52275227

52285228
return {startLoc, endLoc};
52295229
}
@@ -5261,7 +5261,7 @@ SourceRange AssociatedTypeDecl::getSourceRange() const {
52615261
} else if (auto defaultDefinition = getDefaultDefinitionTypeRepr()) {
52625262
endLoc = defaultDefinition->getEndLoc();
52635263
} else if (!getInherited().empty()) {
5264-
endLoc = getInherited().back().getSourceRange().End;
5264+
endLoc = getInherited().getEndLoc();
52655265
} else {
52665266
endLoc = getNameLoc();
52675267
}

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
956956
return { ", ", trailing->getRequirements().back().getSourceRange().End };
957957

958958
// Inheritance clause.
959-
return { " where ", proto->getInherited().back().getSourceRange().End };
959+
return { " where ", proto->getInherited().getEndLoc() };
960960
};
961961

962962
// Retrieve the set of requirements that a given associated type declaration

lib/IDE/Formatting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ class FormatWalker : public ASTWalker {
20632063
if (Inherits.empty())
20642064
return llvm::None;
20652065

2066-
SourceLoc StartLoc = Inherits.front().getSourceRange().Start;
2066+
SourceLoc StartLoc = Inherits.getStartLoc();
20672067
if (StartLoc.isInvalid())
20682068
return llvm::None;
20692069

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,10 +3400,8 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
34003400
emitDiagnostic(diag::assign_protocol_conformance_fix_it, constraint,
34013401
nominal->getDescriptiveKind(), fromType);
34023402
if (!nominal->getInherited().empty()) {
3403-
auto lastInherited = nominal->getInherited().back().getLoc();
3404-
auto lastInheritedEndLoc =
3405-
Lexer::getLocForEndOfToken(getASTContext().SourceMgr, lastInherited);
3406-
conformanceDiag.fixItInsert(lastInheritedEndLoc, ", " + protoString);
3403+
auto lastInheritedEndLoc = nominal->getInherited().getEndLoc();
3404+
conformanceDiag.fixItInsertAfter(lastInheritedEndLoc, ", " + protoString);
34073405
} else {
34083406
auto nameEndLoc = Lexer::getLocForEndOfToken(getASTContext().SourceMgr,
34093407
nominal->getNameLoc());

lib/Sema/DerivedConformanceComparable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void DerivedConformance::tryDiagnoseFailedComparableDerivation(
336336
if (auto enumDecl = dyn_cast<EnumDecl>(nominal)) {
337337
if (enumDecl->hasRawType() && !enumDecl->getRawType()->is<ErrorType>()) {
338338
auto rawType = enumDecl->getRawType();
339-
auto rawTypeLoc = enumDecl->getInherited().front().getSourceRange().Start;
339+
auto rawTypeLoc = enumDecl->getInherited().getStartLoc();
340340
ctx.Diags.diagnose(rawTypeLoc,
341341
diag::comparable_synthesis_raw_value_not_allowed,
342342
rawType, nominal->getDeclaredInterfaceType(),

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ static void addSendableFixIt(
629629
diag.fixItInsert(fixItLoc,
630630
unchecked ? ": @unchecked Sendable" : ": Sendable");
631631
} else {
632-
auto fixItLoc = nominal->getInherited().back().getLoc();
632+
auto fixItLoc = nominal->getInherited().getEndLoc();
633633
diag.fixItInsertAfter(fixItLoc,
634634
unchecked ? ", @unchecked Sendable" : ", Sendable");
635635
}
@@ -644,7 +644,7 @@ static void addSendableFixIt(const GenericTypeParamDecl *genericArgument,
644644
diag.fixItInsertAfter(fixItLoc,
645645
unchecked ? ": @unchecked Sendable" : ": Sendable");
646646
} else {
647-
auto fixItLoc = genericArgument->getInherited().back().getLoc();
647+
auto fixItLoc = genericArgument->getInherited().getEndLoc();
648648
diag.fixItInsertAfter(fixItLoc,
649649
unchecked ? ", @unchecked Sendable" : ", Sendable");
650650
}

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,8 +1319,9 @@ static llvm::Optional<ObjCReason> shouldMarkClassAsObjC(const ClassDecl *CD) {
13191319
.limitBehavior(behavior);
13201320
} else if (CD->getSuperclass().isNull()) {
13211321
CD->diagnose(diag::invalid_objc_swift_root_class_insert_nsobject)
1322-
.fixItInsert(CD->getInherited().front().getLoc(), "NSObject, ")
1323-
.limitBehavior(behavior);
1322+
.fixItInsert(CD->getInherited().getEntry(0).getLoc(),
1323+
"NSObject, ")
1324+
.limitBehavior(behavior);
13241325
}
13251326
}
13261327

@@ -1594,7 +1595,7 @@ static bool isEnumObjC(EnumDecl *enumDecl) {
15941595
if (!isCIntegerType(rawType)) {
15951596
SourceRange errorRange;
15961597
if (!enumDecl->getInherited().empty())
1597-
errorRange = enumDecl->getInherited().front().getSourceRange();
1598+
errorRange = enumDecl->getInherited().getEntry(0).getSourceRange();
15981599
enumDecl->diagnose(diag::objc_enum_raw_type_not_integer, rawType)
15991600
.highlight(errorRange);
16001601
return false;

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,14 +2728,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
27282728
// The raw type must be one of the blessed literal convertible types.
27292729
if (!computeAutomaticEnumValueKind(ED)) {
27302730
if (!rawTy->is<ErrorType>()) {
2731-
DE.diagnose(ED->getInherited().front().getSourceRange().Start,
2731+
DE.diagnose(ED->getInherited().getStartLoc(),
27322732
diag::raw_type_not_literal_convertible, rawTy);
27332733
}
27342734
}
27352735

27362736
// We need at least one case to have a raw value.
27372737
if (ED->getAllElements().empty()) {
2738-
DE.diagnose(ED->getInherited().front().getSourceRange().Start,
2738+
DE.diagnose(ED->getInherited().getStartLoc(),
27392739
diag::empty_enum_raw_type);
27402740
}
27412741
}
@@ -3467,7 +3467,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
34673467
if (EED->hasAssociatedValues()) {
34683468
if (auto rawTy = ED->getRawType()) {
34693469
EED->diagnose(diag::enum_with_raw_type_case_with_argument);
3470-
DE.diagnose(ED->getInherited().front().getSourceRange().Start,
3470+
DE.diagnose(ED->getInherited().getStartLoc(),
34713471
diag::enum_raw_type_here, rawTy);
34723472
EED->setInvalid();
34733473
}

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,8 @@ void swift::diagnoseDistributedFunctionInNonDistributedActorProtocol(
155155
} else {
156156
// Similar to how Sendable FitIts do this, we insert at the end of
157157
// the inherited types.
158-
ASTContext &ctx = proto->getASTContext();
159-
SourceLoc fixItLoc = proto->getInherited().back().getSourceRange().End;
160-
fixItLoc = Lexer::getLocForEndOfToken(ctx.SourceMgr, fixItLoc);
161-
diag.fixItInsert(fixItLoc, ", DistributedActor");
158+
SourceLoc fixItLoc = proto->getInherited().getEndLoc();
159+
diag.fixItInsertAfter(fixItLoc, ", DistributedActor");
162160
}
163161
}
164162

@@ -173,10 +171,8 @@ void swift::addCodableFixIt(
173171
SourceLoc fixItLoc = nominal->getBraces().Start;
174172
diag.fixItInsert(fixItLoc, ": Codable");
175173
} else {
176-
ASTContext &ctx = nominal->getASTContext();
177-
SourceLoc fixItLoc = nominal->getInherited().back().getSourceRange().End;
178-
fixItLoc = Lexer::getLocForEndOfToken(ctx.SourceMgr, fixItLoc);
179-
diag.fixItInsert(fixItLoc, ", Codable");
174+
SourceLoc fixItLoc = nominal->getInherited().getEndLoc();
175+
diag.fixItInsertAfter(fixItLoc, ", Codable");
180176
}
181177
}
182178

0 commit comments

Comments
 (0)