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: be more precise in explaining semantics of ConstSpec with missing RHS #49157

Open
go101 opened this issue Oct 25, 2021 · 6 comments
Open

spec: be more precise in explaining semantics of ConstSpec with missing RHS #49157

go101 opened this issue Oct 25, 2021 · 6 comments
Assignees
Labels
Documentation NeedsFix
Milestone

Comments

@go101
Copy link

@go101 go101 commented Oct 25, 2021

The current spec says:

Within a parenthesized const declaration list the expression list may be omitted from any but the first ConstSpec. Such an empty list is equivalent to the textual substitution of the first preceding non-empty expression list and its type if any. Omitting the list of expressions is therefore equivalent to repeating the previous list.

By the explanation, the following two declarations are equivalent but are not actually (assume they are in local blocks):

const (
    A = iota
    iota = iota
    B
    C
)
const (
    A = iota
    iota = iota
    B = iota
    C
  )

It would be better to change "textual" into "lexical".

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 26, 2021

@ianlancetaylor ianlancetaylor added Documentation NeedsFix labels Oct 26, 2021
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Oct 26, 2021
@griesemer griesemer self-assigned this Oct 26, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 26, 2021

Nice pathological case!

@robpike
Copy link
Contributor

@robpike robpike commented Oct 26, 2021

I am missing what the pathology is, and why s/textual/lexical/ makes a difference. Please fill me in.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 26, 2021

In the first example, simply repeating the last present RHS where it is omitted leads to a different result (than leaving the RHS away) because the iota on the RHS of B = iota (after manually filling it in) is the user-defined, not the built-in iota. This effect is only possible for local constant declarations; at the package level we get a cycle error due to the different scope rules at the package level (see the example for all four cases).

Agreed that I don't know how using "lexical" would make this clearer. But stating that"Such an empty list is equivalent to the textual substitution of the first preceding non-empty expression list and its type if any." seems not quite correct.

@robpike
Copy link
Contributor

@robpike robpike commented Oct 26, 2021

I just wrote,

"I still don't see the problem. You're created a new variable called iota, so of course it's different."

and in writing that saw the problem. Wow, nice corner case.

@go101
Copy link
Author

@go101 go101 commented Oct 26, 2021

In fact, this is not an iota specific issue:

package main

const X = 3

func main() {
	const (
		X = X + X
		Y
	)
	
	println(X, Y)
}

@griesemer griesemer changed the title spec: inaccuracy in iota explanation spec: be more precise in explaining semantics of ConstSpec with missing RHS Oct 26, 2021
@griesemer griesemer removed this from the Backlog milestone May 12, 2022
@griesemer griesemer added this to the Go1.19 milestone May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix
Projects
None yet
Development

No branches or pull requests

4 participants