Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compiler error reporting #3780

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,8 @@
case *ast.StarExpr:
_, ok := c.getStruct(c.typeOf(n.X))
if !ok {
c.prog.Err = errors.New("dereferencing is only supported on structs")
pos := c.buildInfo.config.Fset.Position(n.Pos())
c.prog.Err = fmt.Errorf("%s: dereferencing is only supported on structs", pos)

Check warning on line 917 in pkg/compiler/codegen.go

View check run for this annotation

Codecov / codecov/patch

pkg/compiler/codegen.go#L916-L917

Added lines #L916 - L917 were not covered by tests
return nil
}
ast.Walk(c, n.X)
Expand Down Expand Up @@ -1158,7 +1159,8 @@
c.convertStruct(lit, true)
return nil
}
c.prog.Err = fmt.Errorf("'&' can be used only with struct literals")
pos := c.buildInfo.config.Fset.Position(n.Pos())
c.prog.Err = fmt.Errorf("%s: '&' can be used only with struct literals", pos)

Check warning on line 1163 in pkg/compiler/codegen.go

View check run for this annotation

Codecov / codecov/patch

pkg/compiler/codegen.go#L1162-L1163

Added lines #L1162 - L1163 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a couple of places where the same pattern may be applied (e.g. 20 lines below, try to search for c.prog.Err = usages). Let's move it to a helper and apply it to all relevant places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I didn't find test cases covering this error so I didn't add any new test.

You may construct your own test-case. Take as a reference any compiler unit-test, e.g.

func TestLT(t *testing.T) {

Use evalWithError to check the compilation error.

return nil
}

Expand Down
Loading