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: invalid typeparameters code can be compiled #49059

Closed
YoshikiShibata opened this issue Oct 19, 2021 · 2 comments
Closed

cmd/compile: invalid typeparameters code can be compiled #49059

YoshikiShibata opened this issue Oct 19, 2021 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@YoshikiShibata
Copy link

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

$ go version
go version devel go1.18-4d550727f8 Mon Oct 18 16:33:42 2021 +0000 darwin/arm64

Does this issue reproduce with the latest release?

Yes, with the tip version, not G1.17

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

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/yoshiki.shibata/Library/Caches/go-build"
GOENV="/Users/yoshiki.shibata/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/yoshiki.shibata/gocode/pkg/mod"
GONOPROXY="github.com/kouzoh/*"
GONOSUMDB="github.com/kouzoh/*"
GOOS="darwin"
GOPATH="/Users/yoshiki.shibata/gocode"
GOPRIVATE="github.com/kouzoh/*"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/yoshiki.shibata/tools/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/yoshiki.shibata/tools/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="devel go1.18-4d550727f8 Mon Oct 18 16:33:42 2021 +0000"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5y/6k1nfy3n0sn9h4g61bp36wv40000gp/T/go-build1538714687=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version devel go1.18-4d550727f8 Mon Oct 18 16:33:42 2021 +0000 darwin/arm64
GOROOT/bin/go tool compile -V: compile version devel go1.18-4d550727f8 Mon Oct 18 16:33:42 2021 +0000
uname -v: Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:20 PDT 2021; root:xnu-7195.141.6~3/RELEASE_ARM64_T8101
ProductName:	macOS
ProductVersion:	11.6
BuildVersion:	20G165
lldb --version: lldb-1300.0.32.2
Swift version 5.5-dev

What did you do?

Tried to compile the following code:

// This type is INVALID.
type P[T1, T2 any] struct {
	F *P[T2, T1] // INVALID; must be [T1, T2]
}

What did you expect to see?

Compile error

What did you see instead?

No compile error

Type Parameters Proposal says:

A generic type can refer to itself in cases where a type can ordinarily refer to itself, but when it does so the type arguments must be the type parameters, listed in the same order. This restriction prevents infinite recursion of type instantiation.

// List is a linked list of values of type T.
type List[T any] struct {
	next *List[T] // this reference to List[T] is OK
	val  T
}

// This type is INVALID.
type P[T1, T2 any] struct {
	F *P[T2, T1] // INVALID; must be [T1, T2]
}
@ALTree ALTree changed the title Generics: Invalid code can be compiled cmd/compile: invalid code can be compiled Oct 19, 2021
@ALTree ALTree changed the title cmd/compile: invalid code can be compiled cmd/compile: invalid typeparameters code can be compiled Oct 19, 2021
@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 19, 2021
@ALTree ALTree added this to the Go1.18 milestone Oct 19, 2021
@cuonglm
Copy link
Member

cuonglm commented Oct 19, 2021

This seems only matters when type parameters have different constraints:

package p

type I interface {
	int
}

// This type is INVALID.
type P[T1 I, T2 any] struct {
	F *P[T2, T1] // INVALID; must be [T1, T2]
}

fails with:

rpgo:9:7: T2 has no constraints

@findleyr
Copy link
Contributor

Thank you for the report.

We're tracking this in #48098, so closing as a dupe.

CC @griesemer @mdempsky

@golang golang locked and limited conversation to collaborators Oct 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants