Skip to content
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

Prevent adjacent unary operators from merging #328

Closed
wants to merge 1 commit into from

Conversation

nreid260
Copy link
Contributor

@nreid260 nreid260 commented Jun 8, 2022

No description provided.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 8, 2022
@facebook-github-bot
Copy link
Contributor

@strulovich has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Comment on lines 2849 to 2851
| +-a
| -+a
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be + -a and - +a? same below for +--a

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The intent was to test that the extra space is only added when it when two operators would merge.

We could also add a space here, but I'm not sure it makes sense for other combos.

Copy link
Contributor

Choose a reason for hiding this comment

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

I probably misunderstand - do we want

var x = 5 + -a

or

var x = 5 +-a

?

or did I totally misunderstand?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's for unary operators, so the example would be:

var a = 1
println(+-a) // -1

I agree no human would ever write any of these examples, but a code generators write at least some of them.

Copy link
Contributor

Choose a reason for hiding this comment

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

got it!

visit(baseExpression)
if (baseExpression is KtPostfixExpression &&
baseExpression.operationReference.text.last() == operator.first()) {
builder.space()
Copy link
Contributor

Choose a reason for hiding this comment

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

is this exercised in any test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not currently. The only example I can think of would be a!! !!, but a!!!! has the same semantics. Do you have a preference on that case?

Comment on lines 2849 to 2851
| +-a
| -+a
Copy link
Contributor

Choose a reason for hiding this comment

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

got it!


builder.token(operator)
if (baseExpression is KtPrefixExpression &&
operator.last() == baseExpression.operationReference.text.first()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this relies on the fact that there are no operators with a mixture of characters, right?
I'm wondering if we should always put a space instead of guessing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That might be better? I like not guessing.

How do you feel about these cases:

a--!!
!--a

Copy link
Contributor

Choose a reason for hiding this comment

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

I hate Kotlin :)
let's add these test-cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@cgrushko
Copy link
Contributor

cgrushko commented Jun 8, 2022

Otherwise looks good, thanks!

@facebook-github-bot
Copy link
Contributor

@cgrushko has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@cgrushko has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@nreid260 nreid260 deleted the unary_ops branch May 11, 2023 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants