]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: add -url flag, print URL with error messages if applicable
authorRobert Griesemer <gri@golang.org>
Wed, 15 Mar 2023 21:03:49 +0000 (14:03 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 16 Mar 2023 20:22:11 +0000 (20:22 +0000)
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 <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/base/flag.go
src/cmd/compile/internal/base/print.go

index ccd63f6368fcfd6aabedde41dd08aaa23e219b2e..d9ce42255bcb5a6cffc9a6c7dac4fa806103bc38 100644 (file)
@@ -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 {
index 6d21c332546382ace7688d4d47eef24e18acf98c..25ae04887f4e9b346df24b470aeee8fb585d85a3 100644 (file)
@@ -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()
        }