public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Go patch committed: Copy array in range statement
@ 2014-12-23 18:49 Ian Lance Taylor
  0 siblings, 0 replies; only message in thread
From: Ian Lance Taylor @ 2014-12-23 18:49 UTC (permalink / raw)
  To: gcc-patches, gofrontend-dev

[-- Attachment #1: Type: text/plain, Size: 404 bytes --]

The Go frontend failed to copy an array in a range statement.  The
effect was that if the code in the loop changed future elements of the
original array, it would incorrectly change the results of the
iteration.  This patch from Chris Manghane fixes the problem.  This
issue 34 in the gofrontend issue tracker.  Bootstrapped and ran Go
testsuite on x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

[-- Attachment #2: foo.txt --]
[-- Type: text/plain, Size: 1414 bytes --]

diff -r 738f975a6972 go/statements.cc
--- a/go/statements.cc	Fri Dec 19 14:18:12 2014 -0800
+++ b/go/statements.cc	Tue Dec 23 10:36:47 2014 -0800
@@ -5558,8 +5558,9 @@
 
   // The loop we generate:
   //   len_temp := len(range)
+  //   range_temp := range
   //   for index_temp = 0; index_temp < len_temp; index_temp++ {
-  //           value_temp = range[index_temp]
+  //           value_temp = range_temp[index_temp]
   //           index = index_temp
   //           value = value_temp
   //           original body
@@ -5573,9 +5574,11 @@
   Block* init = new Block(enclosing, loc);
 
   Expression* ref = this->make_range_ref(range_object, range_temp, loc);
+  range_temp = Statement::make_temporary(NULL, ref, loc);
   Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
   Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
 							    len_call, loc);
+  init->add_statement(range_temp);
   init->add_statement(len_temp);
 
   Expression* zexpr = Expression::make_integer_ul(0, NULL, loc);
@@ -5605,7 +5608,7 @@
     {
       iter_init = new Block(body_block, loc);
 
-      ref = this->make_range_ref(range_object, range_temp, loc);
+      ref = Expression::make_temporary_reference(range_temp, loc);
       Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
       Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-12-23 18:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-23 18:49 Go patch committed: Copy array in range statement Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).