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

[Checker] check and reject composite types with non-storable fields #180

Merged
merged 20 commits into from
Jun 30, 2020

Conversation

zhangchiqing
Copy link
Member

@zhangchiqing zhangchiqing commented Jun 26, 2020

Working towards #62 #169

Description

  • Add a check in checker to reject composite types with non-storable fields.

For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@zhangchiqing zhangchiqing marked this pull request as ready for review June 26, 2020 22:09
@turbolent turbolent added the Language Breaking Change Breaks Cadence contracts deployed on Mainnet label Jun 26, 2020
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Great work!

A few suggestions and changes that need to be made

runtime/sema/check_composite_declaration.go Outdated Show resolved Hide resolved
runtime/sema/check_composite_declaration.go Outdated Show resolved Hide resolved
Comment on lines 577 to 588
// check if all members' type are allowed to be the fields
for _, member := range members {
// if a member is a non-storable field, then report error
if !member.IsStorable() {
err := &FieldTypeNotStorableError{
Name: member.Identifier.Identifier,
Type: member.TypeAnnotation.Type,
Pos: member.Identifier.Pos,
}
checker.report(err)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Consider refactoring this into a function

runtime/sema/check_composite_declaration.go Outdated Show resolved Hide resolved
runtime/sema/check_composite_declaration.go Outdated Show resolved Hide resolved
runtime/tests/checker/composite_type_fields_test.go Outdated Show resolved Hide resolved
Comment on lines 222 to 230
t.Run("check error message", func(t *testing.T) {
testcase := cases["function is not a storable field"]
_, err := ParseAndCheck(t, testcase.code)
require.Error(t, err)

checkerError, _ := err.(*sema.CheckerError)
require.Equal(t, "field fn is not storable, type: ((): Int)",
checkerError.ChildErrors()[0].Error())
})
Copy link
Member

Choose a reason for hiding this comment

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

We don't check error messages. I'd remove this.

If you want to keep it, like above, use errs := ExpectCheckerErrors(t, err, 1) and then errs[0]. This properly checks if the error occurred and the correct number of child errors exist.

In the current code checkerError can be nil, so ChildErrors() will crash.
If it is non-nil, ChildErrors() could also return an empty slice, so the indexing ([0]) will crash

// print the failed the cadence code if test case was broken
require.NoError(t, err, errmsg)
} else {
require.Error(t, err, errmsg)
Copy link
Member

Choose a reason for hiding this comment

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

Check the actual errors here, not just that something went wrong: The checker could reject the tests for other reasons.

The common pattern is:

		errs := ExpectCheckerErrors(t, err, 1)

		assert.IsType(t, &sema.FieldTypeNotStorableError{}, errs[0])

ExpectCheckerErrors ensures the error is an error and has the correct number of child errors

runtime/tests/checker/resources_test.go Outdated Show resolved Hide resolved
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Language Breaking Change Breaks Cadence contracts deployed on Mainnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants