Skip to content

Commit caeab58

Browse files
authored
Merge pull request #65224 from StevenWong12/diagnositic_on_modifiers_in_operatordecl
[Parser] Make parser aware of the unexpected attributes in operator declarations
2 parents e9f847d + 3cabe08 commit caeab58

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ ERROR(operator_decl_no_fixity,none,
461461
"operator must be declared as 'prefix', 'postfix', or 'infix'", ())
462462
ERROR(operator_decl_expected_precedencegroup, none,
463463
"expected precedence group name after ':' in operator declaration", ())
464-
464+
ERROR(operator_decl_should_not_contain_other_attributes, none,
465+
"unexpected attribute %0 in operator declaration", (StringRef))
465466
WARNING(operator_decl_remove_designated_types,none,
466467
"designated types are no longer used by the compiler; please remove "
467468
"the designated type list from this operator declaration", ())

lib/Parse/ParseDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4559,6 +4559,17 @@ static void diagnoseOperatorFixityAttributes(Parser &P,
45594559
if (fixityAttrs.empty()) {
45604560
P.diagnose(OD->getOperatorLoc(), diag::operator_decl_no_fixity);
45614561
}
4562+
for (auto it = Attrs.begin(); it != Attrs.end(); ++it) {
4563+
if (isFixityAttr(*it) || (*it)->getKind() == DAK_RawDocComment) {
4564+
continue;
4565+
}
4566+
auto *attr = *it;
4567+
P.diagnose(attr->getLocation(),
4568+
diag::operator_decl_should_not_contain_other_attributes,
4569+
attr->getAttrName())
4570+
.fixItRemove(attr->getRange());
4571+
attr->setInvalid();
4572+
}
45624573
}
45634574
// Infix operator is only allowed on operator declarations, not on func.
45644575
else if (isa<FuncDecl>(D)) {

test/attr/accessibility.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ duplicateAttr(1) // expected-error{{argument passed to call that takes no argume
102102

103103
// CHECK ALLOWED DECLS
104104
private import Swift // expected-error {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport}}
105-
private(set) infix operator ~~~ // expected-error {{'private' modifier cannot be applied to this declaration}} {{1-14=}}
105+
private(set) infix operator ~~~ // expected-error {{unexpected attribute private in operator declaration}} {{1-14=}}
106106

107107
private typealias MyInt = Int
108108

test/attr/attr_dynamic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct NotObjCAble {
77

88
@objc class ObjCClass {}
99

10-
dynamic prefix operator +!+ // expected-error{{'dynamic' modifier cannot be applied to this declaration}} {{1-9=}}
10+
dynamic prefix operator +!+ // expected-error{{unexpected attribute dynamic in operator declaration}} {{1-9=}}
1111

1212
class Foo {
1313
@objc dynamic init() {}

0 commit comments

Comments
 (0)