Skip to content

[QoI] improve diagnostics for operator declarations; unify parsing code #4628

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 1 commit into from
Sep 6, 2016

Conversation

jtbandes
Copy link
Contributor

@jtbandes jtbandes commented Sep 5, 2016

Improves diagnostics for invalid operator declarations, particularly when specifying precedence for a non-infix operator. (Prompted by @erica's post about operators!)

Along the way, cleans up code that was nearly identical between parseDeclOperator{Infix,Prefix,Postfix}(), replacing them with a single function named parseDeclOperatorImpl().

Before:

error: consecutive statements on a line must be separated by ';'
postfix operator +? : DefaultPrecedence
                   ^
                   ;
error: expected expression
postfix operator +? : DefaultPrecedence
                    ^

After:

error: only infix operators may declare a precedence
postfix operator +? : DefaultPrecedence
                    ^~~~~~~~~~~~~~~~~~~

@jtbandes
Copy link
Contributor Author

jtbandes commented Sep 5, 2016

@swift-ci please smoke test

}
bool isPrefix = Attributes.hasAttribute<PrefixAttr>();
bool isInfix = Attributes.hasAttribute<InfixAttr>();
bool isPostfix = Attributes.hasAttribute<PostfixAttr>();
Copy link
Member

@rintaro rintaro Sep 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this PR or later, I suggest to diagnose multiple fixture here.
Currently, postfix infix operator ++++ is accepted.

Although prefix postfix combinations are diagnosed in parseNewDeclAttribute,
we should not simply add DAK_Infix there, because that will emit superfluous diagnostic for func decl:

error: attribute 'infix' cannot be combined with this attribute
infix prefix func ++(x: Int, y: Int) {}
      ^
error: 'infix' modifier is not required or allowed on func declarations
infix prefix func ++(x: Int, y: Int) {}
^~~~~~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could probably handle DAK_Infix there, if you made it so that infix is forgotten, rather than kept, after emitting the 'infix' modifier is not required or allowed on func declarations error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If 'infix' modifier is not required or allowed on func declarations' is emitted early enough, marking it invalid will prevent later checks from seeing it. It's fine for that to be a follow-up PR.

@DougGregor
Copy link
Member

This LGTM; let's address @rintaro 's comments as a follow-up.

@DougGregor DougGregor merged commit 250e6ed into swiftlang:master Sep 6, 2016
@jtbandes jtbandes deleted the diag-operator branch September 6, 2016 15:51
kateinoigakukun added a commit that referenced this pull request Aug 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants