From: Matt T. Proud Date: Tue, 19 Oct 2021 08:09:06 +0000 (+0200) Subject: errors: mention Is methods should not call Unwrap X-Git-Tag: go1.18beta1~839 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7999fd4710e7f987e86d7b32dd9af31ced4810ba;p=gostls13.git errors: mention Is methods should not call Unwrap errors.Is internally unwraps the error until the error matches the target. Because of this, a user-authored Is method on an error type need not call errors.Unwrap on itself or the target, because that would make the unwrapping operation O(N^2). It is a subtle detail to remind authors for resource efficiency reasons. Change-Id: Ic1ba59a5bdbfe2c7cb51a2cba2537ab6de4a13ff Reviewed-on: https://go-review.googlesource.com/c/go/+/356789 Reviewed-by: Jean de Klerk Reviewed-by: Damien Neil Trust: Jean de Klerk Trust: Damien Neil Run-TryBot: Jean de Klerk TryBot-Result: Go Bot --- diff --git a/src/errors/wrap.go b/src/errors/wrap.go index 4eb4f9ae37..b73d5a8ce2 100644 --- a/src/errors/wrap.go +++ b/src/errors/wrap.go @@ -35,7 +35,8 @@ func Unwrap(err error) error { // func (m MyError) Is(target error) bool { return target == fs.ErrExist } // // then Is(MyError{}, fs.ErrExist) returns true. See syscall.Errno.Is for -// an example in the standard library. +// an example in the standard library. An Is method should only shallowly +// compare err and the target and not call Unwrap on either. func Is(err, target error) bool { if target == nil { return err == target