Skip to content

Commit

Permalink
Require converting empty set T{} to T
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Feb 16, 2024
1 parent d00bb4d commit 74b1010
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,36 @@ func TestContractUpgradeIntersectionFieldType(t *testing.T) {
require.NoError(t, err)
})

t.Run("AnyResource restricted type, without restrictions", func(t *testing.T) {

t.Parallel()

const oldCode = `
pub contract Test {
pub resource R {}
pub var a: @AnyResource{}
init() {
self.a <- create R()
}
}
`

const newCode = `
access(all) contract Test {
access(all) resource R {}
access(all) var a: @AnyResource
init() {
self.a <- create R()
}
}
`

err := testContractUpdate(t, oldCode, newCode)
require.NoError(t, err)
})

t.Run("change field type restricted type variable sized", func(t *testing.T) {

t.Parallel()
Expand Down
15 changes: 10 additions & 5 deletions runtime/stdlib/cadence_v0.42_to_v1_contract_upgrade_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,22 @@ typeSwitch:
validator.currentRestrictedTypeUpgradeRestrictions = oldType.Types

// If the old restricted type is for AnyStruct/AnyResource,
// require them to drop the "restricted type".
// e.g: `T{I} -> {I}`
// and if there are atleast one restriction, require them to drop the "restricted type".
// e.g-1: `AnyStruct{I} -> {I}`
// e.g-2: `AnyResource{I} -> {I}`
// See: https://github.com/onflow/cadence/issues/3112
if restrictedNominalType, isNominal := oldType.LegacyRestrictedType.(*ast.NominalType); isNominal {
switch restrictedNominalType.Identifier.Identifier {
case "AnyStruct", "AnyResource":
break typeSwitch
if len(oldType.Types) > 0 {
break typeSwitch
}
}
}

// Otherwise require them to drop the "restriction".
// e.g: `T{I} -> T`
// Otherwise require them to drop the "restrictions".
// e.g-1: `T{I} -> T`
// e.g-2: `AnyStruct{} -> AnyStruct`
return validator.checkTypeUpgradability(oldType.LegacyRestrictedType, newType)

case *ast.VariableSizedType:
Expand Down

0 comments on commit 74b1010

Please sign in to comment.