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

Add support for dereferencing optional references #3008

Merged
merged 4 commits into from
Jan 11, 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
25 changes: 21 additions & 4 deletions runtime/interpreter/interpreter_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,16 +697,33 @@
)

case ast.OperationMul:
referenceValue, ok := value.(ReferenceValue)
if !ok {
panic(errors.NewUnreachableError())

if _, ok := value.(NilValue); ok {
return Nil
}

locationRange := LocationRange{
Location: interpreter.Location,
HasPosition: expression,
}
var isOptional bool

if someValue, ok := value.(*SomeValue); ok {
isOptional = true
value = someValue.InnerValue(interpreter, locationRange)
}

return DereferenceValue(interpreter, locationRange, referenceValue)
referenceValue, ok := value.(ReferenceValue)
if !ok {
panic(errors.NewUnreachableError())

Check warning on line 718 in runtime/interpreter/interpreter_expression.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/interpreter_expression.go#L718

Added line #L718 was not covered by tests
}

dereferencedValue := DereferenceValue(interpreter, locationRange, referenceValue)
if isOptional {
return NewSomeValueNonCopying(interpreter, dereferencedValue)
} else {
return dereferencedValue
}

case ast.OperationMove:
interpreter.invalidateResource(value)
Expand Down
4 changes: 2 additions & 2 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -19432,8 +19432,8 @@ type SomeValue struct {
isDestroyed bool
}

func NewSomeValueNonCopying(interpreter *Interpreter, value Value) *SomeValue {
common.UseMemory(interpreter, common.OptionalValueMemoryUsage)
func NewSomeValueNonCopying(memoryGauge common.MemoryGauge, value Value) *SomeValue {
common.UseMemory(memoryGauge, common.OptionalValueMemoryUsage)

return NewUnmeteredSomeValueNonCopying(value)
}
Expand Down
15 changes: 14 additions & 1 deletion runtime/sema/check_unary_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (checker *Checker) VisitUnaryExpression(expression *ast.UnaryExpression) Ty
return checkExpectedType(valueType, SignedNumberType)

case ast.OperationMul:

var isOptional bool
if optionalType, ok := valueType.(*OptionalType); ok {
isOptional = true
valueType = optionalType.Type
}

referenceType, ok := valueType.(*ReferenceType)
if !ok {
if !valueType.IsInvalidType() {
Expand Down Expand Up @@ -97,7 +104,13 @@ func (checker *Checker) VisitUnaryExpression(expression *ast.UnaryExpression) Ty
)
}

return innerType
if isOptional {
return &OptionalType{
Type: innerType,
}
} else {
return innerType
}

case ast.OperationMove:
if !valueType.IsInvalidType() &&
Expand Down
Loading
Loading