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

go/types, types2: append within a generic function does not compile #50281

Closed
stellirin opened this issue Dec 21, 2021 · 6 comments
Closed

go/types, types2: append within a generic function does not compile #50281

stellirin opened this issue Dec 21, 2021 · 6 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@stellirin
Copy link

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

$ go version
go version go1.18beta1 linux/amd64

Does this issue reproduce with the latest release?

latest beta release

What did you do?

A generic function with a union constraint of []byte | string cannot append to a []byte.

func AppendBytes(buf []byte, s []byte) []byte {
	return append(buf, s[1:6]...)
}

func AppendString(buf []byte, s string) []byte {
	return append(buf, s[1:6]...)
}

type byteseq interface {
	string | []byte
}

// This should allow to eliminate the two functions above.
func AppendByteString[source byteseq](buf []byte, s source) []byte {
	return append(buf, s[1:6]...)
}

https://gotipplay.golang.org/p/WGG7g3HTfZ-

I expect this is a duplicate of a known issue but so far I was not able to find anytinhg. Similar functions are explicitly mentioned in the proposal: https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#composite-types-in-constraints

What did you expect to see?

The program compiles.

What did you see instead?

./prog.go:18:22: cannot use s[1:6] (value of type source constrained by byteseq) as type []byte in argument to append:
	cannot assign string (in source) to []byte
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 21, 2021
@ianlancetaylor ianlancetaylor added this to the Go1.18 milestone Dec 21, 2021
@ianlancetaylor
Copy link
Contributor

CC @griesemer @findleyr

@zhangfannie
Copy link
Contributor

zhangfannie commented Dec 21, 2021

I modified the above program, see https://gotipplay.golang.org/p/j31fyLkpZcL, it works now.

func AppendByteString[source byteseq](buf []byte, s source) []byte{
	return append(buf, []byte(s[1:6])...)
}

Because I am not familiar with type parameters, I do not know if this modification changes its function. Thank you.

@zhangfannie
Copy link
Contributor

Please ingore the above reply, the above mofication changes its function. 🙂

@aarzilli
Copy link
Contributor

Isn't this already disallowed by the current spec because byteseq doesn't have a specific type?

@findleyr findleyr changed the title append within a generic function does not compile go/types, types2: append within a generic function does not compile Dec 21, 2021
@findleyr findleyr self-assigned this Dec 21, 2021
@griesemer
Copy link
Contributor

The simple case is

package p

func _[S string|[]byte](s S) {
        var buf []byte
	_ = append(buf, s...)
}

which is currently disallowed because S has no structural type. But the spec has not been updated with respect to append, and we should have a special case here as we have for copy.

@griesemer griesemer assigned griesemer and unassigned findleyr Jan 7, 2022
@griesemer griesemer added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 7, 2022
@gopherbot
Copy link

Change https://golang.org/cl/376214 mentions this issue: cmd/compile: accept string|[]byte-constrained 2nd argument in append

jproberts pushed a commit to jproberts/go that referenced this issue Jun 21, 2022
Similarly to what we do for the built-in function `copy`,
where we allow a string as 2nd argument to append, also
permit a type parameter constrained by string|[]byte.

While at it, change date in the manual.go2 test files so
that we don't need to constantly correct it when copying
a test case from that file into a proper test file.

Fixes golang#50281.

Change-Id: I23fed66736aa07bb3c481fe97313e828425ac448
Reviewed-on: https://go-review.googlesource.com/c/go/+/376214
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
@golang golang locked and limited conversation to collaborators Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants