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

Open
pwaller opened this issue Jan 21, 2015 · 1 comment
Milestone

Comments

@pwaller
Copy link
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
Copy link
Contributor

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.

@griesemer griesemer changed the title cmd/gc: cannot use Foo literal (type Foo) as type Foo in assignment 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 cmd/gc: better error message when attempting to use a shadowed type name cmd/compile: better error message when attempting to use a shadowed type name Jun 8, 2015
@odeke-em odeke-em self-assigned this Jun 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants