Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modindex/build_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func readGoInfo(f io.Reader, info *fileInfo) error {
if !isValidImport(path) {
// The parser used to return a parse error for invalid import paths, but
// no longer does, so check for and create the error here instead.
info.parseErr = scanner.Error{Pos: info.fset.Position(spec.Pos()), Msg: "invalid import path: " + path}
info.parseErr = &scanner.Error{Pos: info.fset.Position(spec.Pos()), Msg: "invalid import path: " + path}
info.imports = nil
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/tls/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import "strconv"
// which wraps AlertError rather than sending a TLS alert.
type AlertError uint8

var _ error = AlertError(0)

func (e AlertError) Error() string {
return alert(e).String()
}
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func Listen(network, laddr string, config *Config) (net.Listener, error) {

type timeoutError struct{}

var _ error = timeoutError{}

func (timeoutError) Error() string { return "tls: DialWithDialer timed out" }
func (timeoutError) Timeout() bool { return true }
func (timeoutError) Temporary() bool { return true }
Expand Down
2 changes: 1 addition & 1 deletion src/debug/elf/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// errorReader returns error from all operations.
type errorReader struct {
error
error error
}

func (r errorReader) Read(p []byte) (n int, err error) {
Expand Down
2 changes: 1 addition & 1 deletion src/go/build/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func readGoInfo(f io.Reader, info *fileInfo) error {
if !isValidImport(path) {
// The parser used to return a parse error for invalid import paths, but
// no longer does, so check for and create the error here instead.
info.parseErr = scanner.Error{Pos: info.fset.Position(spec.Pos()), Msg: "invalid import path: " + path}
info.parseErr = &scanner.Error{Pos: info.fset.Position(spec.Pos()), Msg: "invalid import path: " + path}
info.imports = nil
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions src/go/internal/gccgoimporter/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type importError struct {
err error
}

var _ error = importError{}

func (e importError) Error() string {
return fmt.Sprintf("import error %s (byte offset = %d): %s", e.pos, e.pos.Offset, e.err)
}
Expand Down
2 changes: 2 additions & 0 deletions src/go/scanner/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Error struct {
Msg string
}

var _ error = (*Error)(nil) // *Error (not Error) is the correct type in error.Is tests.

// Error implements the error interface.
func (e Error) Error() string {
if e.Pos.Filename != "" || e.Pos.IsValid() {
Expand Down
2 changes: 1 addition & 1 deletion src/go/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func testSegments(t *testing.T, segments []segment, filename string) {
// verify scan
var S Scanner
file := fset.AddFile(filename, fset.Base(), len(src))
S.Init(file, []byte(src), func(pos token.Position, msg string) { t.Error(Error{pos, msg}) }, dontInsertSemis)
S.Init(file, []byte(src), func(pos token.Position, msg string) { t.Error(&Error{pos, msg}) }, dontInsertSemis)
for _, s := range segments {
p, _, lit := S.Scan()
pos := file.Position(p)
Expand Down
2 changes: 2 additions & 0 deletions src/internal/reflectlite/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type mapError map[string]string

func (mapError) Error() string { return "mapError" }

// For this test, both mapError and *mapError implement error,
// though in general this leads to ambiguity.
var _ error = mapError{}
var _ error = new(mapError)

Expand Down
4 changes: 0 additions & 4 deletions src/internal/runtime/exithook/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,3 @@ func Run(code int) {
h.F()
}
}

type exitError string

func (e exitError) Error() string { return string(e) }
2 changes: 0 additions & 2 deletions src/log/slog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// TODO: verify that the output of Marshal{Text,JSON} is suitably escaped.

package slog

import (
Expand Down
38 changes: 36 additions & 2 deletions src/log/slog/json_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"internal/goexperiment"
"io"
"log/slog/internal/buffer"
Expand Down Expand Up @@ -54,6 +53,39 @@ func TestJSONHandler(t *testing.T) {
}
}

func TestJSONHandlerMarshalerEscaping(t *testing.T) {
var buf bytes.Buffer
h := NewJSONHandler(&buf, nil)
r := NewRecord(testTime, LevelInfo, "m", 0)
r.AddAttrs(Any("m", jsonMarshaler{marshalerASCII}))
if err := h.Handle(t.Context(), r); err != nil {
t.Fatal(err)
}
got := bytes.TrimSuffix(buf.Bytes(), []byte{'\n'})
if !json.Valid(got) {
t.Fatalf("output is not valid JSON: %q", got)
}
for _, escaped := range []string{`\"`, `\\`, `\t`, `\n`, `\u0000`} {
if !bytes.Contains(got, []byte(escaped)) {
t.Errorf("output %q does not contain JSON escape %q", got, escaped)
}
}
for i := byte(0); i < 0x20; i++ {
if bytes.IndexByte(got, i) >= 0 {
t.Errorf("output %q contains unescaped control character %#x", got, i)
}
}
var record struct {
M []string `json:"m"`
}
if err := json.Unmarshal(got, &record); err != nil {
t.Fatal(err)
}
if len(record.M) != 1 || record.M[0] != marshalerASCII {
t.Errorf("marshaled value = %q, want %q", record.M, []string{marshalerASCII})
}
}

// for testing json.Marshaler
type jsonMarshaler struct {
s string
Expand All @@ -65,13 +97,15 @@ func (j jsonMarshaler) MarshalJSON() ([]byte, error) {
if j.s == "" {
return nil, errors.New("json: empty string")
}
return []byte(fmt.Sprintf(`[%q]`, j.s)), nil
return json.Marshal([]string{j.s})
}

type jsonMarshalerError struct {
jsonMarshaler
}

var _ error = jsonMarshalerError{}

func (jsonMarshalerError) Error() string { return "oops" }

func TestAppendJSONValue(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions src/log/slog/text_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ import (
"fmt"
"internal/testenv"
"io"
"strconv"
"strings"
"testing"
"time"
)

var testTime = time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC)

var marshalerASCII = func() string {
b := make([]byte, 128)
for i := range b {
b[i] = byte(i)
}
return string(b)
}()

func TestTextHandler(t *testing.T) {
for _, test := range []struct {
name string
Expand Down Expand Up @@ -49,6 +58,11 @@ func TestTextHandler(t *testing.T) {
Any("t", text{"abc"}),
`t`, `"text{\"abc\"}"`,
},
{
"TextMarshaler escapes",
Any("t", text{marshalerASCII}),
`t`, strconv.Quote(fmt.Sprintf("text{%q}", marshalerASCII)),
},
{
"TextMarshaler error",
Any("t", text{""}),
Expand Down
2 changes: 2 additions & 0 deletions src/math/big/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type ErrNaN struct {
msg string
}

var _ error = ErrNaN{}

func (err ErrNaN) Error() string {
return err.msg
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func (sd *sysDialer) dialParallel(ctx context.Context, primaries, fallbacks addr

type dialResult struct {
Conn
error
error error
primary bool
done bool
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func TestDialerLocalAddr(t *testing.T) {
type test struct {
network, raddr string
laddr Addr
error
error error
}
var tests = []test{
{"tcp4", "127.0.0.1", nil, nil},
Expand Down
2 changes: 1 addition & 1 deletion src/net/dnsclient_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, network, name strin
type result struct {
p dnsmessage.Parser
server string
error
error error
}

if conf == nil {
Expand Down
2 changes: 2 additions & 0 deletions src/net/http/h2_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type externalStreamError struct {
Cause error
}

var _ error = externalStreamError{}

func (e externalStreamError) Error() string {
return fmt.Sprintf("ID %v, code %v", e.StreamID, e.Code)
}
Expand Down
2 changes: 2 additions & 0 deletions src/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ func (e UnknownNetworkError) Temporary() bool { return false }

type InvalidAddrError string

var _ error = InvalidAddrError("")

func (e InvalidAddrError) Error() string { return string(e) }
func (e InvalidAddrError) Timeout() bool { return false }
func (e InvalidAddrError) Temporary() bool { return false }
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func (e errorAddressString) Error() string {
return "runtime error: " + e.msg
}

var _ error = errorAddressString{}

// Addr returns the memory address where a fault occurred.
// The address provided is best-effort.
// The veracity of the result may depend on the platform.
Expand All @@ -123,6 +125,8 @@ func (e plainError) Error() string {
return string(e)
}

var _ error = plainError("")

// A boundsError represents an indexing or slicing operation gone wrong.
type boundsError struct {
x int64
Expand All @@ -135,6 +139,8 @@ type boundsError struct {
code abi.BoundsErrorCode
}

var _ error = boundsError{}

// boundsErrorFmts provide error text for various out-of-bounds panics.
// Note: if you change these strings, you should adjust the size of the buffer
// in boundsError.Error below as well.
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/synctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ type synctestDeadlockError struct {
bubble *synctestBubble
}

var _ error = synctestDeadlockError{}

func (e synctestDeadlockError) Error() string {
return e.reason
}
Expand Down
Loading