Errorf: support %w anywhere in the format string

Allow a single %w to appear anywhere in the format string, matched
by an operand that satisfies the error interface.

This is complicated by a few things:

- We don't want to do full-fledged printf verb parsing. We approximate,
  and do not handle argument indexes on %w like "%[3]w".

- There is a messy interaction with the xerrors formatting system (not
  adopted in Go 1.13).  When the %w is at the end we can control the
  error string so that the wrapped error's message is formatted
  properly, after the wrapping errors' message. With a %w in the
  middle, we can't do that. In this CL, we print the wrapped error's
  message twice: once in its place in the format string, and then
  again because it's wrapped.

Fixes #go/33143.

Change-Id: I6192096521664476e82d5a54b80e352e47397cfe
Reviewed-by: Damien Neil <>
4 files changed