Skip to content

[CodeCompletion] Migrate completions after return and yield to solver-based #68079

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 3 commits into from
Aug 30, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Aug 23, 2023

This was pretty straightforward. Completions after return needed special handling for return nil in failable constructors because return nil is represented as a FailStmt in the type checker and thus we didn’t get completions for nil here by default.

We didn’t have any tests for completion after yield. I added a couple. The results aren’t amazing (I didn’t look into whether we could include some results or add more type relations) but probably better than before and since this is an experimental language feature, I think it’s not too important.

@ahoppen
Copy link
Member Author

ahoppen commented Aug 23, 2023

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Aug 23, 2023

@swift-ci Please SourceKit stress test

Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

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

Nice!

@ahoppen ahoppen force-pushed the ahoppen/return-solver-based branch from e15688a to 343fcc0 Compare August 23, 2023 15:26
@ahoppen
Copy link
Member Author

ahoppen commented Aug 23, 2023

Note to self: The stress tester passed

We didn’t actually have any tests for this. Completions aren’t great here at the moment but since this is an underscored language feature it’s not that important at the moment.
@ahoppen ahoppen force-pushed the ahoppen/return-solver-based branch from 343fcc0 to ea9582e Compare August 23, 2023 20:38
// `return nil` is not actually represented as a `ReturnExpr` in the AST but
// gets translated to a `FailStmt`. We thus can't produce the 'nil' completion
// using the solver-based implementation. Add the result manually.
if (auto ctor = dyn_cast_or_null<ConstructorDecl>(DC->getAsDecl())) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't really understand this statement. Since we are completing nil, the I think AST doesn't have it yet. So the AST should be like (return_stmt (code_completion_expr)). As far as I can see from the code, only return nil is converted to FailExpr but non-nil isn't.
As long as CompletionLookup::addValueLiteralCompletions() is called, nil seems to be added unconditionally. Could you explain why it's not the case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, we don’t even get to type checking (and thus sawSolutionImpl, which would trigger CompletionLookup) because PreCheckReturnStmtRequest fails. I thought about changing PreCheckStmtRequest to allow return #^COMPLETE^# but I thought that allowing return statements inside initializers might cause issues further down the line if initializer type checking assumes that all returns have been translated to FailStmt.

https://github.com/apple/swift/blob/3687ddfac65a16c0cec61d7ab54a2c7c1760c330/lib/Sema/TypeCheckStmt.cpp#L1709-L1715

@ahoppen ahoppen force-pushed the ahoppen/return-solver-based branch from ea9582e to 4702f82 Compare August 24, 2023 15:48
@ahoppen
Copy link
Member Author

ahoppen commented Aug 28, 2023

@swift-ci Please smoke test

@ahoppen ahoppen merged commit 2b6b3fd into swiftlang:main Aug 30, 2023
@ahoppen ahoppen deleted the ahoppen/return-solver-based branch August 30, 2023 17: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.

4 participants