Skip to content

cmd/compile: the rule is too restricted when ranging over values of type parameters  #49551

@zigo101

Description

@zigo101

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

$ go version
go version devel go1.18-23adc139bf Fri Nov 12 14:56:58 2021 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes, the latest tip.

What did you do?

package main

func Split[T []byte|string](s T, x byte) (a, b T) {
	var k = 0
	for ; k < len(s); k++ {
		if s[k] == x {
			goto End
		}
	}
	
	a = s
	return

End:
	return s[:k], s[k+1:] // invalid operation: cannot slice s (variable of type T constrained by []byte|string): T has no structural type
}

type MyByte byte
func Foo[T []byte|[]MyByte](s T) {
	for range s {} // cannot range over s (variable of type T constrained by []byte|[]MyByte) (T has no structural type)
} 

func Bar[T []byte|map[int]byte](s T) {
	for range s {} // cannot range over s (variable of type T constrained by []byte|map[int]byte) (T has no structural type)

}

func Duk[T []byte|string](s T) {
	for range s {} // cannot range over s (variable of type T constrained by []byte|string) (T has no structural type)

}

type Bytes []byte
func Zed[T []byte|Bytes](s T) {
	for range s {} // okay
}


func main() {}

What did you expect to see?

Compiles okay.

What did you see instead?

Some functions don't compile.

The rule is too restricted, which will limit the use scope of type parameters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions