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

Fix resource field nesting check for enum raw value field #1459

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion runtime/sema/check_composite_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (checker *Checker) visitCompositeDeclaration(declaration *ast.CompositeDecl
}

fieldPositionGetter := func(name string) ast.Position {
return declaration.Members.FieldPosition(name, declaration.CompositeKind)
return compositeType.FieldPosition(name, declaration)
}

checker.checkResourceFieldNesting(
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/check_interface_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (checker *Checker) VisitInterfaceDeclaration(declaration *ast.InterfaceDecl
)

fieldPositionGetter := func(name string) ast.Position {
return declaration.Members.FieldPosition(name, declaration.CompositeKind)
return interfaceType.FieldPosition(name, declaration)
}

checker.checkResourceFieldNesting(
Expand Down
14 changes: 12 additions & 2 deletions runtime/sema/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,8 +1733,8 @@ func (checker *Checker) checkResourceFieldNesting(
return
}

// The field is not a resource or contract, check if there are
// any fields that have a resource type and report them
// The field is not a resource or contract.
// Check if there are any fields that have a resource type and report them

members.Foreach(func(name string, member *Member) {
// NOTE: check type, not resource annotation:
Expand All @@ -1744,6 +1744,16 @@ func (checker *Checker) checkResourceFieldNesting(
return
}

// Skip enums' implicit rawValue field.
// If a resource type is used as the enum raw type,
// it is already reported

if compositeKind == common.CompositeKindEnum &&
name == EnumRawValueFieldName {

return
}

pos := fieldPositionGetter(name)

checker.report(
Expand Down
18 changes: 18 additions & 0 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3854,6 +3854,20 @@ func (t *CompositeType) initializeMemberResolvers() {
})
}

func (t *CompositeType) FieldPosition(name string, declaration *ast.CompositeDeclaration) ast.Position {
var pos ast.Position
if t.Kind == common.CompositeKindEnum &&
name == EnumRawValueFieldName {

if len(declaration.Conformances) > 0 {
pos = declaration.Conformances[0].StartPosition()
}
} else {
pos = declaration.Members.FieldPosition(name, declaration.CompositeKind)
}
return pos
}

// Member

type Member struct {
Expand Down Expand Up @@ -4217,6 +4231,10 @@ func (t *InterfaceType) GetNestedTypes() *StringTypeOrderedMap {
return t.nestedTypes
}

func (t *InterfaceType) FieldPosition(name string, declaration *ast.InterfaceDeclaration) ast.Position {
return declaration.Members.FieldPosition(name, declaration.CompositeKind)
}

// DictionaryType consists of the key and value type
// for all key-value pairs in the dictionary:
// All keys have to be a subtype of the key type,
Expand Down
49 changes: 49 additions & 0 deletions runtime/tests/checker/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,55 @@ func TestCheckInvalidContractResourceFieldMove(t *testing.T) {
assert.IsType(t, &sema.InvalidNestedResourceMoveError{}, errs[0])
}

func TestCheckInvalidEnumResourceField(t *testing.T) {

t.Parallel()

t.Run("raw type given", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
resource R {}

enum E: R {}
`)

errs := ExpectCheckerErrors(t, err, 1)

require.IsType(t, &sema.InvalidEnumRawTypeError{}, errs[0])
})

t.Run("raw type given, nested", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
resource R {
enum E: R {}
}
`)

errs := ExpectCheckerErrors(t, err, 2)

require.IsType(t, &sema.InvalidNestedDeclarationError{}, errs[0])
require.IsType(t, &sema.InvalidEnumRawTypeError{}, errs[1])
})

t.Run("raw type not given", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
enum E {}
`)

errs := ExpectCheckerErrors(t, err, 1)

require.IsType(t, &sema.MissingEnumRawTypeError{}, errs[0])
})
}

// TestCheckResourceInterfaceConformance tests the check
// of conformance of resources to resource interfaces.
//
Expand Down