Error: cannot use Range literal (type Range) as type Range in assignment

120 views
Skip to first unread message

Peter Waller

unread,
Jan 21, 2015, 3:55:43 AM1/21/15
to golang-nuts
After a rebase and refactor of a large-ish codebase, I found myself
staring at this odd compiler error:

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

I eventually found the bug, but the message was hard to interpret,
since it sounds impossible. It turned out that the type was being
aliased inside the function. This wasn't immediately clear - it was
obscured by the "fog of rebase".

I was wondering if it's possible to fix the message somehow to make it
clearer that one of those "Ranges" mentioned in the error is not like
the other?

Playground:

http://play.golang.org/p/ljMvbQf_WC

Code:

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{}
}
}

Brad Fitzpatrick

unread,
Jan 21, 2015, 10:46:55 AM1/21/15
to Peter Waller, golang-nuts
Nice find!

Please file a bug: https://github.com/golang/go/issues/new

Go cares a lot about error message clarity.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brendan Tracey

unread,
Jan 21, 2015, 12:28:40 PM1/21/15
to golan...@googlegroups.com, pe...@scraperwiki.com
I imagine this error is related (though probably distinct)

https://github.com/golang/go/issues/8853

Peter Waller

unread,
Jan 21, 2015, 4:36:49 PM1/21/15
to Brad Fitzpatrick, golang-nuts
On 21 January 2015 at 15:46, Brad Fitzpatrick <brad...@golang.org> wrote:
> Please file a bug: https://github.com/golang/go/issues/new

Filed: https://github.com/golang/go/issues/9655
Reply all
Reply to author
Forward
0 new messages