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 toConstantSized #3446

Merged
merged 2 commits into from
Jul 8, 2024
Merged
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
62 changes: 39 additions & 23 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
@@ -3483,25 +3483,29 @@ func (v *ArrayValue) ToVariableSized(
interpreter *Interpreter,
locationRange LocationRange,
) Value {
var returnArrayStaticType ArrayStaticType
switch v.Type.(type) {
case *ConstantSizedStaticType:
returnArrayStaticType = NewVariableSizedStaticType(
interpreter,
v.Type.ElementType(),
)
default:

// Convert the constant-sized array type to a variable-sized array type.

constantSizedType, ok := v.Type.(*ConstantSizedStaticType)
if !ok {
panic(errors.NewUnreachableError())
}

variableSizedType := NewVariableSizedStaticType(
interpreter,
constantSizedType.Type,
)

// Convert the array to a variable-sized array.

iterator, err := v.array.Iterator()
if err != nil {
panic(errors.NewExternalError(err))
}

return NewArrayValueWithIterator(
interpreter,
returnArrayStaticType,
variableSizedType,
common.ZeroAddress,
uint64(v.Count()),
func() Value {
@@ -3536,33 +3540,41 @@ func (v *ArrayValue) ToConstantSized(
interpreter *Interpreter,
locationRange LocationRange,
expectedConstantSizedArraySize int64,
) Value {
if int64(v.Count()) != expectedConstantSizedArraySize {
) OptionalValue {

// Ensure the array has the expected size.

count := v.Count()

if int64(count) != expectedConstantSizedArraySize {
return NilOptionalValue
}

var returnArrayStaticType ArrayStaticType
switch v.Type.(type) {
case *VariableSizedStaticType:
returnArrayStaticType = NewConstantSizedStaticType(
interpreter,
v.Type.ElementType(),
expectedConstantSizedArraySize,
)
default:
// Convert the variable-sized array type to a constant-sized array type.

variableSizedType, ok := v.Type.(*VariableSizedStaticType)
if !ok {
panic(errors.NewUnreachableError())
}

constantSizedType := NewConstantSizedStaticType(
interpreter,
variableSizedType.Type,
expectedConstantSizedArraySize,
)

// Convert the array to a constant-sized array.

iterator, err := v.array.Iterator()
if err != nil {
panic(errors.NewExternalError(err))
}

return NewArrayValueWithIterator(
constantSizedArray := NewArrayValueWithIterator(
interpreter,
returnArrayStaticType,
constantSizedType,
common.ZeroAddress,
uint64(v.Count()),
uint64(count),
func() Value {

// Meter computation for iterating the array.
@@ -3589,6 +3601,10 @@ func (v *ArrayValue) ToConstantSized(
)
},
)

// Return the constant-sized array as an optional value.

return NewSomeValueNonCopying(interpreter, constantSizedArray)
}

func (v *ArrayValue) SetType(staticType ArrayStaticType) {
37 changes: 37 additions & 0 deletions runtime/tests/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
@@ -11518,6 +11518,43 @@ func TestInterpretArrayToConstantSized(t *testing.T) {
),
)
})

t.Run("ensure result is optional", func(t *testing.T) {
t.Parallel()

baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation)
baseValueActivation.DeclareValue(stdlib.PanicFunction)

baseActivation := activations.NewActivation(nil, interpreter.BaseActivation)
interpreter.Declare(baseActivation, stdlib.PanicFunction)

inter, err := parseCheckAndInterpretWithOptions(t,
`
fun test(): [UInt8; 20] {
return "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
.decodeHex()
.toConstantSized<[UInt8; 20]>()
?? panic("toConstantSized failed")
}
`,
ParseCheckAndInterpretOptions{
CheckerConfig: &sema.Config{
BaseValueActivationHandler: func(_ common.Location) *sema.VariableActivation {
return baseValueActivation
},
},
Config: &interpreter.Config{
BaseActivationHandler: func(_ common.Location) *interpreter.VariableActivation {
return baseActivation
},
},
},
)
require.NoError(t, err)

_, err = inter.Invoke("test")
require.NoError(t, err)
})
}

func TestInterpretCastingBoxing(t *testing.T) {