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: type checks can be ineffective for slices as function arguments #16767

Closed
songgao opened this issue Aug 17, 2016 · 4 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@songgao
Copy link

songgao commented Aug 17, 2016

Please answer these questions before submitting your issue. Thanks!

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

go version go1.7 linux/amd64

  1. What operating system and processor architecture are you using (go env)?
GOARCH="amd64"
GOBIN="/home/songgao/gopath/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/songgao/gopath"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/songgao/go-build405294454=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
  1. What did you do?

This builds and runs fine: https://play.golang.org/p/XPDlxvky-4

But this has compile errors: https://play.golang.org/p/XQ4SXHlJNR

  1. What did you expect to see?

Both cases should have compile errors, consistent with spec

  1. What did you see instead?

    foo(type foo int) is treated as a different from int, but bar(type bar []byte) is treated as the same type as []byte

@magical
Copy link
Contributor

magical commented Aug 17, 2016

Some more test cases: https://play.golang.org/p/Aq74lddwDH

@magical
Copy link
Contributor

magical commented Aug 17, 2016

I think this is actually working as intended.

The spec says:

A value x is assignable to a variable of type T ("x is assignable to T") in any of these cases:

  • ...
  • x's type V and T have identical underlying types and at least one of V or T is not a named type.

and

Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself. Otherwise, T's underlying type is the underlying type of the type to which T refers in its type declaration.

and

Types may be named or unnamed. Named types are specified by a (possibly qualified) type name; unnamed types are specified using a type literal, which composes a new type from existing types.

Consider this code:

type T []byte
var x = []byte{1, 2, 3}
var _ T = x

T is a named type whose underlying type is []byte. X is a variable with an unnamed type (call it V) whose underlying type is also []byte. Since T and V have the same underlying type, and V is unnamed, the assignment in the third line is permitted.

@songgao
Copy link
Author

songgao commented Aug 18, 2016

Hmm.. interesting. But under this rule, shouldn't byte(4) be assignable to variable typed t5 since only one of them is named type and they both have the same underlying type byte?

@rakyll rakyll changed the title Type checks can be ineffective for slices as function arguments cmd/compile: type checks can be ineffective for slices as function arguments Aug 19, 2016
@quentinmit quentinmit added this to the Go1.8Maybe milestone Sep 6, 2016
@quentinmit quentinmit added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 10, 2016
@rsc
Copy link
Contributor

rsc commented Oct 20, 2016

@magical's explanation is correct.
To respond to @songgao's final question: no, because byte and t5 are both named types ("byte" is a name).

@rsc rsc closed this as completed Oct 20, 2016
@golang golang locked and limited conversation to collaborators Oct 20, 2017
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