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

Unwrap optionals with dynamic cast #3580

Merged
merged 8 commits into from
Sep 13, 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
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
Loading