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

Conversation

lock9
Copy link

@lock9 lock9 commented Jan 6, 2025

Problem

The compiler may fail without informing where the error is coming from. I found at least two places that can be fixed.

Solution

Include the file line and position of the error message

Output example:

error while trying to compile smart contract file: .../abstract.go:101:9: '&' can be used only with struct literals

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

@roman-khimov roman-khimov added compiler Go smart contract compiler I3 Minimal impact U4 Nothing urgent enhancement Improving existing functionality S4 Routine labels Jan 6, 2025
Copy link

codecov bot commented Jan 9, 2025

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Project coverage is 83.07%. Comparing base (4d2b88d) to head (9713b67).

Files with missing lines Patch % Lines
pkg/compiler/codegen.go 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3780      +/-   ##
==========================================
- Coverage   83.09%   83.07%   -0.02%     
==========================================
  Files         335      335              
  Lines       46873    46875       +2     
==========================================
- Hits        38949    38942       -7     
- Misses       6337     6344       +7     
- Partials     1587     1589       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

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

A nice extension.

@@ -1158,7 +1159,8 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
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)
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.

@@ -1158,7 +1159,8 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
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)
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Go smart contract compiler enhancement Improving existing functionality I3 Minimal impact S4 Routine U4 Nothing urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants