lang: ast, gapi, interfaces, parser: Print line numbers on error

This adds an initial implementation of printing line numbers on type
unification errors. It also attempts to print a visual position
indicator for most scenarios.

This patch was started by Felix Frank and finished by James Shubin.

Co-authored-by: Felix Frank <Felix.Frank.de@gmail.com>
This commit is contained in:
James Shubin
2025-02-25 20:15:02 -05:00
parent f754bbbf90
commit d7ecc72b41
20 changed files with 714 additions and 172 deletions

View File

@@ -134,8 +134,15 @@ func (obj *FastInvariantSolver) Solve(ctx context.Context, data *unification.Dat
if err := unificationUtil.Unify(x.Expect, x.Actual); err != nil {
// Storing the Expr with this invariant is so that we
// can generate this more helpful error message here.
// TODO: Improve this error message!
return nil, errwrap.Wrapf(err, "unify error with: %s", x.Expr)
displayer, ok := x.Node.(interfaces.TextDisplayer)
if !ok {
obj.Logf("not displayable: %v\n", x.Node)
return nil, errwrap.Wrapf(err, "unify error with: %s", x.Expr)
}
if highlight := displayer.HighlightText(); highlight != "" {
obj.Logf("%s: %s", err.Error(), highlight)
}
return nil, fmt.Errorf("%s: %s", err.Error(), displayer.Byline())
}
if obj.Debug {
e1, e2 := unificationUtil.Extract(x.Expect), unificationUtil.Extract(x.Actual)

View File

@@ -859,6 +859,11 @@ func TestUnification1(t *testing.T) {
data := &interfaces.Data{
// TODO: add missing fields here if/when needed
Metadata: &interfaces.Metadata{},
SourceFinder: func(string) ([]byte, error) {
return nil, fmt.Errorf("not implemented")
},
Debug: testing.Verbose(), // set via the -test.v flag to `go test`
Logf: func(format string, v ...interface{}) {
t.Logf(fmt.Sprintf("test #%d", index)+": ast: "+format, v...)