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: wording about when len(s)/cap(s) are constant doesn't match implementations #56826

Open
mdempsky opened this issue Nov 17, 2022 · 1 comment
Assignees
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@mdempsky
Copy link
Member

mdempsky commented Nov 17, 2022

The spec (https://go.dev/ref/spec#Length_and_capacity) says:

The expressions len(s) and cap(s) are constants if the type of s is an array or pointer to an array and the expression s does not contain channel receives or (non-constant) function calls; in this case s is not evaluated.

However, this doesn't precisely match what implementations allow.

For example, this package compiles successfully with Go 1.4 and gccgo 12.2.1, even though each len argument (syntactically) contains a channel receive and/or non-constant function call:

package p

import "unsafe"

const c = len([...]any{func() { <-(chan int)(nil); panic(0) }})

const d = len([...]any{unsafe.Sizeof(<-(chan int)(nil))})

const e = len([...]any{unsafe.Sizeof(new(int))})

type any interface{} // for Go 1.4

go/types allows declaration c, but rejects d and e. The same applies to recent cmd/compile using types2.

/cc @griesemer @ianlancetaylor

@mdempsky mdempsky added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Nov 17, 2022
@mdempsky mdempsky added this to the Backlog milestone Nov 17, 2022
@griesemer griesemer self-assigned this Nov 18, 2022
@beoran
Copy link

beoran commented Nov 21, 2022

All three of c d and e seem pointless. I would say the spec is correct and the compilers are wrong in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants