Skip to content

Commit c33f984

Browse files
committed
Merge remote-tracking branch 'upstream/master' into net-url-testparseerrors-fix
2 parents 3afe2c8 + 546ea78 commit c33f984

File tree

7 files changed

+107
-8
lines changed

7 files changed

+107
-8
lines changed

doc/go1.13.html

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,37 @@ <h3 id="crypto/ed25519"><a href="/pkg/crypto/ed25519/">crypto/ed25519</a></h3>
525525
<code>crypto/ed25519</code> when used with Go 1.13+.
526526
</p>
527527

528+
<h3 id="error_wrapping">Error wrapping</h3>
529+
530+
<p><!-- CL 163558, 176998 -->
531+
Go 1.13 contains support for error wrapping, as first proposed in
532+
the <a href="https://go.googlesource.com/proposal/+/master/design/29934-error-values.md">
533+
Error Values proposal</a> and discussed on <a href="https://golang.org/issue/29934">the
534+
associated issue</a>.
535+
</p>
536+
<p>
537+
An error <code>e</code> can <em>wrap</em> another error <code>w</code> by providing
538+
an <code>Unwrap</code> method that returns <code>w</code>. Both <code>e</code>
539+
and <code>w</code> are available to programs, allowing <code>e</code> to provide
540+
additional context to <code>w</code> or to reinterpret it while still allowing
541+
programs to make decisions based on <code>w</code>.
542+
</p>
543+
<p>
544+
To support wrapping, <a href="#fmt"><code>fmt.Errorf</code></a> now has a <code>%w</code>
545+
verb for creating wrapped errors, and three new functions in
546+
the <a href="#errors"><code>errors</code></a> package (
547+
<a href="/pkg/errors#Unwrap"><code>errors.Unwrap</code></a>,
548+
<a href="/pkg/errors#Is"><code>errors.Is</code></a> and
549+
<a href="/pkg/errors#As"><code>errors.As</code></a>) simplify unwrapping
550+
and inspecting wrapped errors.
551+
</p>
552+
<p>
553+
For more information, read the <a href="/pkg/errors/"><code>errors</code> package
554+
documentation</a>, or see
555+
the <a href="https://golang.org/wiki/ErrorValueFAQ">Error Value FAQ</a>.
556+
There will soon be a blog post as well.
557+
</p>
558+
528559
<h3 id="minor_library_changes">Minor changes to the library</h3>
529560

530561
<p>
@@ -612,7 +643,8 @@ <h3 id="minor_library_changes">Minor changes to the library</h3>
612643
<dd>
613644
<!-- CL 163558 -->
614645
<p>
615-
The new function <a href="/pkg/errors/#As"><code>As</code></a> finds the first error in a given error’s chain
646+
The new function <a href="/pkg/errors/#As"><code>As</code></a> finds the first
647+
error in a given error’s chain (sequence of wrapped errors)
616648
that matches a given target’s type, and if so, sets the target to that error value.
617649
</p>
618650
<p>

src/cmd/go/internal/version/exe.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) {
103103
}
104104

