Skip to content

Commit 47c1fd9

Browse files
authored
Merge pull request #60751 from ahoppen/pr/rdar99096663
[SourceKit] Fix a crash because the API for retrieving intherited protocols of a ProtocolDecl changed
2 parents a643acd + 8f421a7 commit 47c1fd9

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3309,6 +3309,18 @@ bool RefactoringActionMemberwiseInitLocalRefactoring::performChange() {
33093309
return false;
33103310
}
33113311

3312+
/// If \p NTD is a protocol, return all the protocols it inherits from. If it's
3313+
/// a type, return all the protocols it conforms to.
3314+
static SmallVector<ProtocolDecl *, 2> getAllProtocols(NominalTypeDecl *NTD) {
3315+
if (auto Proto = dyn_cast<ProtocolDecl>(NTD)) {
3316+
return SmallVector<ProtocolDecl *, 2>(
3317+
Proto->getInheritedProtocols().begin(),
3318+
Proto->getInheritedProtocols().end());
3319+
} else {
3320+
return NTD->getAllProtocols();
3321+
}
3322+
}
3323+
33123324
class AddEquatableContext {
33133325

33143326
/// Declaration context
@@ -3366,19 +3378,23 @@ class AddEquatableContext {
33663378
std::vector<VarDecl *> getUserAccessibleProperties();
33673379

33683380
public:
3381+
AddEquatableContext(NominalTypeDecl *Decl)
3382+
: DC(Decl), Adopter(Decl->getDeclaredType()),
3383+
StartLoc(Decl->getBraces().Start),
3384+
ProtocolsLocations(Decl->getInherited()),
3385+
Protocols(getAllProtocols(Decl)),
3386+
ProtInsertStartLoc(Decl->getNameLoc()),
3387+
StoredProperties(Decl->getStoredProperties()),
3388+
Range(Decl->getMembers()){};
33693389

3370-
AddEquatableContext(NominalTypeDecl *Decl) : DC(Decl),
3371-
Adopter(Decl->getDeclaredType()), StartLoc(Decl->getBraces().Start),
3372-
ProtocolsLocations(Decl->getInherited()),
3373-
Protocols(Decl->getAllProtocols()), ProtInsertStartLoc(Decl->getNameLoc()),
3374-
StoredProperties(Decl->getStoredProperties()), Range(Decl->getMembers()) {};
3375-
3376-
AddEquatableContext(ExtensionDecl *Decl) : DC(Decl),
3377-
Adopter(Decl->getExtendedType()), StartLoc(Decl->getBraces().Start),
3378-
ProtocolsLocations(Decl->getInherited()),
3379-
Protocols(Decl->getExtendedNominal()->getAllProtocols()),
3380-
ProtInsertStartLoc(Decl->getExtendedTypeRepr()->getEndLoc()),
3381-
StoredProperties(Decl->getExtendedNominal()->getStoredProperties()), Range(Decl->getMembers()) {};
3390+
AddEquatableContext(ExtensionDecl *Decl)
3391+
: DC(Decl), Adopter(Decl->getExtendedType()),
3392+
StartLoc(Decl->getBraces().Start),
3393+
ProtocolsLocations(Decl->getInherited()),
3394+
Protocols(getAllProtocols(Decl->getExtendedNominal())),
3395+
ProtInsertStartLoc(Decl->getExtendedTypeRepr()->getEndLoc()),
3396+
StoredProperties(Decl->getExtendedNominal()->getStoredProperties()),
3397+
Range(Decl->getMembers()){};
33823398

33833399
AddEquatableContext() : DC(nullptr), Adopter(), ProtocolsLocations(),
33843400
Protocols(), StoredProperties(), Range(nullptr, nullptr) {};
@@ -3583,11 +3599,11 @@ class AddCodableContext {
35833599
public:
35843600
AddCodableContext(NominalTypeDecl *Decl)
35853601
: DC(Decl), StartLoc(Decl->getBraces().Start),
3586-
Protocols(Decl->getAllProtocols()), Range(Decl->getMembers()){};
3602+
Protocols(getAllProtocols(Decl)), Range(Decl->getMembers()){};
35873603

35883604
AddCodableContext(ExtensionDecl *Decl)
35893605
: DC(Decl), StartLoc(Decl->getBraces().Start),
3590-
Protocols(Decl->getExtendedNominal()->getAllProtocols()),
3606+
Protocols(getAllProtocols(Decl->getExtendedNominal())),
35913607
Range(Decl->getMembers()){};
35923608

35933609
AddCodableContext() : DC(nullptr), Protocols(), Range(nullptr, nullptr){};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// This should not crash
2+
// RUN: %sourcekitd-test -req=cursor -cursor-action -pos=4:11 %s -- %s
3+
4+
extension RandomAccessCollection

0 commit comments

Comments
 (0)