Skip to content

Commit 1511afc

Browse files
committed
[Parser] Improving diagnostics for access-level modifiers
Improves the diagnostics for situations where multiple access-level modifiers are used on the same declaration.
1 parent 0f7eb45 commit 1511afc

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,10 @@ ERROR(duplicate_attribute,none,
14851485
"duplicate %select{attribute|modifier}0", (bool))
14861486
NOTE(previous_attribute,none,
14871487
"%select{attribute|modifier}0 already specified here", (bool))
1488+
ERROR(multiple_access_level_modifiers,none,
1489+
"multiple access-level modifiers specified", ())
1490+
NOTE(previous_access_level_modifier,none,
1491+
"'%0' previously specified here", (StringRef))
14881492
ERROR(mutually_exclusive_attrs,none,
14891493
"'%0' contradicts previous %select{attribute|modifier}2 '%1'", (StringRef, StringRef, bool))
14901494

lib/Parse/ParseDecl.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,9 +2926,25 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
29262926
if (!Tok.is(tok::l_paren)) {
29272927
// Normal access control attribute.
29282928
AttrRange = Loc;
2929-
DuplicateAttribute = Attributes.getAttribute<AccessControlAttr>();
2930-
if (!DuplicateAttribute)
2929+
2930+
auto PrevAccessControlAttr = Attributes.getAttribute<AccessControlAttr>();
2931+
2932+
if (!PrevAccessControlAttr) {
29312933
Attributes.add(new (Context) AccessControlAttr(AtLoc, Loc, access));
2934+
} else {
2935+
// Diagnose if there is already an access control attribute on
2936+
// this declaration.
2937+
diagnose(Loc, diag::multiple_access_level_modifiers)
2938+
.highlight(AttrRange);
2939+
diagnose(PrevAccessControlAttr->getLocation(),
2940+
diag::previous_access_level_modifier,
2941+
PrevAccessControlAttr->getAttrName())
2942+
.highlight(PrevAccessControlAttr->getRange());
2943+
2944+
// Removes the reference to the duplicate attribute as we don't need the
2945+
// extra diagnostic.
2946+
DuplicateAttribute = nullptr;
2947+
}
29322948
break;
29332949
}
29342950

0 commit comments

Comments
 (0)