cmd/go/internal/modfile: add new requires to end of file

The old behavior was to add new requires to the first require statement in the file.

If there's more than one statement, though, it's more convenient for the first N-1
to be the manually maintained ones and then have the (possibly large) auto-maintained
block at the end of the file, instead of putting the manually maintained statements
after a giant auto-maintained one.

Suggested by Bryan Mills in an earlier CL, and works better for some real use cases.

Change-Id: I17a1704ca459c8cb5536dc23997bbacc3cd997eb
Reviewed-on: https://go-review.googlesource.com/121859
Reviewed-by: Bryan C. Mills <bcmills@google.com>
diff --git a/vendor/cmd/go/internal/modfile/read.go b/vendor/cmd/go/internal/modfile/read.go
index 8963fc2..1d81ff1 100644
--- a/vendor/cmd/go/internal/modfile/read.go
+++ b/vendor/cmd/go/internal/modfile/read.go
@@ -91,9 +91,10 @@
 
 func (x *FileSyntax) addLine(hint Expr, tokens ...string) *Line {
 	if hint == nil {
-		// If no hint given, add to the first statement of the given type.
+		// If no hint given, add to the last statement of the given type.
 	Loop:
-		for _, stmt := range x.Stmt {
+		for i := len(x.Stmt) - 1; i >= 0; i-- {
+			stmt := x.Stmt[i]
 			switch stmt := stmt.(type) {
 			case *Line:
 				if stmt.Token != nil && stmt.Token[0] == tokens[0] {
diff --git a/vendor/cmd/go/internal/modfile/rule_test.go b/vendor/cmd/go/internal/modfile/rule_test.go
new file mode 100644
index 0000000..b88ad62
--- /dev/null
+++ b/vendor/cmd/go/internal/modfile/rule_test.go
@@ -0,0 +1,90 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package modfile
+
+import (
+	"bytes"
+	"fmt"
+	"testing"
+)
+
+var addRequireTests = []struct {
+	in   string
+	path string
+	vers string
+	out  string
+}{
+	{
+		`
+		module m
+		require x.y/z v1.2.3
+		`,
+		"x.y/z", "v1.5.6",
+		`
+		module m
+		require x.y/z v1.5.6
+		`,
+	},
+	{
+		`
+		module m
+		require x.y/z v1.2.3
+		`,
+		"x.y/w", "v1.5.6",
+		`
+		module m
+		require (
+			x.y/z v1.2.3
+			x.y/w v1.5.6
+		)
+		`,
+	},
+	{
+		`
+		module m
+		require x.y/z v1.2.3
+		require x.y/q/v2 v2.3.4
+		`,
+		"x.y/w", "v1.5.6",
+		`
+		module m
+		require x.y/z v1.2.3
+		require (
+			x.y/q/v2 v2.3.4
+			x.y/w v1.5.6
+		)
+		`,
+	},
+}
+
+func TestAddRequire(t *testing.T) {
+	for i, tt := range addRequireTests {
+		t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
+			f, err := Parse("in", []byte(tt.in), nil)
+			if err != nil {
+				t.Fatal(err)
+			}
+			g, err := Parse("out", []byte(tt.out), nil)
+			if err != nil {
+				t.Fatal(err)
+			}
+			golden, err := g.Format()
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			if err := f.AddRequire(tt.path, tt.vers); err != nil {
+				t.Fatal(err)
+			}
+			out, err := f.Format()
+			if err != nil {
+				t.Fatal(err)
+			}
+			if !bytes.Equal(out, golden) {
+				t.Errorf("have:\n%s\nwant:\n%s", out, golden)
+			}
+		})
+	}
+}