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

Failure to apply the "underlying type" assignability rule #4377

Closed
masiulaniec opened this issue Nov 12, 2012 · 2 comments
Closed

Failure to apply the "underlying type" assignability rule #4377

masiulaniec opened this issue Nov 12, 2012 · 2 comments
Milestone

Comments

@masiulaniec
Copy link

package main

func main() {
    type T string
    var x string
    var y T
    x = y
    y = x
    _, _ = x, y
}

This program fails to compile:

prog.go:7: cannot use y (type T) as type string in assignment
prog.go:8: cannot use x (type string) as type T in assignment

It succeeds after running s/string/[]string/g.

I was expecting both versions to work due to the rule:

  A value x is assignable to a variable of type T ("x is assignable to T")
  if x's type V and T have identical underlying types and at least one
  of V or T is not a named type."

According to the spec, the types of x and y have identical underlying type
in both versions of the program, string and []string respectively. I can't find
a justification for the failure of the string version.

The compiler is 6g at version go1.0.3.
@rsc
Copy link
Contributor

rsc commented Nov 12, 2012

Comment 1:

'string' and 'T' are both named types.

Status changed to WorkingAsIntended.

@ianlancetaylor
Copy link
Member

Comment 2:

The predeclared type string is a named type.  It's name is "string".

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants