internal/impl: use linked-list for unknown field ordering

Unknown fields follow a policy where the latest field takes precedence when
it comes to the ordering. However, the current implementation is incorrect
as it uses a slice and simply swaps the current entry with the last entry.
While this ensures that the latest field seen remains last, it does not ensure
that the swapped out entry is second-to-last.

To provide the desired behavior, a linked-list is used.
For simplicity, we use the list package in the standard library even if it
is neither the most performant nor type safe.

Change-Id: I675145c61f6b5b624ed9e94bbe2251b5a71e2c48
Reviewed-on: https://go-review.googlesource.com/c/145241
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/legacy_test.go b/internal/impl/legacy_test.go
index 32253f3..d2e71d4 100644
--- a/internal/impl/legacy_test.go
+++ b/internal/impl/legacy_test.go
@@ -201,7 +201,7 @@
 	}
 
 	// Simulate manual appending of raw field data.
-	fs = append(fs, joinRaw(raw3a, raw1a, raw1b, raw3b, raw2b, raw1c)...)
+	fs = append(fs, joinRaw(raw3a, raw1a, raw1b, raw2b, raw3b, raw1c)...)
 	if got, want := fs.Len(), 3; got != want {
 		t.Errorf("Len() = %d, want %d", got, want)
 	}
@@ -212,8 +212,8 @@
 		num pref.FieldNumber
 		raw pref.RawFields
 	}{
-		{3, joinRaw(raw3a, raw3b)},
 		{2, joinRaw(raw2a, raw2b)},
+		{3, joinRaw(raw3a, raw3b)},
 		{1, joinRaw(raw1a, raw1b, raw1c)},
 	}
 	fs.Range(func(num pref.FieldNumber, raw pref.RawFields) bool {