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

Open
mmrath opened this issue Sep 14, 2021 · 6 comments
Open

cmd/compile: inconsistent generics behaviour #48389

mmrath opened this issue Sep 14, 2021 · 6 comments

Comments

@mmrath
Copy link

@mmrath 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

@randall77 randall77 commented Sep 16, 2021

@cagedmantis cagedmantis added this to the Go1.18 milestone Sep 16, 2021
@griesemer griesemer self-assigned this Sep 16, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Sep 16, 2021

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

@toothrot
Copy link
Contributor

@toothrot toothrot commented Sep 22, 2021

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

@griesemer
Copy link
Contributor

@griesemer griesemer commented Sep 22, 2021

This will be worked on soon.

@OneOfOne
Copy link
Contributor

@OneOfOne 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

@rogpeppe rogpeppe commented Oct 11, 2021

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants