]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile: add base.Assertf{,At} functions
authorMatthew Dempsky <mdempsky@google.com>
Tue, 20 Jul 2021 20:28:12 +0000 (13:28 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 20 Jul 2021 23:11:40 +0000 (23:11 +0000)
We have almost 200 uses of the "assert" helper functions in noder and
typecheck. Clearly the tiny bit of extra convenience of writing a
one-line assertion rather than an if+panic is helpful, so we might as
well add functions for this to base itself so that it's easier to
write more informative error messages.

Change-Id: I06e2db2f0455af063937b25a53ca42f9413cf496
Reviewed-on: https://go-review.googlesource.com/c/go/+/336050
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/base/print.go
src/cmd/compile/internal/noder/stencil.go
src/cmd/compile/internal/typecheck/subr.go

index b095fd704daad807b3847bbd2916024356161af2..4afe2eb9eea3154f83456ed4ed282f04d7d1ab4a 100644 (file)
@@ -233,6 +233,27 @@ func FatalfAt(pos src.XPos, format string, args ...interface{}) {
        ErrorExit()
 }
 
+// Assert reports "assertion failed" with Fatalf, unless b is true.
+func Assert(b bool) {
+       if !b {
+               Fatalf("assertion failed")
+       }
+}
+
+// Assertf reports a fatal error with Fatalf, unless b is true.
+func Assertf(b bool, format string, args ...interface{}) {
+       if !b {
+               Fatalf(format, args...)
+       }
+}
+
+// AssertfAt reports a fatal error with FatalfAt, unless b is true.
+func AssertfAt(b bool, pos src.XPos, format string, args ...interface{}) {
+       if !b {
+               FatalfAt(pos, format, args...)
+       }
+}
+
 // hcrash crashes the compiler when -h is set, to find out where a message is generated.
 func hcrash() {
        if Flag.LowerH != 0 {
index 7eac8573c96943d2aa0f8f424c8ca84a82ecc4c6..72ecd80cf5d5a6d1c1761a957923e7f75d8d47b5 100644 (file)
@@ -23,9 +23,7 @@ import (
 )
 
 func assert(p bool) {
-       if !p {
-               panic("assertion failed")
-       }
+       base.Assert(p)
 }
 
 // Temporary - for outputting information on derived types, dictionaries, sub-dictionaries.
index d9e6612dfc3f53d5305bef76501f1c90ac758029..a795524b2bcdc21f8ef6266f932b3d034b169b26 100644 (file)
@@ -981,9 +981,7 @@ func MakeDictName(gf *types.Sym, targs []*types.Type, hasBrackets bool) *types.S
 }
 
 func assert(p bool) {
-       if !p {
-               panic("assertion failed")
-       }
+       base.Assert(p)
 }
 
 // General type substituter, for replacing typeparams with type args.