Skip to content

[Parser] Make parser aware of the unexpected attributes in operator declarations #65224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ ERROR(operator_decl_no_fixity,none,
"operator must be declared as 'prefix', 'postfix', or 'infix'", ())
ERROR(operator_decl_expected_precedencegroup, none,
"expected precedence group name after ':' in operator declaration", ())

ERROR(operator_decl_should_not_contain_other_attributes, none,
"unexpected attribute %0 in operator declaration", (StringRef))
WARNING(operator_decl_remove_designated_types,none,
"designated types are no longer used by the compiler; please remove "
"the designated type list from this operator declaration", ())
Expand Down
11 changes: 11 additions & 0 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4606,6 +4606,17 @@ static void diagnoseOperatorFixityAttributes(Parser &P,
if (fixityAttrs.empty()) {
P.diagnose(OD->getOperatorLoc(), diag::operator_decl_no_fixity);
}
for (auto it = Attrs.begin(); it != Attrs.end(); ++it) {
if (isFixityAttr(*it) || (*it)->getKind() == DAK_RawDocComment) {
continue;
}
auto *attr = *it;
P.diagnose(attr->getLocation(),
diag::operator_decl_should_not_contain_other_attributes,
attr->getAttrName())
.fixItRemove(attr->getRange());
attr->setInvalid();
}
}
// Infix operator is only allowed on operator declarations, not on func.
else if (isa<FuncDecl>(D)) {
Expand Down
2 changes: 1 addition & 1 deletion test/attr/accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ duplicateAttr(1) // expected-error{{argument passed to call that takes no argume

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

private typealias MyInt = Int

Expand Down
2 changes: 1 addition & 1 deletion test/attr/attr_dynamic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct NotObjCAble {

@objc class ObjCClass {}

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

class Foo {
@objc dynamic init() {}
Expand Down