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

Forbid interface removals #3380

Merged
merged 2 commits into from
May 29, 2024
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
42 changes: 40 additions & 2 deletions runtime/contract_update_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3246,21 +3246,59 @@ func TestPragmaUpdates(t *testing.T) {
const oldCode = `
access(all) contract Test {
access(all) resource R {}
access(all) struct interface I {}
access(all) struct S {}
}
`

const newCode = `
access(all) contract Test {
#removedType(R)
#removedType(I)
#removedType(S)
}
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, withC1Upgrade)
require.NoError(t, err)
})

testWithValidators(t, "#removedType does not allow resource interface type removal", func(t *testing.T, withC1Upgrade bool) {

const oldCode = `
access(all) contract Test {
access(all) resource interface R {}
}
`

const newCode = `
access(all) contract Test {
#removedType(R)
}
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, withC1Upgrade)
var expectedErr *stdlib.MissingDeclarationError
require.ErrorAs(t, err, &expectedErr)
})

testWithValidators(t, "#removedType does not allow struct interface type removal", func(t *testing.T, withC1Upgrade bool) {

const oldCode = `
access(all) contract Test {
access(all) struct interface S {}
}
`

const newCode = `
access(all) contract Test {
#removedType(S)
}
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, withC1Upgrade)
var expectedErr *stdlib.MissingDeclarationError
require.ErrorAs(t, err, &expectedErr)
})

testWithValidators(t, "#removedType can be added", func(t *testing.T, withC1Upgrade bool) {

const oldCode = `
Expand Down
11 changes: 7 additions & 4 deletions runtime/stdlib/contract_update_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,22 @@ func (validator *ContractUpdateValidator) checkNestedDeclarationRemoval(
newContainingDeclaration ast.Declaration,
removedTypes *orderedmap.OrderedMap[string, struct{}],
) {
declarationKind := nestedDeclaration.DeclarationKind()

// OK to remove events - they are not stored
if nestedDeclaration.DeclarationKind() == common.DeclarationKindEvent {
if declarationKind == common.DeclarationKindEvent {
return
}

// OK to remove a type if it is included in a #removedType pragma
if removedTypes.Contains(nestedDeclaration.DeclarationIdentifier().Identifier) {
// OK to remove a type if it is included in a #removedType pragma, and it is not an interface
if removedTypes.Contains(nestedDeclaration.DeclarationIdentifier().Identifier) &&
!declarationKind.IsInterfaceDeclaration() {
return
}

validator.report(&MissingDeclarationError{
Name: nestedDeclaration.DeclarationIdentifier().Identifier,
Kind: nestedDeclaration.DeclarationKind(),
Kind: declarationKind,
Range: ast.NewUnmeteredRangeFromPositioned(
newContainingDeclaration.DeclarationIdentifier(),
),
Expand Down
Loading