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

spec: unclear if shift string(1 << s) should be invalid #45114

Open
griesemer opened this issue Mar 18, 2021 · 4 comments
Open

spec: unclear if shift string(1 << s) should be invalid #45114

griesemer opened this issue Mar 18, 2021 · 4 comments
Assignees
Milestone

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented Mar 18, 2021

package p
var s uint
var _ = string(1 << s)

is not accepted by the compiler:

bug.go:3:15: invalid operation: 1 << s (shift of type string)

Per the spec (https://golang.org/ref/spec#Constant_expressions):
"If the left operand of a non-constant shift expression is an untyped constant, it is first implicitly converted to the type it would assume if the shift expression were replaced by its left operand alone."

In this case, the left operand would be converted to int (not string). This is different from cases such as, say float64(1 << s) where 1 is implicitly converted to 1.0 because it can be done without loss of precision. It seems a bit far-fetched to claim that a 1 can be converted to a string without loss of precision. (That said, the spec is notoriously difficult to decipher with respect to shifts and implicit conversions.)

As an aside, there's still #3939 which is proposing to prohibit string(int_value), and go vet is checking for this. So we're not permitting a new category of programs by making string(1 << s) valid.

Both go/types and types2 accept this code.

The test $GOROOT/test/fixedbugs/bug193.go assumes that this code should fail.

@griesemer griesemer added this to the Go1.17 milestone Mar 18, 2021
@griesemer griesemer self-assigned this Mar 18, 2021
@griesemer
Copy link
Contributor Author

@griesemer griesemer commented Mar 18, 2021

cc: @findleyr

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 19, 2021

My vote is to change the spec, since cmd/compile, gccgo, and GoLLVM all reject this. I'm assuming that nobody is clamoring for this to work.

@griesemer
Copy link
Contributor Author

@griesemer griesemer commented Mar 19, 2021

Fair enough. If anything, we need to clarify the spec in that regard.

@griesemer griesemer changed the title cmd/compile: valid shift string(1 << s) not accepted spec: unclear if shift string(1 << s) should be invalid Mar 19, 2021
@griesemer griesemer modified the milestones: Go1.17, Backlog Mar 19, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 19, 2021

Change https://golang.org/cl/303094 mentions this issue: test: enable fixedbugs/bug193.go for -G compiler option

gopherbot pushed a commit that referenced this issue Mar 23, 2021
Temporarily disable a questionable test case in fixedbugs/bug193.go
and enable the test as a whole. See the issues below for details.

Updates #45114.
Updates #45117.

Change-Id: I1de6f8d79b592eeeec139cd92b6c9cac56a9a74b
Reviewed-on: https://go-review.googlesource.com/c/go/+/303094
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants