Skip to content

Commit 3cabe08

Browse files
committed
[Parser] Make parser aware of the unexpected attributes
1 parent 4e71efc commit 3cabe08

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
@@ -4606,6 +4606,17 @@ static void diagnoseOperatorFixityAttributes(Parser &P,
46064606
if (fixityAttrs.empty()) {
46074607
P.diagnose(OD->getOperatorLoc(), diag::operator_decl_no_fixity);
46084608
}
4609+
for (auto it = Attrs.begin(); it != Attrs.end(); ++it) {
4610+
if (isFixityAttr(*it) || (*it)->getKind() == DAK_RawDocComment) {
4611+
continue;
4612+
}
4613+
auto *attr = *it;
4614+
P.diagnose(attr->getLocation(),
4615+
diag::operator_decl_should_not_contain_other_attributes,
4616+
attr->getAttrName())
4617+
.fixItRemove(attr->getRange());
4618+
attr->setInvalid();
4619+
}
46094620
}
46104621
// Infix operator is only allowed on operator declarations, not on func.
46114622
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)