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: should auto dereference <pointer-to-map>[index] #44211

Closed
docwhat opened this issue Feb 10, 2021 · 4 comments
Closed

cmd/compile: should auto dereference <pointer-to-map>[index] #44211

docwhat opened this issue Feb 10, 2021 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@docwhat
Copy link

docwhat commented Feb 10, 2021

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

$ go version
go version go1.15.7 darwin/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="/Users/docwhat/Library/Caches/go-build"
GOENV="/Users/docwhat/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/docwhat/go/pkg/mod"
GOOS="darwin"
GOPATH="/Users/docwhat/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.7_1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.7_1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wq/4zytjdrx2l7f5xf6vl7k56340000gn/T/go-build573187384=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I wrote code that looked like this:

type Currency string

type Balance map[Currency]float32

func (b *Balance) Add(amount Amount) *Balance {
    current, ok := b[amount.Currency]
    if ok {
        b[amount.Currency] = current + amount.Value
    } else {
        b[amount.Currency] = amount.Value
    }
    return b
}

What did you expect to see?

I expected it to work.

What did you see instead?

prog.go:15: invalid operation: b[amount.Currency] (type *Balance does not support indexing)
prog.go:17: invalid operation: b[amount.Currency] (type *Balance does not support indexing)
prog.go:19: invalid operation: b[amount.Currency] (type *Balance does not support indexing)

Additional info

Changing the code to:

func (b *Balance) Add(amount Amount) *Balance {
    current, ok := (*b)[amount.Currency]
    if ok {
        (*b)[amount.Currency] = current + amount.Value
    } else {
        (*b)[amount.Currency] = amount.Value
    }
    return b
}

Since the compiler automatically dereferences *structs in other places, why doesn't it dereference b[key] as well? Does it not have enough information?

The source code was gotten from StackOverflow: Go: invalid operation - type *map[key]value does not support indexing. I have some similar though more complicated code.

@cherrymui
Copy link
Member

What is the type of Balance? Index operation never works on struct, or *struct.

@dmitshur dmitshur changed the title compiler should auto dereference <pointer-to-struct>[index] cmd/compile: should auto dereference <pointer-to-struct>[index] Feb 10, 2021
@dmitshur dmitshur added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 10, 2021
@docwhat
Copy link
Author

docwhat commented Feb 10, 2021

What is the type of Balance?

type Balance map[Currency]float32

Index operation never works on struct, or *struct.

It doesn't work, true. But why not? Doesn't the compiler have enough information?

@randall77
Copy link
Contributor

The compiler only implicitly dereferences on pointers to arrays, not pointers to maps (or other types, like slices). So if you had

type Balance [4]float32

it would work.

I don't think it makes sense to autodereference in this case. It is seldom the right answer to have a pointer to a map, so there's no point in providing syntactic sugar for it.
(There's no point in your Add method to take a *Balance receiver. Just take a Balance.)

@randall77 randall77 changed the title cmd/compile: should auto dereference <pointer-to-struct>[index] cmd/compile: should auto dereference <pointer-to-map>[index] Feb 10, 2021
@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Feb 10, 2021

This is not a compiler issue. This is a language issue. The language does not permit the compiler to make this automatic dereference. Automatic dereference is only defined for selectors (https://golang.org/ref/spec#Selectors) and for array (not slice) indexes (https://golang.org/ref/spec#Index_expressions). Closing this issue.

(You could file a language change proposal for this -- see https://golang.org/s/proposal -- but as pointer-to-map types are rarely used I don't think there would be much support for such a change.)

@golang golang locked and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

6 participants