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

proposal: Go 2: add a built-in tilde ~ (bitwise not) unary operator (op: OBITNOT -> OCOM), deprecate ^x (XOR) usage #46847

Closed
Dentrax opened this issue Jun 21, 2021 · 7 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@Dentrax
Copy link

Dentrax commented Jun 21, 2021

TL;DR: I propose adding built-in support for ~ (bitwise not) unary operator and deprecate of ^x (XOR) usage

Abstract
The ~ operator (pronounced as tilde) is a complement (unary) operator, performs a bitwise NOT operation. It takes one-bit operand and returns its complement. If the operand is 1, it returns 0, and if it is 0, it returns 1. When applied to numbers, represented by bitwise not, it changes the number’s sign and then subtracts one. *

For example:

  • 10101000 11101001 // Original (-22,295 in 16-bit one's complement)
  • 01010111 00010110 // ~Original (22,294 in 16-bit one's complement)

I propose the addition of a new built-in ~ operator.

Motivation
Currently, AFAIK, go1.16.4 does not have support for ~ operator. I could find neither an actively discussing issue nor a PR. It is a great opportunity to learn compiler theory and get used to Go compiler internals.

Backgroud
Tilde ~ operator widely implemented and used by some programming languages. (i.e. Python, C/C++, Java, Rust (!), etc.). Can be used for different purposes in every programming language. We can start implementing this as a unary operator. In the some programming languages, the tilde character is used as a bitwise NOT operator, following the notation in logic: ~p not means not p, where p is a proposition.

Proposal
I propose that ~ should work like any other bitwise operator as described in the spec. We will only support prefix notation usage on integers. i.e. ~EXPR . Following codes should compile successfully, as ^ already does.

  • ~1997 // returns -1998
  • a := 7 and ~a // returns -8
  • ~-15 // returns 14
  • -~15 // returns 16
  • foo := func(x int) int { return ~x } and ~foo(707) // returns -708
  • if ~7 < 5 { // true }
  • ~3.1415 // INVALID
  • x ~= y // INVALID
  • ~bool // INVALID
  • 5~7 // INVALID
  • 7~ // INVALID

Compatibility
We should no longer allow ^EXPR usage. To be more aligned with the usage of the unary syntax just like in other languages. Which will cause language change.

Complexity
Since we will add just another single unary operator to the compiler, I think it will not cause even the tiny complexity. The compiler already supports other bitwise operations. (i.e. &, | ,^, &^), and ^ for bitwise not. Especially OBITNOT operator in the compiler.

Implementation
At a minimum this will impact:

  • spec: add an operator to the relevant part
  • compiler frontend: add new Token, update Scanner and Parser
  • compiler backend: replace OBITNOT OpCode to OCOM, update SSA and GC
  • optimization: i.e. ~(~7) = 7 (already implemented)
  • docs: the tour, all educational resources

Furthermore
After implementation for integers, we may also add different features for this operator, in the future:

  • indicating type equality or lazy pattern match
  • object comparison
  • array concatenation
  • … and so on.

Additional Notes

I'm trying to write a new blog post about "How to contribute to Go Compiler". What I want to demonstrate is how can we add a new operator, how to write a discussion proposal, how to contribute to Go. Your feedbacks are valuable. Thanks.

@gopherbot gopherbot added this to the Proposal milestone Jun 21, 2021
@seankhliao seankhliao added v2 A language change or incompatible library change LanguageChange labels Jun 21, 2021
@seankhliao
Copy link
Member

Please fill out the language change template

This seems like a very big change for no discernible benefit? note the ~ operator will be used for #45346

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 21, 2021
@ianlancetaylor
Copy link
Contributor

You seem to be suggesting that we write ^x as ~x. We aren't going to do that. There is nothing wrong with ^x. There is nothing wrong with ~x either, but changing now would cost far more than any possible benefit.

@ianlancetaylor ianlancetaylor changed the title proposal: spec: add a built-in tilde ~ (bitwise not) unary operator (op: OBITNOT -> OCOM), deprecate ^x (XOR) usage proposal: Go 2: add a built-in tilde ~ (bitwise not) unary operator (op: OBITNOT -> OCOM), deprecate ^x (XOR) usage Jun 21, 2021
@DeedleFake
Copy link

I'm trying to write a new blog post about "How to contribute to Go Compiler". What I want to demonstrate is how can we add a new operator, how to write a discussion proposal, how to contribute to Go. Your feedbacks are valuable. Thanks.

Do you expect this to be rejected and are only filing it in order to be able to reference it in a blog post about making proposals?

@Dentrax
Copy link
Author

Dentrax commented Jun 22, 2021

You seem to be suggesting that we write ^x as ~x. We aren't going to do that. There is nothing wrong with ^x. There is nothing wrong with ~x either, but changing now would cost far more than any possible benefit.

Exactly, I agree with this. 👍

Do you expect this to be rejected and are only filing it in order to be able to reference it in a blog post about making proposals?

Sure! I filled the proposal template to be able to reference and get feedbacks from you. One thing I'm curious about is, should we fill the Go 2 language change template for the any language changes? Even if it has not related to Go 2? I was followed the design TEMPLATE.

@ianlancetaylor
Copy link
Contributor

Yes, we expect the language change template to be filled in for any language change proposal, and this is a language change proposal.

@seankhliao
Copy link
Member

I'm going to close this as it appears that the only purpose was for the author to write a blog post (and it is unlikely to be accepted anyway).

@golang golang locked and limited conversation to collaborators Apr 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
@DeedleFake @ianlancetaylor @gopherbot @seankhliao @Dentrax and others