Skip to content

cmd/compile: cannot return a value even its type subjects to type constraints  #51501

@changkun

Description

@changkun

Does this issue reproduce with the latest release?

Yes

What did you do?

https://go.dev/play/p/pCzjLWAepvO?v=gotip

package main

func foo[T ~int]() T {
	x := 42
	return T(x) // OK
}

func foo2[T ~int]() T {
	x := 42
	return int(x) // ERROR: cannot use int(x) (value of type int) as type T in return statement
}

func main() {}

What did you expect to see?

Compile pass.

What did you see instead?

Compile error.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions