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: confusing error when variable name shares type name #26547

Closed
andygarfield opened this issue Jul 23, 2018 · 4 comments

Comments

Projects
None yet
4 participants
@andygarfield
Copy link

commented Jul 23, 2018

What version of Go are you using?

1.10.3

What operating system and processor architecture are you using?

amd64

What did you do?

Playground Link

package main

import (
	"fmt"
)

type dog struct {
	name string
}

func main() {
	dog := dog{name: "fido"}

	func (d dog) {
		fmt.Println(d.name)
	}(dog)
}

What did you see?

prog.go:14:8: dog is not a type
prog.go:14:10: dog is not a type
prog.go:15:16: dog is not a type

What did you expect to see?

A better error message, since dog is a type, after all. Maybe something like:

prog.go:12:1: variable name duplicates type name
@FMNSSun

This comment has been minimized.

Copy link

commented Jul 23, 2018

This would imply that you won't be able to shadow type names anymore.

type foo struct { 
 x int
}

binds the type struct { x int } to the identifier foo and identifiers may be redeclared in an inner block. dog is an identifier.

This would also imply that if you add types to existing code, and there happens to be a collision such as

type i struct {
  x float64
}

func bar() {
  for i := 0; i < 10; i++ {
  ...
 }
}

then this would produce an error. What you're proposing isn't adding better error messages, it's adding a new kind of error.

The actual funny thing imo is that

prog.go:14:8: dog is not a type
prog.go:14:10: dog is not a type

it reports the same error twice on the same line?

@bcmills bcmills changed the title Poor compile error when variable name shares type name cmd/compile: confusing error when variable name shares type name Jul 23, 2018

@bcmills bcmills added this to the Go1.12 milestone Jul 23, 2018

@bcmills

This comment has been minimized.

Copy link
Member

commented Jul 23, 2018

As @FMNSSun notes, it is not an error for a variable name to shadow a type name as long as they are declared in different scopes. (Contrast https://play.golang.org/p/D-1VbXmGbfd.)

This diagnostic would probably be improved by a reference to the declaration in use, though:

prog.go:14:8: dog (declared at prog.go:12) is not a type
prog.go:14:10: dog (declared at prog.go:12) is not a type
prog.go:15:16: dog (declared at prog.go:12) is not a type
@mvdan

This comment has been minimized.

Copy link
Member

commented Jul 23, 2018

This seems like an exact duplicate of #23065.

@bcmills

This comment has been minimized.

Copy link
Member

commented Jul 23, 2018

Indeed.

@bcmills bcmills closed this Jul 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.