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

Cleanup reference tracking #2969

Merged
merged 3 commits into from
Dec 8, 2023
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
4 changes: 1 addition & 3 deletions runtime/convertValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,8 @@ func exportValueWithInterpreter(
defer delete(seenReferences, v)
seenReferences[v] = struct{}{}

referencedValue := v.MustReferencedValue(inter, locationRange)

return exportValueWithInterpreter(
referencedValue,
v.Value,
inter,
locationRange,
seenReferences,
Expand Down
10 changes: 5 additions & 5 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5158,7 +5158,7 @@ func (interpreter *Interpreter) trackReferencedResourceKindedValue(
}

// TODO: Remove the `destroyed` flag
func (interpreter *Interpreter) invalidateReferencedResources(value Value, destroyed bool) {
func (interpreter *Interpreter) invalidateReferencedResources(value Value) {
// skip non-resource typed values
if !value.IsResourceKinded(interpreter) {
return
Expand All @@ -5169,25 +5169,25 @@ func (interpreter *Interpreter) invalidateReferencedResources(value Value, destr
switch value := value.(type) {
case *CompositeValue:
value.ForEachLoadedField(interpreter, func(_ string, fieldValue Value) (resume bool) {
interpreter.invalidateReferencedResources(fieldValue, destroyed)
interpreter.invalidateReferencedResources(fieldValue)
// continue iteration
return true
})
storageID = value.StorageID()
case *DictionaryValue:
value.IterateLoaded(interpreter, func(_, value Value) (resume bool) {
interpreter.invalidateReferencedResources(value, destroyed)
interpreter.invalidateReferencedResources(value)
return true
})
storageID = value.StorageID()
case *ArrayValue:
value.IterateLoaded(interpreter, func(element Value) (resume bool) {
interpreter.invalidateReferencedResources(element, destroyed)
interpreter.invalidateReferencedResources(element)
return true
})
storageID = value.StorageID()
case *SomeValue:
interpreter.invalidateReferencedResources(value.value, destroyed)
interpreter.invalidateReferencedResources(value.value)
return
default:
// skip non-container typed values.
Expand Down
88 changes: 20 additions & 68 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,7 @@

v.isDestroyed = true

interpreter.invalidateReferencedResources(v, true)
interpreter.invalidateReferencedResources(v)

v.array = nil
}
Expand Down Expand Up @@ -2810,7 +2810,7 @@
// This allows raising an error when the resource array is attempted
// to be transferred/moved again (see beginning of this function)

interpreter.invalidateReferencedResources(v, false)
interpreter.invalidateReferencedResources(v)

v.array = nil
}
Expand Down Expand Up @@ -16624,7 +16624,7 @@

v.isDestroyed = true

interpreter.invalidateReferencedResources(v, true)
interpreter.invalidateReferencedResources(v)

v.dictionary = nil
}
Expand Down Expand Up @@ -17410,7 +17410,7 @@
// This allows raising an error when the resource is attempted
// to be transferred/moved again (see beginning of this function)

interpreter.invalidateReferencedResources(v, false)
interpreter.invalidateReferencedResources(v)

v.dictionary = nil
}
Expand Down Expand Up @@ -18256,7 +18256,7 @@

v.isDestroyed = true

interpreter.invalidateReferencedResources(v, true)
interpreter.invalidateReferencedResources(v)

v.dictionary = nil
}
Expand Down Expand Up @@ -19050,7 +19050,7 @@
// This allows raising an error when the resource array is attempted
// to be transferred/moved again (see beginning of this function)

interpreter.invalidateReferencedResources(v, false)
interpreter.invalidateReferencedResources(v)

v.dictionary = nil
}
Expand Down Expand Up @@ -20197,19 +20197,10 @@
}

func (v *EphemeralReferenceValue) StaticType(inter *Interpreter) StaticType {
referencedValue := v.ReferencedValue(inter, EmptyLocationRange, true)
if referencedValue == nil {
panic(DereferenceError{
Cause: "the value being referenced has been destroyed or moved",
})
}

self := *referencedValue

return NewReferenceStaticType(
inter,
v.Authorization,
self.StaticType(inter),
v.Value.StaticType(inter),
)
}

Expand All @@ -20229,39 +20220,20 @@
return &v.Value
}

func (v *EphemeralReferenceValue) MustReferencedValue(
interpreter *Interpreter,
locationRange LocationRange,
) Value {
referencedValue := v.ReferencedValue(interpreter, locationRange, true)
if referencedValue == nil {
panic(DereferenceError{
Cause: "the value being referenced has been destroyed or moved",
LocationRange: locationRange,
})
}

return *referencedValue
}

