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

cmd/compile: slicing rules too restrictive for generic slice operands #49566

Closed
griesemer opened this issue Nov 12, 2021 · 4 comments
Closed

cmd/compile: slicing rules too restrictive for generic slice operands #49566

griesemer opened this issue Nov 12, 2021 · 4 comments
Labels
NeedsFix
Milestone

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented Nov 12, 2021

Reminder issue.

Currently not permitted (no structural type):

func _[S []byte|string](s S, i, j int) S {
   return s[i : j]
}

A little bit tricky to get right because we sometimes have to preserve the string type.

@griesemer griesemer added the NeedsFix label Nov 12, 2021
@griesemer griesemer added this to the Go1.18 milestone Nov 12, 2021
@griesemer griesemer self-assigned this Nov 12, 2021
@go101
Copy link

@go101 go101 commented Nov 13, 2021

Is this the same problem?

func Entry[T []int | map[int]int](c T, i int) int {
	return c[i] // invalid operation: cannot index c (variable of type T constrained by sliceOrMap
}

@griesemer
Copy link
Contributor Author

@griesemer griesemer commented Nov 13, 2021

Not quite the same problem (simple indexing rules are more relaxed) but similar: we don't allow mixing maps with non-map types for indexing. Maps just behave very differently than slices. Also, we don't have strong reasons (yet) that suggest that this must be permitted.

We are aware that per the generics proposal this could or even "should" work. But the generics proposal is not very specific with the rule details. For 1.18 and the spec (in the process of being written) we need to be very specific.

In short, for 1.18 we play it somewhat conservative. It's easy to relax the rules down the road, but impossible to change if we make a mistake by being overly broad. Thanks.

@go101
Copy link

@go101 go101 commented Nov 13, 2021

The proposal does provide such an example, but I think now the proposal has been outdated.

OK, I will submit a proposal to support this.

@gopherbot
Copy link

@gopherbot gopherbot commented Nov 13, 2021

Change https://golang.org/cl/363662 mentions this issue: cmd/compile/internal/types2: allow slicing for operands with []byte|string type sets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix
Projects
None yet
Development

No branches or pull requests

3 participants