Skip to content

Commit

Permalink
Merge pull request #3580 from RZhang05/unwrap-optionals-with-dynamic-…
Browse files Browse the repository at this point in the history
…cast

Unwrap optionals with dynamic cast
  • Loading branch information
turbolent authored Sep 13, 2024
2 parents 0d6879a + 2945662 commit 0ca61f0
Show file tree
Hide file tree
Showing 2 changed files with 413 additions and 0 deletions.
7 changes: 7 additions & 0 deletions runtime/interpreter/interpreter_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,13 @@ func (interpreter *Interpreter) VisitCastingExpression(expression *ast.CastingEx
// thus this is the only place where it becomes necessary to "instantiate" the result of a map to its
// concrete outputs. In other places (e.g. interface conformance checks) we want to leave maps generic,
// so we don't substitute them.

// if the target is anystruct or anyresource we want to preserve optionals
unboxedExpectedType := sema.UnwrapOptionalType(expectedType)
if !(unboxedExpectedType == sema.AnyStructType || unboxedExpectedType == sema.AnyResourceType) {
// otherwise dynamic cast now always unboxes optionals
value = interpreter.Unbox(locationRange, value)
}
valueSemaType := interpreter.SubstituteMappedEntitlements(interpreter.MustSemaTypeOfValue(value))
valueStaticType := ConvertSemaToStaticType(interpreter, valueSemaType)
isSubType := interpreter.IsSubTypeOfSemaType(valueStaticType, expectedType)
Expand Down
Loading

0 comments on commit 0ca61f0

Please sign in to comment.