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

Allow creating references to nested optionals #3132

Merged
merged 5 commits into from
Mar 12, 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
100 changes: 50 additions & 50 deletions runtime/interpreter/interpreter_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,24 +1349,14 @@

result := interpreter.evalExpression(referenceExpression.Expression)

makeReference := func(value Value, typ *sema.ReferenceType) *EphemeralReferenceValue {
// if we are currently interpretering a function that was declared with mapped entitlement access, any appearances
// of that mapped access in the body of the function should be replaced with the computed output of the map
auth := interpreter.getEffectiveAuthorization(typ)

locationRange := LocationRange{
Location: interpreter.Location,
HasPosition: referenceExpression,
}
return interpreter.createReference(borrowType, result, referenceExpression)
}

return NewEphemeralReferenceValue(
interpreter,
auth,
value,
typ.Type,
locationRange,
)
}
func (interpreter *Interpreter) createReference(
SupunS marked this conversation as resolved.
Show resolved Hide resolved
borrowType sema.Type,
value Value,
hasPosition ast.HasPosition,
) Value {

// There are four potential cases:
// 1) Target type is optional, actual value is also optional (nil/SomeValue)
Expand All @@ -1376,71 +1366,81 @@

switch typ := borrowType.(type) {
case *sema.OptionalType:
innerBorrowType, ok := typ.Type.(*sema.ReferenceType)
// we enforce this in the checker
if !ok {
panic(errors.NewUnreachableError())
}

switch result := result.(type) {
innerType := typ.Type

switch value := value.(type) {
case *SomeValue:
// Case (1):
// References to optionals are transformed into optional references,
// so move the *SomeValue out to the reference itself

locationRange := LocationRange{
Location: interpreter.Location,
HasPosition: referenceExpression.Expression,
HasPosition: hasPosition,
}

innerValue := result.InnerValue(interpreter, locationRange)
innerValue := value.InnerValue(interpreter, locationRange)

return NewSomeValueNonCopying(
interpreter,
makeReference(innerValue, innerBorrowType),
)
referenceValue := interpreter.createReference(innerType, innerValue, hasPosition)

// Wrap the reference with an optional (since an optional is expected).
return NewSomeValueNonCopying(interpreter, referenceValue)

case NilValue:
return Nil

default:
// Case (2):
// If the referenced value is non-optional,
// but the target type is optional,
// then box the reference properly
// but the target type is optional.
referenceValue := interpreter.createReference(innerType, value, hasPosition)

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

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/interpreter_expression.go#L1396-L1397

Added lines #L1396 - L1397 were not covered by tests

locationRange := LocationRange{
Location: interpreter.Location,
HasPosition: referenceExpression,
}

return interpreter.BoxOptional(
locationRange,
makeReference(result, innerBorrowType),
borrowType,
)
// Wrap the reference with an optional (since an optional is expected).
return NewSomeValueNonCopying(interpreter, referenceValue)

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

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/interpreter_expression.go#L1399-L1400

Added lines #L1399 - L1400 were not covered by tests
}

case *sema.ReferenceType:
// Case (3): target type is non-optional, actual value is optional.
// This path shouldn't be reachable. This is only a defensive step
// to ensure references are properly created/tracked.
if someValue, ok := result.(*SomeValue); ok {
if someValue, ok := value.(*SomeValue); ok {
locationRange := LocationRange{
Location: interpreter.Location,
HasPosition: referenceExpression.Expression,
HasPosition: hasPosition,
}
innerValue := someValue.InnerValue(interpreter, locationRange)

auth := interpreter.getEffectiveAuthorization(typ)
return NewEphemeralReferenceValue(interpreter, auth, innerValue, typ.Type, locationRange)
return interpreter.createReference(typ, innerValue, hasPosition)
}

// Case (4): target type is non-optional, actual value is also non-optional
return makeReference(result, typ)
// Case (4): target type is non-optional, actual value is also non-optional.
return interpreter.newEphemeralReference(value, typ, hasPosition)

default:
panic(errors.NewUnreachableError())

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

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/interpreter_expression.go#L1418-L1419

Added lines #L1418 - L1419 were not covered by tests
}
}

func (interpreter *Interpreter) newEphemeralReference(
value Value,
typ *sema.ReferenceType,
hasPosition ast.HasPosition,
) *EphemeralReferenceValue {
// If we are currently interpreting a function that was declared with mapped entitlement access, any appearances
// of that mapped access in the body of the function should be replaced with the computed output of the map
auth := interpreter.getEffectiveAuthorization(typ)

locationRange := LocationRange{
Location: interpreter.Location,
HasPosition: hasPosition,
}

panic(errors.NewUnreachableError())
return NewEphemeralReferenceValue(
interpreter,
auth,
value,
typ.Type,
locationRange,
)
}

func (interpreter *Interpreter) VisitForceExpression(expression *ast.ForceExpression) Value {
Expand Down
141 changes: 98 additions & 43 deletions runtime/sema/check_reference_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,20 @@ func (checker *Checker) VisitReferenceExpression(referenceExpression *ast.Refere
}

// Check the result type and ensure it is a reference type
var isOpt bool
var referenceType *ReferenceType
var expectedLeftType, returnType Type

if !resultType.IsInvalidType() {
var ok bool
// Reference expressions may reference a value which has an optional type.
// For example, the result of indexing into a dictionary is an optional:
//
// let ints: {Int: String} = {0: "zero"}
// let ref: &T? = &ints[0] as &T? // read as (&T)?
//
// In this case the reference expression's type is an optional type.
// Unwrap it one level to get the actual reference type
var optType *OptionalType
optType, isOpt = resultType.(*OptionalType)
if isOpt {
resultType = optType.Type
}

referenceType, ok = resultType.(*ReferenceType)
if !ok {
checker.report(
&NonReferenceTypeReferenceError{
ActualType: resultType,
Range: ast.NewRangeFromPositioned(checker.memoryGauge, referenceExpression),
},
)
} else {
expectedLeftType = referenceType.Type
returnType = referenceType
if isOpt {
expectedLeftType = &OptionalType{Type: expectedLeftType}
returnType = &OptionalType{Type: returnType}
}
}
// Unwrap it (recursively) to get the actual reference type
expectedLeftType, returnType, referenceType =
checker.expectedTypeForReferencedExpr(resultType, referenceExpression)
}

// Type-check the referenced expression
Expand Down Expand Up @@ -100,24 +78,12 @@ func (checker *Checker) VisitReferenceExpression(referenceExpression *ast.Refere

hasErrors := len(checker.errors) > beforeErrors
if !hasErrors {
// If the reference type was an optional type,
// we proposed an optional type to the referenced expression.
//
// Check that it actually has an optional type

// If the reference type was a non-optional type,
// check that the referenced expression does not have an optional type

// Do not report an error if the `expectedLeftType` is unknown

if _, ok := actualType.(*OptionalType); ok != isOpt && expectedLeftType != nil {
checker.report(&TypeMismatchError{
ExpectedType: expectedLeftType,
ActualType: actualType,
Expression: referencedExpression,
Range: checker.expressionRange(referenceExpression),
})
}
checker.checkOptionalityMatch(
expectedLeftType,
actualType,
referencedExpression,
referenceExpression,
)
}

if referenceType == nil {
Expand All @@ -130,3 +96,92 @@ func (checker *Checker) VisitReferenceExpression(referenceExpression *ast.Refere

return returnType
}

func (checker *Checker) expectedTypeForReferencedExpr(
expectedType Type,
hasPosition ast.HasPosition,
) (expectedLeftType, returnType Type, referenceType *ReferenceType) {
// Reference expressions may reference a value which has an optional type.
// For example, the result of indexing into a dictionary is an optional:
//
// let ints: {Int: String} = {0: "zero"}
// let ref: &T? = &ints[0] as &T? // read as (&T)?
//
// In this case the reference expression's type is an optional type.
// Unwrap it to get the actual reference type

switch expectedType := expectedType.(type) {
case *OptionalType:
expectedLeftType, returnType, referenceType =
checker.expectedTypeForReferencedExpr(expectedType.Type, hasPosition)

// Re-wrap with an optional
expectedLeftType = &OptionalType{Type: expectedLeftType}
returnType = &OptionalType{Type: returnType}

case *ReferenceType:
referencedType := expectedType.Type
if referencedOptionalType, referenceToOptional := referencedType.(*OptionalType); referenceToOptional {
checker.report(
&ReferenceToAnOptionalError{
ReferencedOptionalType: referencedOptionalType,
Range: ast.NewRangeFromPositioned(checker.memoryGauge, hasPosition),
},
)
}

return expectedType.Type, expectedType, expectedType

default:
checker.report(
&NonReferenceTypeReferenceError{
ActualType: expectedType,
Range: ast.NewRangeFromPositioned(checker.memoryGauge, hasPosition),
},
)
}

return
}

func (checker *Checker) checkOptionalityMatch(
expectedType, actualType Type,
referencedExpression ast.Expression,
referenceExpression ast.Expression,
) {

// Do not report an error if the `expectedType` is unknown
if expectedType == nil {
return
}

// If the reference type was an optional type,
// we proposed an optional type to the referenced expression.
//
// Check that it actually has an optional type

// If the reference type was a non-optional type,
// check that the referenced expression does not have an optional type

expectedOptional, expectedIsOptional := expectedType.(*OptionalType)
actualOptional, actualIsOptional := actualType.(*OptionalType)

if expectedIsOptional && actualIsOptional {
checker.checkOptionalityMatch(
expectedOptional.Type,
actualOptional.Type,
referencedExpression,
referenceExpression,
)
return
}

if expectedIsOptional != actualIsOptional {
checker.report(&TypeMismatchError{
ExpectedType: expectedType,
ActualType: actualType,
Expression: referencedExpression,
Range: checker.expressionRange(referenceExpression),
})
}
}
36 changes: 36 additions & 0 deletions runtime/sema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2795,6 +2795,42 @@ func (e *NonReferenceTypeReferenceError) SecondaryError() string {
)
}

// ReferenceToAnOptionalError

type ReferenceToAnOptionalError struct {
ReferencedOptionalType *OptionalType
ast.Range
}

var _ SemanticError = &ReferenceToAnOptionalError{}
var _ errors.UserError = &ReferenceToAnOptionalError{}
var _ errors.SecondaryError = &ReferenceToAnOptionalError{}

func (*ReferenceToAnOptionalError) isSemanticError() {}

func (*ReferenceToAnOptionalError) IsUserError() {}

func (e *ReferenceToAnOptionalError) Error() string {
return "cannot create reference"
}

func (e *ReferenceToAnOptionalError) SecondaryError() string {
return fmt.Sprintf(
"expected non-optional type, got `%s`. Consider taking a reference with type `%s`",
e.ReferencedOptionalType.QualifiedString(),

// Suggest taking the optional out of the reference type.
NewOptionalType(
nil,
NewReferenceType(
nil,
UnauthorizedAccess,
e.ReferencedOptionalType.Type,
),
),
)
}

// InvalidResourceCreationError

type InvalidResourceCreationError struct {
Expand Down
Loading
Loading