Skip to content

Commit 2cbab4e

Browse files
griesemergopherbot
authored andcommitted
cmd/compile: pass type checker error codes in the compiler
Pass type checker error codes to base.ErrorfAt function calls in the compiler (but don't do anything yet with the code). Also, provide error codes to base.ErrorfAt calls in the compiler as needed. This opens the door towards reporting the error code and/or providing a link/reference to more detailed explanations (see internal/types/errors/codes.go). Change-Id: I0ff9368d8163499ffdac6adfe8331fdc4a19b4b3 Reviewed-on: https://go-review.googlesource.com/c/go/+/475198 Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 3790cec commit 2cbab4e

File tree

20 files changed

+74
-65
lines changed

20 files changed

+74
-65
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package base
77
import (
88
"fmt"
99
"internal/buildcfg"
10+
"internal/types/errors"
1011
"os"
1112
"runtime/debug"
1213
"sort"
@@ -105,11 +106,11 @@ func sameline(a, b src.XPos) bool {
105106

106107
// Errorf reports a formatted error at the current line.
107108
func Errorf(format string, args ...interface{}) {
108-
ErrorfAt(Pos, format, args...)
109+
ErrorfAt(Pos, 0, format, args...)
109110
}
110111

111112
// ErrorfAt reports a formatted error message at pos.
112-
func ErrorfAt(pos src.XPos, format string, args ...interface{}) {
113+
func ErrorfAt(pos src.XPos, code errors.Code, format string, args ...interface{}) {
113114
msg := fmt.Sprintf(format, args...)
114115

115116
if strings.HasPrefix(msg, "syntax error") {

src/cmd/compile/internal/escape/escape.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (b *batch) finish(fns []*ir.Func) {
303303
if loc.escapes {
304304
if n.Op() == ir.ONAME {
305305
if base.Flag.CompilingRuntime {
306-
base.ErrorfAt(n.Pos(), "%v escapes to heap, not allowed in runtime", n)
306+
base.ErrorfAt(n.Pos(), 0, "%v escapes to heap, not allowed in runtime", n)
307307
}
308308
if base.Flag.LowerM != 0 {
309309
base.WarnfAt(n.Pos(), "moved to heap: %v", n)

src/cmd/compile/internal/escape/graph.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (e *escape) newLoc(n ir.Node, transient bool) *location {
218218
base.Fatalf("e.curfn isn't set")
219219
}
220220
if n != nil && n.Type() != nil && n.Type().NotInHeap() {
221-
base.ErrorfAt(n.Pos(), "%v is incomplete (or unallocatable); stack allocation disallowed", n.Type())
221+
base.ErrorfAt(n.Pos(), 0, "%v is incomplete (or unallocatable); stack allocation disallowed", n.Type())
222222
}
223223

224224
if n != nil && n.Op() == ir.ONAME {

src/cmd/compile/internal/ir/func.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func ClosureDebugRuntimeCheck(clo *ClosureExpr) {
331331
}
332332
}
333333
if base.Flag.CompilingRuntime && clo.Esc() == EscHeap && !clo.IsGoWrap {
334-
base.ErrorfAt(clo.Pos(), "heap-allocated closure %s, not allowed in runtime", FuncName(clo.Func))
334+
base.ErrorfAt(clo.Pos(), 0, "heap-allocated closure %s, not allowed in runtime", FuncName(clo.Func))
335335
}
336336
}
337337

src/cmd/compile/internal/noder/irgen.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package noder
66

77
import (
88
"fmt"
9+
"internal/types/errors"
910
"regexp"
1011
"sort"
1112

@@ -47,7 +48,7 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
4748
if versionErrorRx.MatchString(msg) {
4849
msg = fmt.Sprintf("%s (-lang was set to %s; check go.mod)", msg, base.Flag.Lang)
4950
}
50-
base.ErrorfAt(m.makeXPos(terr.Pos), "%s", msg)
51+
base.ErrorfAt(m.makeXPos(terr.Pos), terr.Code, "%s", msg)
5152
},
5253
Importer: &importer,
5354
Sizes: &gcSizes{},
@@ -72,7 +73,7 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
7273
syntax.Inspect(file, func(n syntax.Node) bool {
7374
if n, ok := n.(*syntax.InterfaceType); ok {
7475
if f.hasCycle(n.GetTypeInfo().Type.(*types2.Interface)) {
75-
base.ErrorfAt(m.makeXPos(n.Pos()), "invalid recursive type: anonymous interface refers to itself (see https://go.dev/issue/56103)")
76+
base.ErrorfAt(m.makeXPos(n.Pos()), errors.InvalidTypeCycle, "invalid recursive type: anonymous interface refers to itself (see https://go.dev/issue/56103)")
7677

7778
for typ := range f.cyclic {
7879
f.cyclic[typ] = false // suppress duplicate errors
@@ -106,7 +107,7 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
106107
return ti.pos.Before(tj.pos)
107108
})
108109
for _, targ := range nihTargs {
109-
base.ErrorfAt(targ.pos, "cannot use incomplete (or unallocatable) type as a type argument: %v", targ.typ)
110+
base.ErrorfAt(targ.pos, 0, "cannot use incomplete (or unallocatable) type as a type argument: %v", targ.typ)
110111
}
111112
}
112113

src/cmd/compile/internal/noder/noder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func LoadPackage(filenames []string) {
6565
var m posMap
6666
for _, p := range noders {
6767
for e := range p.err {
68-
base.ErrorfAt(m.makeXPos(e.Pos), "%s", e.Msg)
68+
base.ErrorfAt(m.makeXPos(e.Pos), 0, "%s", e.Msg)
6969
}
7070
if p.file == nil {
7171
base.ErrorExit()

src/cmd/compile/internal/noder/writer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func newPkgWriter(m posMap, pkg *types2.Package, info *types2.Info) *pkgWriter {
108108

109109
// errorf reports a user error about thing p.
110110
func (pw *pkgWriter) errorf(p poser, msg string, args ...interface{}) {
111-
base.ErrorfAt(pw.m.pos(p), msg, args...)
111+
base.ErrorfAt(pw.m.pos(p), 0, msg, args...)
112112
}
113113

114114
// fatalf reports an internal compiler error about thing p.

src/cmd/compile/internal/pkginit/initorder.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package pkginit
77
import (
88
"container/heap"
99
"fmt"
10+
"internal/types/errors"
1011
"strings"
1112

1213
"cmd/compile/internal/base"
@@ -243,7 +244,7 @@ func reportInitLoopAndExit(l []*ir.Name) {
243244
}
244245
fmt.Fprintf(&msg, "\t%v: %v", ir.Line(l[0]), l[0])
245246

246-
base.ErrorfAt(l[0].Pos(), msg.String())
247+
base.ErrorfAt(l[0].Pos(), errors.InvalidInitCycle, msg.String())
247248
base.ErrorExit()
248249
}
249250

src/cmd/compile/internal/ssagen/abi.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (s *SymABIs) GenABIWrappers() {
146146
defABI, hasDefABI := s.defs[symName]
147147
if hasDefABI {
148148
if len(fn.Body) != 0 {
149-
base.ErrorfAt(fn.Pos(), "%v defined in both Go and assembly", fn)
149+
base.ErrorfAt(fn.Pos(), 0, "%v defined in both Go and assembly", fn)
150150
}
151151
fn.ABI = defABI
152152
}
@@ -251,7 +251,7 @@ func makeABIWrapper(f *ir.Func, wrapperABI obj.ABI) {
251251
// below to handle the receiver. Panic if we see this scenario.
252252
ft := f.Nname.Type()
253253
if ft.NumRecvs() != 0 {
254-
base.ErrorfAt(f.Pos(), "makeABIWrapper support for wrapping methods not implemented")
254+
base.ErrorfAt(f.Pos(), 0, "makeABIWrapper support for wrapping methods not implemented")
255255
return
256256
}
257257

src/cmd/compile/internal/ssagen/nowb.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (c *nowritebarrierrecChecker) check() {
154154
}
155155
// Check go:nowritebarrier functions.
156156
if fn.Pragma&ir.Nowritebarrier != 0 && fn.WBPos.IsKnown() {
157-
base.ErrorfAt(fn.WBPos, "write barrier prohibited")
157+
base.ErrorfAt(fn.WBPos, 0, "write barrier prohibited")
158158
}
159159
}
160160

@@ -185,7 +185,7 @@ func (c *nowritebarrierrecChecker) check() {
185185
fmt.Fprintf(&err, "\n\t%v: called by %v", base.FmtPos(call.lineno), call.target.Nname)
186186
call = funcs[call.target]
187187
}
188-
base.ErrorfAt(fn.WBPos, "write barrier prohibited by caller; %v%s", fn.Nname, err.String())
188+
base.ErrorfAt(fn.WBPos, 0, "write barrier prohibited by caller; %v%s", fn.Nname, err.String())
189189
continue
190190
}
191191

src/cmd/compile/internal/ssagen/pgen.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ func CheckLargeStacks() {
327327
})
328328
for _, large := range largeStackFrames {
329329
if large.callee != 0 {
330-
base.ErrorfAt(large.pos, "stack frame too large (>1GB): %d MB locals + %d MB args + %d MB callee", large.locals>>20, large.args>>20, large.callee>>20)
330+
base.ErrorfAt(large.pos, 0, "stack frame too large (>1GB): %d MB locals + %d MB args + %d MB callee", large.locals>>20, large.args>>20, large.callee>>20)
331331
} else {
332-
base.ErrorfAt(large.pos, "stack frame too large (>1GB): %d MB locals + %d MB args", large.locals>>20, large.args>>20)
332+
base.ErrorfAt(large.pos, 0, "stack frame too large (>1GB): %d MB locals + %d MB args", large.locals>>20, large.args>>20)
333333
}
334334
}
335335
}

src/cmd/compile/internal/staticdata/data.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func dstringdata(s *obj.LSym, off int, t string, pos src.XPos, what string) int
214214
// causing a cryptic error message by the linker. Check for oversize objects here
215215
// and provide a useful error message instead.
216216
if int64(len(t)) > 2e9 {
217-
base.ErrorfAt(pos, "%v with length %v is too big", what, len(t))
217+
base.ErrorfAt(pos, 0, "%v with length %v is too big", what, len(t))
218218
return 0
219219
}
220220

src/cmd/compile/internal/staticdata/embed.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func embedFileList(v *ir.Name, kind int) []string {
3131
for _, pattern := range e.Patterns {
3232
files, ok := base.Flag.Cfg.Embed.Patterns[pattern]
3333
if !ok {
34-
base.ErrorfAt(e.Pos, "invalid go:embed: build system did not map pattern: %s", pattern)
34+
base.ErrorfAt(e.Pos, 0, "invalid go:embed: build system did not map pattern: %s", pattern)
3535
}
3636
for _, file := range files {
3737
if base.Flag.Cfg.Embed.Files[file] == "" {
38-
base.ErrorfAt(e.Pos, "invalid go:embed: build system did not map file: %s", file)
38+
base.ErrorfAt(e.Pos, 0, "invalid go:embed: build system did not map file: %s", file)
3939
continue
4040
}
4141
if !have[file] {
@@ -57,7 +57,7 @@ func embedFileList(v *ir.Name, kind int) []string {
5757

5858
if kind == embedString || kind == embedBytes {
5959
if len(list) > 1 {
60-
base.ErrorfAt(v.Pos(), "invalid go:embed: multiple files for type %v", v.Type())
60+
base.ErrorfAt(v.Pos(), 0, "invalid go:embed: multiple files for type %v", v.Type())
6161
return nil
6262
}
6363
}
@@ -109,12 +109,12 @@ func WriteEmbed(v *ir.Name) {
109109

110110
commentPos := (*v.Embed)[0].Pos
111111
if base.Flag.Cfg.Embed.Patterns == nil {
112-
base.ErrorfAt(commentPos, "invalid go:embed: build system did not supply embed configuration")
112+
base.ErrorfAt(commentPos, 0, "invalid go:embed: build system did not supply embed configuration")
113113
return
114114
}
115115
kind := embedKind(v.Type())
116116
if kind == embedUnknown {
117-
base.ErrorfAt(v.Pos(), "go:embed cannot apply to var of type %v", v.Type())
117+
base.ErrorfAt(v.Pos(), 0, "go:embed cannot apply to var of type %v", v.Type())
118118
return
119119
}
120120

@@ -124,7 +124,7 @@ func WriteEmbed(v *ir.Name) {
124124
file := files[0]
125125
fsym, size, err := fileStringSym(v.Pos(), base.Flag.Cfg.Embed.Files[file], kind == embedString, nil)
126126
if err != nil {
127-
base.ErrorfAt(v.Pos(), "embed %s: %v", file, err)
127+
base.ErrorfAt(v.Pos(), 0, "embed %s: %v", file, err)
128128
}
129129
sym := v.Linksym()
130130
off := 0
@@ -160,7 +160,7 @@ func WriteEmbed(v *ir.Name) {
160160
} else {
161161
fsym, size, err := fileStringSym(v.Pos(), base.Flag.Cfg.Embed.Files[file], true, hash)
162162
if err != nil {
163-
base.ErrorfAt(v.Pos(), "embed %s: %v", file, err)
163+
base.ErrorfAt(v.Pos(), 0, "embed %s: %v", file, err)
164164
}
165165
off = objw.SymPtr(slicedata, off, fsym, 0) // data string
166166
off = objw.Uintptr(slicedata, off, uint64(size))

src/cmd/compile/internal/typecheck/const.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"go/constant"
1010
"go/token"
11+
"internal/types/errors"
1112
"math"
1213
"math/big"
1314
"strings"
@@ -567,7 +568,7 @@ func OrigConst(n ir.Node, v constant.Value) ir.Node {
567568
if what == "" {
568569
base.Fatalf("unexpected overflow: %v", n.Op())
569570
}
570-
base.ErrorfAt(n.Pos(), "constant %v overflow", what)
571+
base.ErrorfAt(n.Pos(), errors.NumericOverflow, "constant %v overflow", what)
571572
n.SetType(nil)
572573
return n
573574
}

src/cmd/compile/internal/typecheck/dcl.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package typecheck
66

77
import (
88
"fmt"
9+
"internal/types/errors"
910
"sync"
1011

1112
"cmd/compile/internal/base"
@@ -48,15 +49,15 @@ func Declare(n *ir.Name, ctxt ir.Class) {
4849

4950
// kludgy: TypecheckAllowed means we're past parsing. Eg reflectdata.methodWrapper may declare out of package names later.
5051
if !inimport && !TypecheckAllowed && s.Pkg != types.LocalPkg {
51-
base.ErrorfAt(n.Pos(), "cannot declare name %v", s)
52+
base.ErrorfAt(n.Pos(), 0, "cannot declare name %v", s)
5253
}
5354

5455
if ctxt == ir.PEXTERN {
5556
if s.Name == "init" {
56-
base.ErrorfAt(n.Pos(), "cannot declare init - must be func")
57+
base.ErrorfAt(n.Pos(), errors.InvalidInitDecl, "cannot declare init - must be func")
5758
}
5859
if s.Name == "main" && s.Pkg.Name == "main" {
59-
base.ErrorfAt(n.Pos(), "cannot declare main - must be func")
60+
base.ErrorfAt(n.Pos(), errors.InvalidMainDecl, "cannot declare main - must be func")
6061
}
6162
Target.Externs = append(Target.Externs, n)
6263
s.Def = n
@@ -154,7 +155,7 @@ func checkdupfields(what string, fss ...[]*types.Field) {
154155
continue
155156
}
156157
if seen[f.Sym] {
157-
base.ErrorfAt(f.Pos, "duplicate %s %s", what, f.Sym.Name)
158+
base.ErrorfAt(f.Pos, errors.DuplicateFieldAndMethod, "duplicate %s %s", what, f.Sym.Name)
158159
continue
159160
}
160161
seen[f.Sym] = true

0 commit comments

Comments
 (0)