cmd/compile: propagate correct line numbers in treecopy

Added a lineno parameter to treecopy and listtreecopy
(ignored if = 0).  When nodes are copied the copy is
assigned the non-zero lineno (normally this would be
the destination).

Fixes #8183

Change-Id: Iffb767a745093fb89aa08bf8a7692c2f0122be98
Reviewed-on: https://go-review.googlesource.com/10334
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/src/cmd/compile/internal/gc/subr.go b/src/cmd/compile/internal/gc/subr.go
index b10a6b3..08fafa8 100644
--- a/src/cmd/compile/internal/gc/subr.go
+++ b/src/cmd/compile/internal/gc/subr.go
@@ -736,7 +736,12 @@
 	return r
 }
 
-func treecopy(n *Node) *Node {
+// treecopy recursively copies n, with the exception of
+// ONAME, OLITERAL, OTYPE, and non-iota ONONAME leaves.
+// Copies of iota ONONAME nodes are assigned the current
+// value of iota_. If lineno != 0, it sets the line number
+// of newly allocated nodes to lineno.
+func treecopy(n *Node, lineno int32) *Node {
 	if n == nil {
 		return nil
 	}
@@ -747,9 +752,12 @@
 		m = Nod(OXXX, nil, nil)
 		*m = *n
 		m.Orig = m
-		m.Left = treecopy(n.Left)
-		m.Right = treecopy(n.Right)
-		m.List = listtreecopy(n.List)
+		m.Left = treecopy(n.Left, lineno)
+		m.Right = treecopy(n.Right, lineno)
+		m.List = listtreecopy(n.List, lineno)
+		if lineno != -1 {
+			m.Lineno = lineno
+		}
 		if m.Defn != nil {
 			panic("abort")
 		}
@@ -764,6 +772,9 @@
 
 			*m = *n
 			m.Iota = iota_
+			if lineno != 0 {
+				m.Lineno = lineno
+			}
 			break
 		}
 		fallthrough
@@ -3092,10 +3103,10 @@
 	return et
 }
 
-func listtreecopy(l *NodeList) *NodeList {
+func listtreecopy(l *NodeList, lineno int32) *NodeList {
 	var out *NodeList
 	for ; l != nil; l = l.Next {
-		out = list(out, treecopy(l.N))
+		out = list(out, treecopy(l.N, lineno))
 	}
 	return out
 }