Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit e8c2198

Browse files
committed
Make errors.stack comparable (#30)
Fixed #29 errors.New, etc values are not expected to be compared by value but the change in errors#27 made them incomparable. Assert that various kinds of errors have a functional equality operator, even if the result of that equality is always false.
1 parent abe54b4 commit e8c2198

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

errors.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ import (
6464
// stack represents a stack of programm counters.
6565
type stack []uintptr
6666

67-
func (s stack) Stack() []uintptr { return s }
67+
func (s *stack) Stack() []uintptr { return *s }
6868

69-
func (s stack) Location() (string, int) {
70-
return location(s[0] - 1)
69+
func (s *stack) Location() (string, int) {
70+
return location((*s)[0] - 1)
7171
}
7272

7373
// New returns an error that formats as the given text.
7474
func New(text string) error {
7575
return struct {
7676
error
77-
stack
77+
*stack
7878
}{
7979
errors.New(text),
8080
callers(),
@@ -95,7 +95,7 @@ func (c cause) Message() string { return c.message }
9595
func Errorf(format string, args ...interface{}) error {
9696
return struct {
9797
error
98-
stack
98+
*stack
9999
}{
100100
fmt.Errorf(format, args...),
101101
callers(),
@@ -120,10 +120,10 @@ func Wrapf(cause error, format string, args ...interface{}) error {
120120
return wrap(cause, fmt.Sprintf(format, args...), callers())
121121
}
122122

123-
func wrap(err error, msg string, st stack) error {
123+
func wrap(err error, msg string, st *stack) error {
124124
return struct {
125125
cause
126-
stack
126+
*stack
127127
}{
128128
cause{
129129
cause: err,
@@ -198,11 +198,12 @@ func Fprint(w io.Writer, err error) {
198198
}
199199
}
200200

201-
func callers() stack {
201+
func callers() *stack {
202202
const depth = 32
203203
var pcs [depth]uintptr
204204
n := runtime.Callers(3, pcs[:])
205-
return pcs[0:n]
205+
var st stack = pcs[0:n]
206+
return &st
206207
}
207208

208209
// location returns the source file and line matching pc.

errors_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,24 @@ func TestStack(t *testing.T) {
241241
}
242242
}
243243
}
244+
245+
// errors.New, etc values are not expected to be compared by value
246+
// but the change in errors#27 made them incomparable. Assert that
247+
// various kinds of errors have a functional equality operator, even
248+
// if the result of that equality is always false.
249+
func TestErrorEquality(t *testing.T) {
250+
tests := []struct {
251+
err1, err2 error
252+
}{
253+
{io.EOF, io.EOF},
254+
{io.EOF, nil},
255+
{io.EOF, errors.New("EOF")},
256+
{io.EOF, New("EOF")},
257+
{New("EOF"), New("EOF")},
258+
{New("EOF"), Errorf("EOF")},
259+
{New("EOF"), Wrap(io.EOF, "EOF")},
260+
}
261+
for _, tt := range tests {
262+
_ = tt.err1 == tt.err2 // mustn't panic
263+
}
264+
}

0 commit comments

Comments
 (0)