Skip to content

Commit 779276a

Browse files
committed
Bail On Checking Invalid Precedence Group
When the user spells an invalid precedence group, the Relation for that group will fail to resolve the decl. We need to handle this in checkPrecedenceGroup. Also add some asserts while I'm here. rdar://75248642
1 parent 4c7059f commit 779276a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

include/swift/AST/Decl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6977,6 +6977,11 @@ class PrecedenceGroupDecl : public Decl {
69776977
return HigherThanLoc;
69786978
}
69796979

6980+
/// Retrieve the array of \c Relation objects containing those precedence
6981+
/// groups with higher precedence than this precedence group.
6982+
///
6983+
/// The elements of this array may be invalid, in which case they will have
6984+
/// null \c PrecedenceGroupDecl elements.
69806985
ArrayRef<Relation> getHigherThan() const {
69816986
return { getHigherThanBuffer(), NumHigherThan };
69826987
}
@@ -6994,6 +6999,11 @@ class PrecedenceGroupDecl : public Decl {
69946999
return LowerThanLoc;
69957000
}
69967001

7002+
/// Retrieve the array of \c Relation objects containing those precedence
7003+
/// groups with lower precedence than this precedence group.
7004+
///
7005+
/// The elements of this array may be invalid, in which case they will have
7006+
/// null \c PrecedenceGroupDecl elements.
69977007
ArrayRef<Relation> getLowerThan() const {
69987008
return { getLowerThanBuffer(), NumLowerThan };
69997009
}

lib/Sema/TypeCheckAccess.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,12 @@ class DeclAvailabilityChecker : public DeclVisitor<DeclAvailabilityChecker> {
17971797
void checkPrecedenceGroup(const PrecedenceGroupDecl *PGD,
17981798
const Decl *refDecl, SourceLoc diagLoc,
17991799
SourceRange refRange) {
1800+
// Bail on invalid predence groups. This can happen when the user spells a
1801+
// relation element that doesn't actually exist.
1802+
if (!PGD) {
1803+
return;
1804+
}
1805+
18001806
const SourceFile *SF = refDecl->getDeclContext()->getParentSourceFile();
18011807
ModuleDecl *M = PGD->getModuleContext();
18021808
if (!SF->isImportedImplementationOnly(M))

lib/Serialization/Serialization.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,10 +3158,14 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
31583158
auto associativity = getRawStableAssociativity(group->getAssociativity());
31593159

31603160
SmallVector<DeclID, 8> relations;
3161-
for (auto &rel : group->getHigherThan())
3161+
for (auto &rel : group->getHigherThan()) {
3162+
assert(rel.Group && "Undiagnosed invalid precedence group!");
31623163
relations.push_back(S.addDeclRef(rel.Group));
3163-
for (auto &rel : group->getLowerThan())
3164+
}
3165+
for (auto &rel : group->getLowerThan()) {
3166+
assert(rel.Group && "Undiagnosed invalid precedence group!");
31643167
relations.push_back(S.addDeclRef(rel.Group));
3168+
}
31653169

31663170
unsigned abbrCode = S.DeclTypeAbbrCodes[PrecedenceGroupLayout::Code];
31673171
PrecedenceGroupLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: not %target-swift-frontend -typecheck %s
2+
3+
precedencegroup MyOperatorPrecedence {
4+
lowerThan: MultiplicationPrecedence
5+
higherThan: AdditionPrec
6+
associativity: left
7+
assignment: false
8+
}

0 commit comments

Comments
 (0)