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: clarify int type used for index values of range clauses #18910

Closed
davecheney opened this issue Feb 3, 2017 · 5 comments
Closed

spec: clarify int type used for index values of range clauses #18910

davecheney opened this issue Feb 3, 2017 · 5 comments

Comments

@davecheney
Copy link
Contributor

davecheney commented Feb 3, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version devel +64c5280 Fri Feb 3 04:40:36 2017 +0000 linux/amd64

What operating system and processor architecture are you using (go env)?

linux/amd64

What did you do?

package main

import "fmt"

func main() {
        type int = int32
        var x int = 3
        for i := range []int{1, 2, 3} {
                fmt.Println(i == x)
        }
}

What did you expect to see?

false
false
false

What did you see instead?

# command-line-arguments
./alias.go:9: invalid operation: i == x (mismatched types int and int32)

https://golang.org/ref/spec#For_statements says that the type of the index variable is int, but it looks like this is the universe declaration of int, not the current alias.

@ianlancetaylor
Copy link
Contributor

I think this is a spec bug. I think it's correct for the for statement to use the universe declaration of int.

It's not an aliasing issue as such. Your argument works just as well for type int int32 as it does for type int = int32.

@davecheney
Copy link
Contributor Author

Your argument works just as well for type int int32 as it does for type int = int32.

Good point, well spotted.

@griesemer griesemer changed the title cmd/compile: aliasing universe types does not affect range index value spec: clarify int type used for index values of range clauses Feb 3, 2017
@griesemer griesemer self-assigned this Feb 3, 2017
@griesemer
Copy link
Contributor

griesemer commented Feb 3, 2017

@ianlancetaylor Agreed. Changed title accordingly. That said, it's not clear to me what the best way to address this is in the spec. There's a lot of places where we talk about type 'int' and we always mean universe's 'int' no matter what kind of shadowing declarations of 'int' are active.

@griesemer griesemer added this to the Go1.9Maybe milestone Feb 3, 2017
@griesemer griesemer added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Feb 3, 2017
@mdempsky
Copy link
Member

mdempsky commented Feb 3, 2017

Perhaps a paragraph under "Predeclared identifiers" mentioning that whenever the spec names a predeclared identifier, that it refers to the universal declaration?

@griesemer griesemer removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Feb 16, 2017
@griesemer
Copy link
Contributor

I've looked at this a bit. Adding a paragraph to the "Predeclared identifiers" section may help if one reads the spec sequentially and keeps that paragraph in mind at all times.

But that's not what people do when they have a specific question. Instead they look for the respective section in the spec. The clarification needs to be in those sections, if at all.

There's many mentions of the type int, and many other types as well (rune, string, etc.), all over the spec. We don't want to sprinkle the text with "predeclared int type" etc. Another option might be to link all these types to the "Predeclared identifiers" section. That will only help if people click on the link.

But more generally, we've now gone for almost 8 years and this has never come up as a problem: Readers tend to think of the predeclared types when we mention int type, string type, etc. It really only leads to confusion if people define their own names that happen to shadow predeclared identifiers and even that it needs a somewhat contrived example (as is the case in the example used for the issue).

We have a more wider issue related to confusing nomenclature. Closing this one in favor of #4532.

@golang golang locked and limited conversation to collaborators Feb 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants