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

Decouple interpreter values from interpreter – Part 3 #3751

Merged
merged 6 commits into from
Feb 13, 2025
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
15 changes: 14 additions & 1 deletion interpreter/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ import (
)

type TypeConverter interface {
MustConvertStaticToSemaType(staticType StaticType) sema.Type
ConvertStaticToSemaType(staticType StaticType) (sema.Type, error)
}

var _ TypeConverter = &Interpreter{}

func MustConvertStaticToSemaType(staticType StaticType, typeConverter TypeConverter) sema.Type {
semaType, err := typeConverter.ConvertStaticToSemaType(staticType)
if err != nil {
panic(err)
}
return semaType
}

func MustSemaTypeOfValue(value Value, context ValueStaticTypeContext) sema.Type {
staticType := value.StaticType(context)
return MustConvertStaticToSemaType(staticType, context)
}

type SubTypeChecker interface {
IsSubTypeOfSemaType(staticSubType StaticType, superType sema.Type) bool
}
Expand Down
58 changes: 24 additions & 34 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ func (interpreter *Interpreter) evaluateDefaultDestroyEvent(
if containingResourceComposite.Kind == common.CompositeKindAttachment {
var base *EphemeralReferenceValue
// in evaluation of destroy events, base and self are fully entitled, as the value must be owned
entitlementSupportingType, ok := interpreter.MustSemaTypeOfValue(containingResourceComposite).(sema.EntitlementSupportingType)
entitlementSupportingType, ok := MustSemaTypeOfValue(containingResourceComposite, interpreter).(sema.EntitlementSupportingType)
if !ok {
panic(errors.NewUnreachableError())
}
Expand Down Expand Up @@ -1441,7 +1441,7 @@ func (declarationInterpreter *Interpreter) declareNonEnumCompositeValue(
var self Value = value
if declaration.Kind() == common.CompositeKindAttachment {

attachmentType := interpreter.MustSemaTypeOfValue(value).(*sema.CompositeType)
attachmentType := MustSemaTypeOfValue(value, interpreter).(*sema.CompositeType)
// Self's type in the constructor is fully entitled, since
// the constructor can only be called when in possession of the base resource

Expand Down Expand Up @@ -1886,7 +1886,7 @@ func (interpreter *Interpreter) transferAndConvert(
if targetType != nil &&
!interpreter.IsSubTypeOfSemaType(resultStaticType, targetType) {

resultSemaType := interpreter.MustConvertStaticToSemaType(resultStaticType)
resultSemaType := MustConvertStaticToSemaType(resultStaticType, interpreter)

panic(ValueTransferTypeError{
ExpectedType: targetType,
Expand Down Expand Up @@ -2144,7 +2144,7 @@ func (interpreter *Interpreter) convert(value Value, valueType, targetType sema.
return value
}

targetElementType := interpreter.MustConvertStaticToSemaType(arrayStaticType.ElementType())
targetElementType := MustConvertStaticToSemaType(arrayStaticType.ElementType(), interpreter)

array := arrayValue.array

Expand All @@ -2168,7 +2168,7 @@ func (interpreter *Interpreter) convert(value Value, valueType, targetType sema.
}

value := MustConvertStoredValue(interpreter, element)
valueType := interpreter.MustConvertStaticToSemaType(value.StaticType(interpreter))
valueType := MustConvertStaticToSemaType(value.StaticType(interpreter), interpreter)
return interpreter.convert(value, valueType, targetElementType, locationRange)
},
)
Expand All @@ -2184,8 +2184,8 @@ func (interpreter *Interpreter) convert(value Value, valueType, targetType sema.
return value
}

targetKeyType := interpreter.MustConvertStaticToSemaType(dictStaticType.KeyType)
targetValueType := interpreter.MustConvertStaticToSemaType(dictStaticType.ValueType)
targetKeyType := MustConvertStaticToSemaType(dictStaticType.KeyType, interpreter)
targetValueType := MustConvertStaticToSemaType(dictStaticType.ValueType, interpreter)

dictionary := dictValue.dictionary

Expand Down Expand Up @@ -2214,8 +2214,8 @@ func (interpreter *Interpreter) convert(value Value, valueType, targetType sema.
key := MustConvertStoredValue(interpreter, k)
value := MustConvertStoredValue(interpreter, v)

keyType := interpreter.MustConvertStaticToSemaType(key.StaticType(interpreter))
valueType := interpreter.MustConvertStaticToSemaType(value.StaticType(interpreter))
keyType := MustConvertStaticToSemaType(key.StaticType(interpreter), interpreter)
valueType := MustConvertStaticToSemaType(value.StaticType(interpreter), interpreter)

convertedKey := interpreter.convert(key, keyType, targetKeyType, locationRange)
convertedValue := interpreter.convert(value, valueType, targetValueType, locationRange)
Expand Down Expand Up @@ -3522,6 +3522,8 @@ func init() {
}

func dictionaryTypeFunction(invocation Invocation) Value {
inter := invocation.Interpreter

keyTypeValue, ok := invocation.Arguments[0].(TypeValue)
if !ok {
panic(errors.NewUnreachableError())
Expand All @@ -3538,18 +3540,18 @@ func dictionaryTypeFunction(invocation Invocation) Value {
// if the given key is not a valid dictionary key, it wouldn't make sense to create this type
if keyType == nil ||
!sema.IsSubType(
invocation.Interpreter.MustConvertStaticToSemaType(keyType),
MustConvertStaticToSemaType(keyType, inter),
sema.HashableStructType,
) {
return Nil
}

return NewSomeValueNonCopying(
invocation.Interpreter,
inter,
NewTypeValue(
invocation.Interpreter,
inter,
NewDictionaryStaticType(
invocation.Interpreter,
inter,
keyType,
valueType,
),
Expand Down Expand Up @@ -3656,7 +3658,7 @@ func functionTypeFunction(invocation Invocation) Value {
panic(errors.NewUnreachableError())
}

returnType := interpreter.MustConvertStaticToSemaType(typeValue.Type)
returnType := MustConvertStaticToSemaType(typeValue.Type, interpreter)

var parameterTypes []sema.Parameter
parameterCount := parameters.Count()
Expand All @@ -3665,7 +3667,7 @@ func functionTypeFunction(invocation Invocation) Value {
parameters.Iterate(
interpreter,
func(param Value) bool {
semaType := interpreter.MustConvertStaticToSemaType(param.(TypeValue).Type)
semaType := MustConvertStaticToSemaType(param.(TypeValue).Type, interpreter)
parameterTypes = append(
parameterTypes,
sema.Parameter{
Expand Down Expand Up @@ -3959,7 +3961,7 @@ var runtimeTypeConstructors = []runtimeTypeConstructor{

ty := typeValue.Type
// InclusiveRanges must hold integers
elemSemaTy := inter.MustConvertStaticToSemaType(ty)
elemSemaTy := MustConvertStaticToSemaType(ty, inter)
if !sema.IsSameTypeKind(elemSemaTy, sema.IntegerType) {
return Nil
}
Expand Down Expand Up @@ -4030,7 +4032,7 @@ func (interpreter *Interpreter) IsSubType(subType StaticType, superType StaticTy
return true
}

semaType := interpreter.MustConvertStaticToSemaType(superType)
semaType := MustConvertStaticToSemaType(superType, interpreter)

return interpreter.IsSubTypeOfSemaType(subType, semaType)
}
Expand All @@ -4057,7 +4059,7 @@ func (interpreter *Interpreter) IsSubTypeOfSemaType(staticSubType StaticType, su
return superType == sema.AnyStructType
}

semaSubType := interpreter.MustConvertStaticToSemaType(staticSubType)
semaSubType := MustConvertStaticToSemaType(staticSubType, interpreter)

return sema.IsSubType(semaSubType, superType)
}
Expand Down Expand Up @@ -4466,7 +4468,7 @@ func (interpreter *Interpreter) authAccountReadFunction(
valueStaticType := value.StaticType(interpreter)

if !interpreter.IsSubTypeOfSemaType(valueStaticType, ty) {
valueSemaType := interpreter.MustConvertStaticToSemaType(valueStaticType)
valueSemaType := MustConvertStaticToSemaType(valueStaticType, interpreter)

panic(ForceCastTypeMismatchError{
ExpectedType: ty,
Expand Down Expand Up @@ -4688,18 +4690,6 @@ func (interpreter *Interpreter) ConvertStaticToSemaType(staticType StaticType) (
)
}

func (interpreter *Interpreter) MustSemaTypeOfValue(value Value) sema.Type {
return interpreter.MustConvertStaticToSemaType(value.StaticType(interpreter))
}

func (interpreter *Interpreter) MustConvertStaticToSemaType(staticType StaticType) sema.Type {
semaType, err := interpreter.ConvertStaticToSemaType(staticType)
if err != nil {
panic(err)
}
return semaType
}

func (interpreter *Interpreter) MustConvertStaticAuthorizationToSemaAccess(auth Authorization) sema.Access {
access, err := ConvertStaticAuthorizationToSemaAccess(auth, interpreter)
if err != nil {
Expand Down Expand Up @@ -5157,7 +5147,7 @@ func (interpreter *Interpreter) ExpectType(
valueStaticType := value.StaticType(interpreter)

if !interpreter.IsSubTypeOfSemaType(valueStaticType, expectedType) {
valueSemaType := interpreter.MustConvertStaticToSemaType(valueStaticType)
valueSemaType := MustConvertStaticToSemaType(valueStaticType, interpreter)

panic(TypeMismatchError{
ExpectedType: expectedType,
Expand All @@ -5174,8 +5164,8 @@ func (interpreter *Interpreter) checkContainerMutation(
) {
if !interpreter.IsSubType(element.StaticType(interpreter), elementType) {
panic(ContainerMutationError{
ExpectedType: interpreter.MustConvertStaticToSemaType(elementType),
ActualType: interpreter.MustSemaTypeOfValue(element),
ExpectedType: MustConvertStaticToSemaType(elementType, interpreter),
ActualType: MustSemaTypeOfValue(element, interpreter),
LocationRange: locationRange,
})
}
Expand Down
Loading
Loading