This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Go patch committed: error if naked return results are shadowed
- From: Ian Lance Taylor <iant at google dot com>
- To: gcc-patches at gcc dot gnu dot org, gofrontend-dev at googlegroups dot com
- Date: Mon, 24 Oct 2011 12:44:20 -0700
- Subject: Go patch committed: error if naked return results are shadowed
The 6g Go compiler has picked up an error if a naked return is used when
named result variables are shadowed. This catches a typical error in Go
programs when using the := construct. This patch implements the same
error in the gccgo frontend. The patch includes a few fixes in the Go
library; each use was bad code. Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu. Committed to mainline.
Ian
diff -r 026308865c54 go/parse.cc
--- a/go/parse.cc Sun Oct 23 21:59:53 2011 -0700
+++ b/go/parse.cc Mon Oct 24 12:28:44 2011 -0700
@@ -3839,6 +3839,23 @@
if (this->expression_may_start_here())
vals = this->expression_list(NULL, false);
this->gogo_->add_statement(Statement::make_return_statement(vals, location));
+
+ if (vals == NULL
+ && this->gogo_->current_function()->func_value()->results_are_named())
+ {
+ Named_object* function = this->gogo_->current_function();
+ Function::Results* results = function->func_value()->result_variables();
+ for (Function::Results::const_iterator p = results->begin();
+ p != results->end();
+ ++p)
+ {
+ Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
+ go_assert(no != NULL);
+ if (!no->is_result_variable())
+ error_at(location, "%qs is shadowed during return",
+ (*p)->message_name().c_str());
+ }
+ }
}
// IfStmt = "if" [ SimpleStmt ";" ] Expression Block
diff -r 026308865c54 libgo/go/crypto/openpgp/s2k/s2k.go
--- a/libgo/go/crypto/openpgp/s2k/s2k.go Sun Oct 23 21:59:53 2011 -0700
+++ b/libgo/go/crypto/openpgp/s2k/s2k.go Mon Oct 24 12:28:44 2011 -0700
@@ -100,7 +100,7 @@
}
return f, nil
case 2:
- _, err := io.ReadFull(r, buf[:8])
+ _, err = io.ReadFull(r, buf[:8])
if err != nil {
return
}
@@ -109,7 +109,7 @@
}
return f, nil
case 3:
- _, err := io.ReadFull(r, buf[:9])
+ _, err = io.ReadFull(r, buf[:9])
if err != nil {
return
}
diff -r 026308865c54 libgo/go/exp/gui/x11/auth.go
--- a/libgo/go/exp/gui/x11/auth.go Sun Oct 23 21:59:53 2011 -0700
+++ b/libgo/go/exp/gui/x11/auth.go Mon Oct 24 12:28:44 2011 -0700
@@ -65,23 +65,25 @@
return
}
for {
- family, err := readU16BE(br, b[0:2])
+ var family uint16
+ var addr, disp, name0, data0 string
+ family, err = readU16BE(br, b[0:2])
if err != nil {
return
}
- addr, err := readStr(br, b[0:])
+ addr, err = readStr(br, b[0:])
if err != nil {
return
}
- disp, err := readStr(br, b[0:])
+ disp, err = readStr(br, b[0:])
if err != nil {
return
}
- name0, err := readStr(br, b[0:])
+ name0, err = readStr(br, b[0:])
if err != nil {
return
}
- data0, err := readStr(br, b[0:])
+ data0, err = readStr(br, b[0:])
if err != nil {
return
}
diff -r 026308865c54 libgo/go/exp/gui/x11/conn.go
--- a/libgo/go/exp/gui/x11/conn.go Sun Oct 23 21:59:53 2011 -0700
+++ b/libgo/go/exp/gui/x11/conn.go Mon Oct 24 12:28:44 2011 -0700
@@ -391,12 +391,13 @@
// checkDepths checks that we have an agreeable X Depth (i.e. one that has an agreeable X VisualType).
func checkDepths(r io.Reader, b []byte, n int, visual uint32) (agree bool, err os.Error) {
for i := 0; i < n; i++ {
- depth, err := readU16LE(r, b)
+ var depth, visualsLen uint16
+ depth, err = readU16LE(r, b)
if err != nil {
return
}
depth &= 0xff
- visualsLen, err := readU16LE(r, b)
+ visualsLen, err = readU16LE(r, b)
if err != nil {
return
}
@@ -408,11 +409,11 @@
for j := 0; j < int(visualsLen); j++ {
// Read 24 bytes: visual(4), class(1), bits per rgb value(1), colormap entries(2),
// red mask(4), green mask(4), blue mask(4), padding(4).
- v, err := readU32LE(r, b)
- _, err = readU32LE(r, b)
- rm, err := readU32LE(r, b)
- gm, err := readU32LE(r, b)
- bm, err := readU32LE(r, b)
+ v, _ := readU32LE(r, b)
+ _, _ = readU32LE(r, b)
+ rm, _ := readU32LE(r, b)
+ gm, _ := readU32LE(r, b)
+ bm, _ := readU32LE(r, b)
_, err = readU32LE(r, b)
if err != nil {
return
@@ -428,7 +429,8 @@
// checkScreens checks that we have an agreeable X Screen.
func checkScreens(r io.Reader, b []byte, n int) (root, visual uint32, err os.Error) {
for i := 0; i < n; i++ {
- root0, err := readU32LE(r, b)
+ var root0, visual0, x uint32
+ root0, err = readU32LE(r, b)
if err != nil {
return
}
@@ -438,17 +440,18 @@
if err != nil {
return
}
- visual0, err := readU32LE(r, b)
+ visual0, err = readU32LE(r, b)
if err != nil {
return
}
// Next 4 bytes: backing stores, save unders, root depth, allowed depths length.
- x, err := readU32LE(r, b)
+ x, err = readU32LE(r, b)
if err != nil {
return
}
nDepths := int(x >> 24)
- agree, err := checkDepths(r, b, nDepths, visual0)
+ var agree bool
+ agree, err = checkDepths(r, b, nDepths, visual0)
if err != nil {
return
}