Skip to content

[CodeComplete] A couple of contextual type handling tweaks #71211

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

Merged
merged 2 commits into from
Jan 30, 2024

Conversation

hamishknight
Copy link
Contributor

Found while cleaning up the constraint system's handling of ReturnStmts, improve the handling of contextual types in a couple of cases.

If we know we have a Void expected type, don't set
`ExpectsNonVoid` to `true`.
This can occur for e.g multi-statement closure
returns (and soon for singe-expression closures
too), so fall back to evaluating the expression
type if it's not fully resolved.
@hamishknight
Copy link
Contributor Author

@swift-ci please test

return self.#^TestExplicitSingleExprClosureBinding^#
}
}
// FIXME: Because we have an explicit return, and no expected type, we shouldn't suggest Void.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think suggesting values that return Void after return makes sense if the closure has a Void return type. For example return callback() is a common pattern for early returns out of closures and as a shorthand of callback(); return

So, I would just remove the FIXME

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but in this case there's no contextual type. If there was an explicit Void contextual type, we'd allow it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could carve out an exception for CTP_ReturnStmt if we really wanted this behavior though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what I’m trying to say is that I don’t really care about this case because you can argue either way and I would just remove the FIXME. But if you want to keep it, that’s good with me as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to keep it for now since this is going to change anyway with my constraint system patch. I should also note that not suggesting Void is also the behavior we currently have for multi-statement closure returns. I don't really feel too strongly about it either way (and am happy to carve out an exception for returns if we want that), but I think we should at least be consistent there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing in #71272

@hamishknight hamishknight merged commit 562ab8a into swiftlang:main Jan 30, 2024
@hamishknight hamishknight deleted the completion-tweaks branch January 30, 2024 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants