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: better error message when attempting to use a shadowed type name #9655

Open
pwaller opened this Issue Jan 21, 2015 · 1 comment

Comments

Projects
None yet
4 participants
@pwaller
Contributor

pwaller commented Jan 21, 2015

The compiler will emit a confusing error when a type is shadowed within a function. It should somehow try and make clear that the inner type shadows the global type.

prog.go:13: cannot use Range literal (type Range) as type Range in assignment

Link to play.golang.org example

Link to golang-nuts thread

package main

type Range struct{ Start, End int }

type Foo struct{ rng Range }

func main() {

    type Range struct{ Start, End int }
    foos := []Foo{}

    for i, foo := range foos {
        foos[i].rng = Range{}
    }
}

See also #8853.

Edit: I incorrectly referred to "shadowing" as "aliasing".

@griesemer

This comment has been minimized.

Contributor

griesemer commented Jan 21, 2015

Just for the record: There's no "aliasing" going on here - you are declaring a new Range that happens to shadow an outer Range type; not a recommended practice for types.

It's probably a bit of work to make these cases print more information when it makes sense (such as in this one), and not when it's not needed.

Certainly not urgent.

@minux minux added the repo-main label Jan 22, 2015

@griesemer griesemer changed the title from cmd/gc: cannot use Foo literal (type Foo) as type Foo in assignment to cmd/gc: better error message when attempting to use a shadowed type name Jan 23, 2015

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc removed the repo-main label Apr 14, 2015

@rsc rsc changed the title from cmd/gc: better error message when attempting to use a shadowed type name to cmd/compile: better error message when attempting to use a shadowed type name Jun 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment