From: Robert Griesemer Date: Wed, 15 Mar 2023 21:03:49 +0000 (-0700) Subject: cmd/compile: add -url flag, print URL with error messages if applicable X-Git-Tag: go1.21rc1~1241 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7a21f799a5ac23d3e191a106d71af9b8f65279fd;p=gostls13.git cmd/compile: add -url flag, print URL with error messages if applicable If the -url flag is provided, when encountering a type checking error, the compiler will also print a URL to a more detailed description of the error and an example, if available. Example uses: go tool compile -url filename.go go build -gcflags=-url pkg/path For instance, a duplicate declaration of an identifier will report https://pkg.go.dev/internal/types/errors#DuplicateDecl We may refine the provided URL over time. Change-Id: Iabe3008a49d9dd88bf690f99e4a4a5432dc08786 Reviewed-on: https://go-review.googlesource.com/c/go/+/476716 Auto-Submit: Robert Griesemer Reviewed-by: Robert Griesemer Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Robert Findley --- diff --git a/src/cmd/compile/internal/base/flag.go b/src/cmd/compile/internal/base/flag.go index ccd63f6368..d9ce42255b 100644 --- a/src/cmd/compile/internal/base/flag.go +++ b/src/cmd/compile/internal/base/flag.go @@ -124,6 +124,7 @@ type CmdFlags struct { TrimPath string "help:\"remove `prefix` from recorded source file paths\"" WB bool "help:\"enable write barrier\"" // TODO: remove PgoProfile string "help:\"read profile from `file`\"" + Url bool "help:\"print explanatory URL with error message if applicable\"" // Configuration derived from flags; not a flag itself. Cfg struct { diff --git a/src/cmd/compile/internal/base/print.go b/src/cmd/compile/internal/base/print.go index 6d21c33254..25ae04887f 100644 --- a/src/cmd/compile/internal/base/print.go +++ b/src/cmd/compile/internal/base/print.go @@ -18,8 +18,9 @@ import ( // An errorMsg is a queued error message, waiting to be printed. type errorMsg struct { - pos src.XPos - msg string + pos src.XPos + msg string + code errors.Code } // Pos is the current source position being processed, @@ -43,7 +44,7 @@ func SyntaxErrors() int { } // addErrorMsg adds a new errorMsg (which may be a warning) to errorMsgs. -func addErrorMsg(pos src.XPos, format string, args ...interface{}) { +func addErrorMsg(pos src.XPos, code errors.Code, format string, args ...interface{}) { msg := fmt.Sprintf(format, args...) // Only add the position if know the position. // See issue golang.org/issue/11361. @@ -51,8 +52,9 @@ func addErrorMsg(pos src.XPos, format string, args ...interface{}) { msg = fmt.Sprintf("%v: %s", FmtPos(pos), msg) } errorMsgs = append(errorMsgs, errorMsg{ - pos: pos, - msg: msg + "\n", + pos: pos, + msg: msg + "\n", + code: code, }) } @@ -84,6 +86,10 @@ func FlushErrors() { for i, err := range errorMsgs { if i == 0 || err.msg != errorMsgs[i-1].msg { fmt.Printf("%s", err.msg) + if Flag.Url && err.code != 0 { + // TODO(gri) we should come up with a better URL eventually + fmt.Printf("\thttps://pkg.go.dev/internal/types/errors#%s\n", err.code) + } } } errorMsgs = errorMsgs[:0] @@ -133,7 +139,7 @@ func ErrorfAt(pos src.XPos, code errors.Code, format string, args ...interface{} lasterror.msg = msg } - addErrorMsg(pos, "%s", msg) + addErrorMsg(pos, code, "%s", msg) numErrors++ hcrash() @@ -175,7 +181,7 @@ func Warn(format string, args ...interface{}) { // so this should be used only when the user has opted in // to additional output by setting a particular flag. func WarnfAt(pos src.XPos, format string, args ...interface{}) { - addErrorMsg(pos, format, args...) + addErrorMsg(pos, 0, format, args...) if Flag.LowerM != 0 { FlushErrors() }