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
Closed

cmd/compile: confusing error when variable name shares type name #26547

andygarfield opened this issue Jul 23, 2018 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@andygarfield
Copy link

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
Copy link

FMNSSun 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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 23, 2018
@bcmills bcmills added this to the Go1.12 milestone Jul 23, 2018
@bcmills
Copy link
Contributor

bcmills 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
Copy link
Member

mvdan commented Jul 23, 2018

This seems like an exact duplicate of #23065.

@bcmills
Copy link
Contributor

bcmills commented Jul 23, 2018

Indeed.

@bcmills bcmills closed this as completed Jul 23, 2018
@golang golang locked and limited conversation to collaborators Jul 23, 2019
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