diff --git a/src/runtime/panic.go b/src/runtime/panic.go index eb49c12c0..12a139967 100644 --- a/src/runtime/panic.go +++ b/src/runtime/panic.go @@ -1,5 +1,6 @@ package runtime +// Builtin function panic(msg), used as a compiler intrinsic. func _panic(message interface{}) { printstring("panic: ") printitf(message) @@ -7,28 +8,30 @@ func _panic(message interface{}) { abort() } +// Cause a runtime panic, which is (currently) always a string. +func runtimePanic(msg string) { + printstring("panic: runtime error: ") + println(msg) + abort() +} + // Check for bounds in *ssa.Index, *ssa.IndexAddr and *ssa.Lookup. func lookupBoundsCheck(length, index int) { if index < 0 || index >= length { - // printstring() here is safe as this function is excluded from bounds - // checking. - printstring("panic: runtime error: index out of range\n") - abort() + runtimePanic("index out of range") } } // Check for bounds in *ssa.Slice. func sliceBoundsCheck(length, low, high uint) { if !(0 <= low && low <= high && high <= length) { - printstring("panic: runtime error: slice out of range\n") - abort() + runtimePanic("slice out of range") } } // Check for bounds in *ssa.MakeSlice. func sliceBoundsCheckMake(length, capacity uint) { if !(0 <= length && length <= capacity) { - printstring("panic: runtime error: slice size out of range\n") - abort() + runtimePanic("slice size out of range") } } diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index 2c072022d..fb59175c9 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -48,7 +48,7 @@ func abort() { func alloc(size uintptr) unsafe.Pointer { buf := _Cfunc_calloc(1, size) if buf == nil { - panic("cannot allocate memory") + runtimePanic("cannot allocate memory") } return buf }