105105
func (x *elfExe) DataStart() uint64 {
106+
for _, s := range x.f.Sections {
107+
if s.Name == ".go.buildinfo" {
108+
return s.Addr
109+
}
110+
}
106111
for _, p := range x.f.Progs {
107112
if p.Type == elf.PT_LOAD && p.Flags&(elf.PF_X|elf.PF_W) == elf.PF_W {
108113
return p.Vaddr

src/cmd/go/testdata/script/mod_invalid_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ go mod edit -require golang.org/x/[email protected]
141141
go mod edit -replace golang.org/x/[email protected]=golang.org/x/[email protected]
142142
cd outside
143143
! go get -d golang.org/x/text@upgrade
144-
stderr 'go get golang.org/x/text@upgrade: golang.org/x/text@v1.999999.0: invalid version: unknown revision v1.999999.0'
144+
stderr 'go: [email protected] requires\n\tgolang.org/x/text@v1.999999.0: reading golang.org/x/text/go.mod at revision v1.999999.0: unknown revision v1.999999.0'
145145
cd ..
146146
go get -d golang.org/x/text@upgrade
147147
go list -m golang.org/x/text

src/cmd/go/testdata/script/version.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,12 @@ go version -m fortune.exe
88
stdout '^\tpath\trsc.io/fortune'
99
stdout '^\tmod\trsc.io/fortune\tv1.0.0'
1010

11+
go build -buildmode=pie -o external.exe rsc.io/fortune
12+
go version external.exe
13+
stdout '^external.exe: .+'
14+
go version -m external.exe
15+
stdout '^\tpath\trsc.io/fortune'
16+
stdout '^\tmod\trsc.io/fortune\tv1.0.0'
17+
1118
-- go.mod --
1219
module m

src/errors/errors.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,58 @@
33
// license that can be found in the LICENSE file.
44

55
// Package errors implements functions to manipulate errors.
6+
//
7+
// The New function creates errors whose only content is a text message.
8+
//
9+
// The Unwrap, Is and As functions work on errors that may wrap other errors.
10+
// An error wraps another error if its type has the method
11+
//
12+
// Unwrap() error
13+
//
14+
// If e.Unwrap() returns a non-nil error w, then we say that e wraps w.
15+
//
16+
// A simple way to create wrapped errors is to call fmt.Errorf and apply the %w verb
17+
// to the error argument:
18+
//
19+
// fmt.Errorf("... %w ...", ..., err, ...).Unwrap()
20+
//
21+
// returns err.
22+
//
23+
// Unwrap unpacks wrapped errors. If its argument's type has an
24+
// Unwrap method, it calls the method once. Otherwise, it returns nil.
25+
//
26+
// Is unwraps its first argument sequentially looking for an error that matches the
27+
// second. It reports whether it finds a match. It should be used in preference to
28+
// simple equality checks:
29+
//
30+
// if errors.Is(err, os.ErrExist)
31+
//
32+
// is preferable to
33+
//
34+
// if err == os.ErrExist
35+
//
36+
// because the former will succeed if err wraps os.ErrExist.
37+
//
38+
// As unwraps its first argument sequentially looking for an error that can be
39+
// assigned to its second argument, which must be a pointer. If it succeeds, it
40+
// performs the assignment and returns true. Otherwise, it returns false. The form
41+
//
42+
// var perr *os.PathError
43+
// if errors.As(err, &perr) {
44+
// fmt.Println(perr.Path)
45+
// }
46+
//
47+
// is preferable to
48+
//
49+
// if perr, ok := err.(*os.PathError); ok {
50+
// fmt.Println(perr.Path)
51+
// }
52+
//
53+
// because the former will succeed if err wraps an *os.PathError.
654
package errors
755

856
// New returns an error that formats as the given text.
57+
// Each call to New returns a distinct error value even if the text is identical.
958
func New(text string) error {
1059
return &errorString{text}
1160
}

src/errors/wrap.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func Unwrap(err error) error {
2323

2424
// Is reports whether any error in err's chain matches target.
2525
//
26+
// The chain consists of err itself followed by the sequence of errors obtained by
27+
// repeatedly calling Unwrap.
28+
//
2629
// An error is considered to match a target if it is equal to that target or if
2730
// it implements a method Is(error) bool such that Is(target) returns true.
2831
func Is(err, target error) bool {
@@ -50,6 +53,9 @@ func Is(err, target error) bool {
5053
// As finds the first error in err's chain that matches target, and if so, sets
5154
// target to that error value and returns true.
5255
//
56+
// The chain consists of err itself followed by the sequence of errors obtained by
57+
// repeatedly calling Unwrap.
58+
//
5359
// An error matches target if the error's concrete value is assignable to the value
5460
// pointed to by target, or if the error has a method As(interface{}) bool such that
5561
// As(target) returns true. In the latter case, the As method is responsible for

src/syscall/syscall_freebsd.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919

2020
const (
2121
_SYS_FSTAT_FREEBSD12 = 551 // { int fstat(int fd, _Out_ struct stat *sb); }
22-
_SYS_FSTATAT_FREEBSD12 = 552 // { int fstatat(int fd, _In_z_ char *path, \
23-
_SYS_GETDIRENTRIES_FREEBSD12 = 554 // { ssize_t getdirentries(int fd, \
24-
_SYS_STATFS_FREEBSD12 = 555 // { int statfs(_In_z_ char *path, \
25-
_SYS_FSTATFS_FREEBSD12 = 556 // { int fstatfs(int fd, \
26-
_SYS_GETFSSTAT_FREEBSD12 = 557 // { int getfsstat( \
27-
_SYS_MKNODAT_FREEBSD12 = 559 // { int mknodat(int fd, _In_z_ char *path, \
22+
_SYS_FSTATAT_FREEBSD12 = 552 // { int fstatat(int fd, _In_z_ char *path, _Out_ struct stat *buf, int flag); }
23+
_SYS_GETDIRENTRIES_FREEBSD12 = 554 // { ssize_t getdirentries(int fd, _Out_writes_bytes_(count) char *buf, size_t count, _Out_ off_t *basep); }
24+
_SYS_STATFS_FREEBSD12 = 555 // { int statfs(_In_z_ char *path, _Out_ struct statfs *buf); }
25+
_SYS_FSTATFS_FREEBSD12 = 556 // { int fstatfs(int fd, _Out_ struct statfs *buf); }
26+
_SYS_GETFSSTAT_FREEBSD12 = 557 // { int getfsstat(_Out_writes_bytes_opt_(bufsize) struct statfs *buf, long bufsize, int mode); }
27+
_SYS_MKNODAT_FREEBSD12 = 559 // { int mknodat(int fd, _In_z_ char *path, mode_t mode, dev_t dev); }
2828
)
2929

3030
// See https://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/versions.html.

0 commit comments

Comments
 (0)