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: inconsistent generics behaviour #48389

Closed
mmrath opened this issue Sep 14, 2021 · 7 comments
Closed

cmd/compile: inconsistent generics behaviour #48389

mmrath opened this issue Sep 14, 2021 · 7 comments
Labels
FrozenDueToAge generics Issue is related to generics NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Milestone

Comments

@mmrath
Copy link

mmrath commented Sep 14, 2021

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

$ go version
go version devel go1.18-f93a63addb Mon Sep 13 20:17:35 2021 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Latest master

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

The below programs fails to build with error whereas a similar program with string replaced with int compiles fine. Not sure if this is a bug or expected.

./main4.go:9:16: cannot convert in (variable of type parameter T constrained by Constraint) to string

package main4

type Constraint interface {
    ~string
}

func C[T Constraint](in T) {
    _ = string(in)
}

What did you expect to see?

What did you see instead?

@randall77
Copy link
Contributor

@griesemer @findleyr

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 16, 2021
@cagedmantis cagedmantis added this to the Go1.18 milestone Sep 16, 2021
@cagedmantis cagedmantis added the generics Issue is related to generics label Sep 16, 2021
@griesemer griesemer self-assigned this Sep 16, 2021
@griesemer
Copy link
Contributor

Looks like a bug to me. There's several issues around conversions that we need to address.

@toothrot
Copy link
Contributor

Checking in on this issue as it's labeled a release blocker for Go 1.18. Is there any update?

@griesemer
Copy link
Contributor

This will be worked on soon.

@OneOfOne
Copy link
Contributor

OneOfOne commented Oct 8, 2021

Also another case:

func toStrings[T ~string|~[]byte](in []T) []string {
	out := make([]string, 0, len(in))
	for _, v := range in {
		out = append(out, string(v))
	}
	return out
}

@rogpeppe
Copy link
Contributor

Another issue which may be closely related. This code should work but doesn't.
I get the same error as in the description for this issue:

./tst.go:28:17: cannot convert x (variable of type parameter T constrained by I1) to Struct1

https://go2goplay.golang.org/p/JyUKVBNOFfX

type Struct1 struct {
	A int
}

type Struct2 struct {
	A int `json:"a"`
}

type I1 interface {
	Struct1 | Struct2
}

func F[T I1](x T) Struct1 {
	return Struct1(x)
}

@griesemer
Copy link
Contributor

Generic conversions are now implemented in the compiler (port to go/types is forthcoming). Closing.

@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge generics Issue is related to generics NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Projects
None yet
Development

No branches or pull requests

8 participants