Skip to content

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

@YoshikiShibata

Description

@YoshikiShibata

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions