Skip to content

Commit 0f2d2f4

Browse files
authored
Merge pull request #18671 from davezarzycki/avoid_double_lookup
2 parents 9bbdc8a + 3f2fdd8 commit 0f2d2f4

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,20 +373,21 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
373373
continue;
374374

375375
// Check whether we inherited from the same type twice.
376-
CanType inheritedCanTy = inheritedTy->getCanonicalType();
377-
auto knownType = inheritedTypes.find(inheritedCanTy);
378-
if (knownType != inheritedTypes.end()) {
376+
auto knownPair = inheritedTypes.insert({ inheritedTy->getCanonicalType(),
377+
{i, inherited.getSourceRange() }});
378+
if (knownPair.second == /*insertion failed*/false) {
379+
auto knownIndex = knownPair.first->second.first;
380+
auto knownRange = knownPair.first->second.second;
379381
// If the duplicated type is 'AnyObject', check whether the first was
380382
// written as 'class'. Downgrade the error to a warning in such cases
381383
// for backward compatibility with Swift <= 4.
382384
if (!Context.LangOpts.isSwiftVersionAtLeast(5) &&
383385
inheritedTy->isAnyObject() &&
384386
(isa<ProtocolDecl>(decl) || isa<AbstractTypeParamDecl>(decl)) &&
385-
Lexer::getTokenAtLocation(Context.SourceMgr,
386-
knownType->second.second.Start)
387+
Lexer::getTokenAtLocation(Context.SourceMgr, knownRange.Start)
387388
.is(tok::kw_class)) {
388-
SourceLoc classLoc = knownType->second.second.Start;
389-
SourceRange removeRange = getRemovalRange(knownType->second.first);
389+
SourceLoc classLoc = knownRange.Start;
390+
SourceRange removeRange = getRemovalRange(knownIndex);
390391

391392
diagnose(classLoc, diag::duplicate_anyobject_class_inheritance)
392393
.fixItRemoveChars(removeRange.Start, removeRange.End);
@@ -398,11 +399,10 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
398399
diagnose(inherited.getSourceRange().Start,
399400
diag::duplicate_inheritance, inheritedTy)
400401
.fixItRemoveChars(removeRange.Start, removeRange.End)
401-
.highlight(knownType->second.second);
402+
.highlight(knownRange);
402403
inherited.setInvalidType(Context);
403404
continue;
404405
}
405-
inheritedTypes[inheritedCanTy] = { i, inherited.getSourceRange() };
406406

407407
if (inheritedTy->isExistentialType()) {
408408
auto layout = inheritedTy->getExistentialLayout();

0 commit comments

Comments
 (0)