xerrors: redirect to go1.13 primitives for 1.13

For interoperability it is especially important that the
Printer type is redirected. Otherwise the FormatError method
would not be of the same signature and there would be
no interoperability.

Some of the more bulky code is now also redirected to use
1.13.

Tests are verified to pass for both go1.12 and go1.13.

Change-Id: Ia5776252aa4774d900c53661659d9a8b61793015
Reviewed-on: https://go-review.googlesource.com/c/xerrors/+/167577
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/adaptor.go b/adaptor_go1_12.go
similarity index 99%
rename from adaptor.go
rename to adaptor_go1_12.go
index 4317f24..6b9f2f0 100644
--- a/adaptor.go
+++ b/adaptor_go1_12.go
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !go1.13
+
 package xerrors
 
 import (
diff --git a/adaptor_go1_13.go b/adaptor_go1_13.go
new file mode 100644
index 0000000..57f519d
--- /dev/null
+++ b/adaptor_go1_13.go
@@ -0,0 +1,55 @@
+// 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.
+
+// +build go1.13
+
+package xerrors
+
+import (
+	"errors"
+	"fmt"
+	"strconv"
+)
+
+// A Frame contains part of a call stack.
+type Frame = errors.Frame
+
+// Caller returns a Frame that describes a frame on the caller's stack.
+// The argument skip is the number of frames to skip over.
+// Caller(0) returns the frame for the caller of Caller.
+var Caller func(skip int) Frame = errors.Caller
+
+// FormatError calls the FormatError method of f with an errors.Printer
+// configured according to s and verb, and writes the result to s.
+func FormatError(f Formatter, s fmt.State, verb rune) {
+	// Assuming this function is only called from the Format method, and given
+	// that FormatError takes precedence over Format, it cannot be called from
+	// any package that supports errors.Formatter. It is therefore safe to
+	// disregard that State may be a specific printer implementation and use one
+	// of our choice instead.
+
+	width, okW := s.Width()
+	prec, okP := s.Precision()
+
+	// Construct format string from State s.
+	format := []byte{'%'}
+	if s.Flag('-') {
+		format = append(format, '-')
+	}
+	if s.Flag('+') {
+		format = append(format, '+')
+	}
+	if s.Flag(' ') {
+		format = append(format, ' ')
+	}
+	if okW {
+		format = strconv.AppendInt(format, int64(width), 10)
+	}
+	if okP {
+		format = append(format, '.')
+		format = strconv.AppendInt(format, int64(prec), 10)
+	}
+	format = append(format, string(verb)...)
+	fmt.Fprintf(s, string(format), f)
+}
diff --git a/fmt_test.go b/fmt_test.go
index 6744b8a..5933f18 100644
--- a/fmt_test.go
+++ b/fmt_test.go
@@ -7,7 +7,6 @@
 import (
 	"fmt"
 	"io"
-	"os"
 	"path"
 	"reflect"
 	"regexp"
@@ -91,7 +90,7 @@
 		nonascii = &wrapped{"café", nil}
 		newline  = &wrapped{"msg with\nnewline",
 			&wrapped{"and another\none", nil}}
-		fallback  = &wrapped{"fallback", os.ErrNotExist}
+		fallback  = &wrapped{"fallback", xerrors.New("file does not exist")}
 		oldAndNew = &wrapped{"new style", formatError("old style")}
 		framed    = &withFrameAndMore{
 			frame: xerrors.Caller(0),
@@ -106,6 +105,13 @@
 		want   string
 		regexp bool
 	}{{
+		err: xerrors.New("foo"),
+		fmt: "%+v",
+		want: "foo:" +
+			"\n    golang.org/x/xerrors_test.TestErrorFormatter" +
+			"\n        .+/golang.org/x/xerrors/fmt_test.go:1\\d\\d",
+		regexp: true,
+	}, {
 		err:  simple,
 		fmt:  "%s",
 		want: "simple",
@@ -156,7 +162,7 @@
 		fmt: "%+v",
 		want: "something:" +
 			"\n    golang.org/x/xerrors_test.TestErrorFormatter" +
-			"\n        .+/fmt_test.go:97" +
+			"\n        .+/fmt_test.go:9\\d" +
 			"\n    something more",
 		regexp: true,
 	}, {
@@ -173,7 +179,10 @@
 		// Note: no colon after the last error, as there are no details.
 		want: "fallback:" +
 			"\n    somefile.go:123" +
-			"\n  - file does not exist",
+			"\n  - file does not exist:" +
+			"\n    golang.org/x/xerrors_test.TestErrorFormatter" +
+			"\n        .+/golang.org/x/xerrors/fmt_test.go:9\\d",
+		regexp: true,
 	}, {
 		err:  opaque,
 		fmt:  "%s",
@@ -281,12 +290,12 @@
 		err:  simple,
 		fmt:  "%T",
 		want: "*xerrors_test.wrapped",
-	}, {
-		err:  simple,
-		fmt:  "%🤪",
-		want: "%!🤪(*xerrors_test.wrapped)",
-		// For 1.13:
-		//  want: "%!🤪(*xerrors_test.wrapped=&{simple <nil>})",
+		// }, {
+		// 	// The behavior for this case is different between go1.12 and go1.13.
+		// 	err:  simple,
+		// 	fmt:  "%🤪",
+		// 	want: "%!🤪(*xerrors_test.wrapped=&{simple <nil>})", // go1.12
+		// 	want: "&{%!🤪(string=simple) <nil>}",                // go1.13
 	}, {
 		err:  formatError("use fmt.Formatter"),
 		fmt:  "%#v",
diff --git a/format.go b/format_go1_12.go
similarity index 97%
rename from format.go
rename to format_go1_12.go
index 1bc9c26..ba856fe 100644
--- a/format.go
+++ b/format_go1_12.go
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !go1.13
+
 package xerrors
 
 // A Formatter formats error messages.
diff --git a/format_go1_13.go b/format_go1_13.go
new file mode 100644
index 0000000..95c6596
--- /dev/null
+++ b/format_go1_13.go
@@ -0,0 +1,19 @@
+// 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.
+
+// +build go1.13
+
+package xerrors
+
+import "errors"
+
+// A Formatter formats error messages.
+type Formatter = errors.Formatter
+
+// A Printer formats error messages.
+//
+// The most common implementation of Printer is the one provided by package fmt
+// during Printf (as of Go 1.13). Localization packages such as golang.org/x/text/message
+// typically provide their own implementations.
+type Printer = errors.Printer
diff --git a/frame.go b/frame_go1_12.go
similarity index 98%
rename from frame.go
rename to frame_go1_12.go
index 0de628e..6740f7f 100644
--- a/frame.go
+++ b/frame_go1_12.go
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !go1.13
+
 package xerrors
 
 import (