@@ -774,8 +774,18 @@ void IterableDeclContext::addMemberPreservingSourceOrder(Decl *member) {
774
774
if (existingMember->isImplicit ())
775
775
continue ;
776
776
777
- if (isa<EnumCaseDecl>(existingMember) ||
778
- isa<IfConfigDecl>(existingMember))
777
+ // An EnumCaseDecl contains one or more EnumElementDecls,
778
+ // but the EnumElementDecls are also added as members of
779
+ // the parent enum. We ignore the EnumCaseDecl since its
780
+ // source range overlaps with that of the EnumElementDecls.
781
+ if (isa<EnumCaseDecl>(existingMember))
782
+ continue ;
783
+
784
+ // The elements of the active clause of an IfConfigDecl
785
+ // are added to the parent type. We ignore the IfConfigDecl
786
+ // since its source range overlaps with the source ranges
787
+ // of the active elements.
788
+ if (isa<IfConfigDecl>(existingMember))
779
789
continue ;
780
790
781
791
if (!SM.isBeforeInBuffer (existingMember->getEndLoc (), start))
@@ -819,14 +829,24 @@ void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,
819
829
assert (!member->NextDecl && " Already added to a container" );
820
830
821
831
#ifndef NDEBUG
832
+ // Assert that new declarations are always added in source order.
822
833
auto checkSourceRange = [&](Decl *prev, Decl *next) {
834
+ // SKip these checks for imported and deserialized decls.
823
835
if (!member->getDeclContext ()->getParentSourceFile ())
824
836
return ;
825
837
826
838
auto shouldSkip = [](Decl *d) {
827
- if (isa<VarDecl>(d) || isa<EnumElementDecl>(d) || isa<IfConfigDecl>(d))
839
+ // PatternBindingDecl source ranges overlap with VarDecls,
840
+ // EnumCaseDecl source ranges overlap with EnumElementDecls,
841
+ // and IfConfigDecl source ranges overlap with the elements
842
+ // of the active clause. Skip them all here to avoid
843
+ // spurious assertions.
844
+ if (isa<PatternBindingDecl>(d) ||
845
+ isa<EnumCaseDecl>(d) ||
846
+ isa<IfConfigDecl>(d))
828
847
return true ;
829
848
849
+ // Ignore source location information of implicit declarations.
830
850
if (d->isImplicit ())
831
851
return true ;
832
852
@@ -839,8 +859,10 @@ void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,
839
859
SourceLoc prevEnd = prev->getEndLoc ();
840
860
SourceLoc nextStart = next->getStartLoc ();
841
861
842
- if (!prevEnd.isValid () || !nextStart.isValid ())
843
- return ;
862
+ assert (prevEnd.isValid () &&
863
+ " Only implicit decls can have invalid source location" );
864
+ assert (nextStart.isValid () &&
865
+ " Only implicit decls can have invalid source location" );
844
866
845
867
if (getASTContext ().SourceMgr .isBeforeInBuffer (prevEnd, nextStart))
846
868
return ;
0 commit comments