Skip to content

Commit 74e32ac

Browse files
aykevldeadprogram
authored andcommitted
compiler: improve error locations in goroutine lowering
1 parent 072f8c3 commit 74e32ac

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

compiler/goroutine-lowering.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ package compiler
104104
// scheduler, which runs in the background scheduling all coroutines.
105105

106106
import (
107-
"errors"
108107
"fmt"
109108
"strings"
110109

@@ -283,7 +282,7 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
283282
if use.IsConstant() && use.Opcode() == llvm.PtrToInt {
284283
for _, call := range getUses(use) {
285284
if call.IsACallInst().IsNil() || call.CalledValue().Name() != "runtime.makeGoroutine" {
286-
return false, errors.New("async function " + f.Name() + " incorrectly used in ptrtoint, expected runtime.makeGoroutine")
285+
return false, errorAt(call, "async function incorrectly used in ptrtoint, expected runtime.makeGoroutine")
287286
}
288287
}
289288
// This is a go statement. Do not mark the parent as async, as
@@ -303,12 +302,19 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
303302
// Not a call instruction. Maybe a store to a global? In any
304303
// case, this requires support for async calls across function
305304
// pointers which is not yet supported.
306-
return false, errors.New("async function " + f.Name() + " used as function pointer")
305+
at := use
306+
if use.IsAInstruction().IsNil() {
307+
// The use might not be an instruction (for example, in the
308+
// case of a const bitcast). Fall back to reporting the
309+
// location of the function instead.
310+
at = f
311+
}
312+
return false, errorAt(at, "async function "+f.Name()+" used as function pointer")
307313
}
308314
parent := use.InstructionParent().Parent()
309315
for i := 0; i < use.OperandsCount()-1; i++ {
310316
if use.Operand(i) == f {
311-
return false, errors.New("async function " + f.Name() + " used as function pointer in " + parent.Name())
317+
return false, errorAt(use, "async function "+f.Name()+" used as function pointer")
312318
}
313319
}
314320
worklist = append(worklist, parent)
@@ -906,12 +912,12 @@ func (c *Compiler) lowerMakeGoroutineCalls(sched bool) error {
906912
origFunc := ptrtointIn.Operand(0)
907913
uses := getUses(goroutine)
908914
if len(uses) != 1 || uses[0].IsAIntToPtrInst().IsNil() {
909-
return errors.New("expected exactly 1 inttoptr use of runtime.makeGoroutine")
915+
return errorAt(makeGoroutine, "expected exactly 1 inttoptr use of runtime.makeGoroutine")
910916
}
911917
inttoptrOut := uses[0]
912918
uses = getUses(inttoptrOut)
913919
if len(uses) != 1 || uses[0].IsACallInst().IsNil() {
914-
return errors.New("expected exactly 1 call use of runtime.makeGoroutine bitcast")
920+
return errorAt(inttoptrOut, "expected exactly 1 call use of runtime.makeGoroutine bitcast")
915921
}
916922
realCall := uses[0]
917923

0 commit comments

Comments
 (0)