Skip to content

cmd/compile: constraint type inference for generic types is not implemented #54469

Closed
@MrFox131

Description

@MrFox131

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

$ go version
go version go1.19 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mrfox131/.cache/go-build"
GOENV="/home/mrfox131/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mrfox131/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mrfox131/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4107873395=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/e4HeQf10djz

What did you expect to see?

According to https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#pointer-method-example
I expected to see that constraint type inference will work for function type definitions too/

What did you see instead?

Constraint type inference does not work for type definitions where type is some function/

Activity

MrFox131

MrFox131 commented on Aug 16, 2022

@MrFox131
Author

Title format was not really clear for me, so I apologize for the inappropriate title.

changed the title [-]affected: Generics feature[/-] [+]cmd/compile: constraint type inference for type definition fails[/+] on Aug 16, 2022
added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Aug 16, 2022
MrFox131

MrFox131 commented on Aug 17, 2022

@MrFox131
Author

To be honest struct was not meant in docs, but I found out, that type inference for structs does not work too

https://go.dev/play/p/5JcVfw2zxxN

ianlancetaylor

ianlancetaylor commented on Aug 17, 2022

@ianlancetaylor
Contributor

In Go 1.19 constraint type inference only applies to functions, not types. See https://go.dev/cl/404875. We can leave this issue open as a request to try to make this work in the future.

CC @griesemer @findleyr

added this to the Backlog milestone on Aug 17, 2022
added
genericsIssue is related to generics
TypeInferenceIssue is related to generic type inference
and removed
compiler/runtimeIssues related to the Go compiler and/or runtime.
on Aug 17, 2022
MrFox131

MrFox131 commented on Aug 17, 2022

@MrFox131
Author

Thanks for the reply. I think if this is implemented it will be much more logical than type inference only for functions. Most likely I will try to do this myself, I am already reading the guideline.

griesemer

griesemer commented on Aug 17, 2022

@griesemer
Contributor

It turns out that type inference for types is much more difficult than type inference for functions. A function is not a type and thus cannot be used to instantiate another function or type (nor does it need to be inferred). But a type may be used to instantiate another type, and possibly itself. On top of that, if we combine this with type inference, there are all kinds of cycle problems that we currently can't handle correctly (and don't fully understand), and which may require significant (re-)engineering of the type checker. And it's not even clear that it's desirable as it may make code really difficult to read.

changed the title [-]cmd/compile: constraint type inference for type definition fails[/-] [+]cmd/compile: constraint type inference for generic types is not implemented[/+] on Aug 17, 2022
removed
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Aug 17, 2022
added
NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
on Aug 19, 2022
griesemer

griesemer commented on May 16, 2023

@griesemer
Contributor

We don't plan to do this. Closing.

locked and limited conversation to collaborators on May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.TypeInferenceIssue is related to generic type inferencegenericsIssue is related to generics

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ianlancetaylor@griesemer@gopherbot@joedian@MrFox131

        Issue actions

          cmd/compile: constraint type inference for generic types is not implemented · Issue #54469 · golang/go