Skip to content

Commit 7a21f79

Browse files
griesemergopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent e91876f commit 7a21f79

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/cmd/compile/internal/base/flag.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ type CmdFlags struct {
124124
TrimPath string "help:\"remove `prefix` from recorded source file paths\""
125125
WB bool "help:\"enable write barrier\"" // TODO: remove
126126
PgoProfile string "help:\"read profile from `file`\""
127+
Url bool "help:\"print explanatory URL with error message if applicable\""
127128

128129
// Configuration derived from flags; not a flag itself.
129130
Cfg struct {

src/cmd/compile/internal/base/print.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818

1919
// An errorMsg is a queued error message, waiting to be printed.
2020
type errorMsg struct {
21-
pos src.XPos
22-
msg string
21+
pos src.XPos
22+
msg string
23+
code errors.Code
2324
}
2425

2526
// Pos is the current source position being processed,
@@ -43,16 +44,17 @@ func SyntaxErrors() int {
4344
}
4445

4546
// addErrorMsg adds a new errorMsg (which may be a warning) to errorMsgs.
46-
func addErrorMsg(pos src.XPos, format string, args ...interface{}) {
47+
func addErrorMsg(pos src.XPos, code errors.Code, format string, args ...interface{}) {
4748
msg := fmt.Sprintf(format, args...)
4849
// Only add the position if know the position.
4950
// See issue golang.org/issue/11361.
5051
if pos.IsKnown() {
5152
msg = fmt.Sprintf("%v: %s", FmtPos(pos), msg)
5253
}
5354
errorMsgs = append(errorMsgs, errorMsg{
54-
pos: pos,
55-
msg: msg + "\n",
55+
pos: pos,
56+
msg: msg + "\n",
57+
code: code,
5658
})
5759
}
5860

@@ -84,6 +86,10 @@ func FlushErrors() {
8486
for i, err := range errorMsgs {
8587
if i == 0 || err.msg != errorMsgs[i-1].msg {
8688
fmt.Printf("%s", err.msg)
89+
if Flag.Url && err.code != 0 {
90+
// TODO(gri) we should come up with a better URL eventually
91+
fmt.Printf("\thttps://pkg.go.dev/internal/types/errors#%s\n", err.code)
92+
}
8793
}
8894
}
8995
errorMsgs = errorMsgs[:0]
@@ -133,7 +139,7 @@ func ErrorfAt(pos src.XPos, code errors.Code, format string, args ...interface{}
133139
lasterror.msg = msg
134140
}
135141

136-
addErrorMsg(pos, "%s", msg)
142+
addErrorMsg(pos, code, "%s", msg)
137143
numErrors++
138144

139145
hcrash()
@@ -175,7 +181,7 @@ func Warn(format string, args ...interface{}) {
175181
// so this should be used only when the user has opted in
176182
// to additional output by setting a particular flag.
177183
func WarnfAt(pos src.XPos, format string, args ...interface{}) {
178-
addErrorMsg(pos, format, args...)
184+
addErrorMsg(pos, 0, format, args...)
179185
if Flag.LowerM != 0 {
180186
FlushErrors()
181187
}

0 commit comments

Comments
 (0)