Ok, so strictly speaking there is no bug.  Both gc and gccgo are
spec-compliant; gc just opts not to raise an error in this case.  (One
might say it is a bug that gc doesn't raise the error consistently, but I
don't think the spec says anything about consistency, only optionality.)

In any case, I think I'm splitting the wrong hairs.  The real bug is that
the spec makes this error optional.  What is the point of this?  Such
optional behavior can only reduce portability.

I'd love to open an issue to fix the spec, although at best I think it
could only be marked for Go2 because it would involve backwards
incompatible changes that would break a lot of working code.

On Mon, Jul 4, 2016 at 5:16 AM, Ian Lance Taylor <i...@golang.org> wrote:

> On Sat, Jul 2, 2016 at 2:20 AM,  <gordon.kl...@gmail.com> wrote:
> > More generally, this is https://github.com/golang/go/issues/10989.
> >
> > Such ineffectual assignments can be detected using
> > https://github.com/gordonklaus/ineffassign.
> >
> > An analysis showed that only a small fraction of such were bugs, making
> the
> > backwards-incompatible language change not worthwhile.
> >
> > @ianlancetaylor: It's interesting that gccgo gives an error for the case
> > given in this thread.  Does it also flag other "assigned and not used",
> like
> >
> > func main() {
> >     x := 0
> >     _ = x
> >     x = 1
> > }
> >
> > ?
>
> gccgo does not warn about that.
>
> The warning issued by gc and gccgo is described by this implementation
> restriction in the spec: "A compiler may make it illegal to declare a
> variable inside a function body if the variable is never used"
> (https://golang.org/ref/spec#Variable_declarations).  In the above
> function, the variable is used, so an error is not permitted by the
> spec.
>
> Ian
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to