func (v *EphemeralReferenceValue) GetMember(
interpreter *Interpreter,
locationRange LocationRange,
name string,
) Value {
self := v.MustReferencedValue(interpreter, locationRange)

return interpreter.getMember(self, locationRange, name)
return interpreter.getMember(v.Value, locationRange, name)
}

func (v *EphemeralReferenceValue) RemoveMember(
interpreter *Interpreter,
locationRange LocationRange,
identifier string,
) Value {
self := v.MustReferencedValue(interpreter, locationRange)

if memberAccessibleValue, ok := self.(MemberAccessibleValue); ok {
if memberAccessibleValue, ok := v.Value.(MemberAccessibleValue); ok {

Check warning on line 20236 in runtime/interpreter/value.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/value.go#L20236

Added line #L20236 was not covered by tests
return memberAccessibleValue.RemoveMember(interpreter, locationRange, identifier)
}

Expand All @@ -20274,19 +20246,15 @@
name string,
value Value,
) bool {
self := v.MustReferencedValue(interpreter, locationRange)

return interpreter.setMember(self, locationRange, name, value)
return interpreter.setMember(v.Value, locationRange, name, value)
}

func (v *EphemeralReferenceValue) GetKey(
interpreter *Interpreter,
locationRange LocationRange,
key Value,
) Value {
self := v.MustReferencedValue(interpreter, locationRange)

return self.(ValueIndexableValue).
return v.Value.(ValueIndexableValue).
GetKey(interpreter, locationRange, key)
}

Expand All @@ -20296,9 +20264,7 @@
key Value,
value Value,
) {
self := v.MustReferencedValue(interpreter, locationRange)

self.(ValueIndexableValue).
v.Value.(ValueIndexableValue).
SetKey(interpreter, locationRange, key, value)
}

Expand All @@ -20308,9 +20274,7 @@
key Value,
value Value,
) {
self := v.MustReferencedValue(interpreter, locationRange)

self.(ValueIndexableValue).
v.Value.(ValueIndexableValue).

Check warning on line 20277 in runtime/interpreter/value.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/value.go#L20277

Added line #L20277 was not covered by tests
InsertKey(interpreter, locationRange, key, value)
}

Expand All @@ -20319,9 +20283,7 @@
locationRange LocationRange,
key Value,
) Value {
self := v.MustReferencedValue(interpreter, locationRange)

return self.(ValueIndexableValue).
return v.Value.(ValueIndexableValue).

Check warning on line 20286 in runtime/interpreter/value.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/value.go#L20286

Added line #L20286 was not covered by tests
RemoveKey(interpreter, locationRange, key)
}

Expand All @@ -20330,7 +20292,7 @@
locationRange LocationRange,
key sema.Type,
) Value {
self := v.MustReferencedValue(interpreter, locationRange)
self := v.Value

if selfComposite, isComposite := self.(*CompositeValue); isComposite {
return selfComposite.getTypeKey(
Expand All @@ -20351,9 +20313,7 @@
key sema.Type,
value Value,
) {
self := v.MustReferencedValue(interpreter, locationRange)

self.(TypeIndexableValue).
v.Value.(TypeIndexableValue).

Check warning on line 20316 in runtime/interpreter/value.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/value.go#L20316

Added line #L20316 was not covered by tests
SetTypeKey(interpreter, locationRange, key, value)
}

Expand All @@ -20362,9 +20322,7 @@
locationRange LocationRange,
key sema.Type,
) Value {
self := v.MustReferencedValue(interpreter, locationRange)

return self.(TypeIndexableValue).
return v.Value.(TypeIndexableValue).

Check warning on line 20325 in runtime/interpreter/value.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/value.go#L20325

Added line #L20325 was not covered by tests
RemoveTypeKey(interpreter, locationRange, key)
}

Expand All @@ -20389,14 +20347,9 @@
locationRange LocationRange,
results TypeConformanceResults,
) bool {
referencedValue := v.ReferencedValue(interpreter, locationRange, true)
if referencedValue == nil {
return false
}
self := v.Value

self := *referencedValue

staticType := self.StaticType(interpreter)
staticType := v.Value.StaticType(interpreter)

if !interpreter.IsSubTypeOfSemaType(staticType, v.BorrowedType) {
return false
Expand Down Expand Up @@ -20477,10 +20430,9 @@
function func(value Value) (resume bool),
locationRange LocationRange,
) {
referencedValue := v.MustReferencedValue(interpreter, locationRange)
forEachReference(
interpreter,
referencedValue,
v.Value,
elementType,
function,
locationRange,
Expand Down
Loading