Skip to content

Commit 19dd7fd

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

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4606,6 +4606,18 @@ static void diagnoseOperatorFixityAttributes(Parser &P,
46064606
if (fixityAttrs.empty()) {
46074607
P.diagnose(OD->getOperatorLoc(), diag::operator_decl_no_fixity);
46084608
}
4609+
4610+
for (auto it = Attrs.begin(); it != Attrs.end(); ++it) {
4611+
if (isFixityAttr(*it)) {
4612+
continue;
4613+
}
4614+
auto *attr = *it;
4615+
P.diagnose(attr->getLocation(),
4616+
diag::operator_decl_should_not_contain_other_attributes,
4617+
attr->getAttrName())
4618+
.fixItRemove(attr->getRange());
4619+
attr->setInvalid();
4620+
}
46094621
}
46104622
// Infix operator is only allowed on operator declarations, not on func.
46114623
else if (isa<FuncDecl>(D)) {

test/Parse/operator_decl.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,5 @@ infix operator *<*< : F, Proto
117117

118118
// expected-error@+2 {{expected precedence group name after ':' in operator declaration}}
119119
postfix operator ++: // expected-error {{only infix operators may declare a precedence}} {{20-21=}}
120+
121+
dynamic prefix operator +!+ // expected-error{{unexpected attribute 'dynamic' in operator declaration}} {{1-9=}}

test/attr/attr_dynamic.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ struct NotObjCAble {
77

88
@objc class ObjCClass {}
99

10-
dynamic prefix operator +!+ // expected-error{{'dynamic' modifier cannot be applied to this declaration}} {{1-9=}}
11-
1210
class Foo {
1311
@objc dynamic init() {}
1412
@objc dynamic init(x: NotObjCAble) {} // expected-error{{method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C}} expected-note{{Swift structs cannot be represented in Objective-C}}

0 commit comments

Comments
 (0)