Skip to content

[Refactoring] Use known alternative in async refactoring if one exists #38689

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
Aug 3, 2021

Conversation

bnbarham
Copy link
Contributor

Update the async refactorings to use the name from the async alternative
if one is known, rather than always assuming the name matches the
synchronous function.

Note that this could also be used to determine whether results remain
optional or not, but that has been left for a future patch.

Resolves rdar://80612521

@bnbarham bnbarham requested review from ahoppen and hamishknight July 29, 2021 05:11
@bnbarham
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 703411c

@bnbarham
Copy link
Contributor Author

Failing test was disabled in another PR that's now merged

@swift-ci please test macOS platform

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.

LGTM!

Comment on lines +95 to +98
// MULTIPLEHANDLERS: func multipleHandlers() async {
// MULTIPLEHANDLERS-NEXT: let str2 = await multiHandlers2(newArg: "foo", newHandler: { str1 in
// MULTIPLEHANDLERS-NEXT: print(str1)
// MULTIPLEHANDLERS-NEXT: })
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

@bnbarham bnbarham force-pushed the use-known-alternative branch from 703411c to 9f26b23 Compare July 30, 2021 07:29
@bnbarham bnbarham requested a review from etcwilde July 30, 2021 07:29
@bnbarham
Copy link
Contributor Author

@etcwilde I forgot to add tests for renaming to a getter in the attribute tests. 8f4d272 fixes the RenameDeclRequest to grab the underlying accessor if the name is getter: or setter:. Also added a new printUserFacingName to AccessorDecl so the refactoring code can use it too.

@hamishknight there's now some code to handle the accessor and I changed SemaAnnotator to walk the DeclRefExpr inside a double curry thunk rather than just calling passReference - the rename would be skipped otherwise.

@bnbarham
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 9f26b23

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 9f26b23

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.

Accessor and curry thunk changes look good to me!

Comment on lines 7461 to 7464
if (auto *Accessor = dyn_cast<AccessorDecl>(HandlerDesc.Alternative))
Name = Accessor->getStorage()->getBaseIdentifier();
else
Name = HandlerDesc.Alternative->getBaseIdentifier();
Copy link
Contributor

Choose a reason for hiding this comment

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

Just occurred to me that getBaseIdentifier would hit an assertion if this a decl with a special name such as init, and I believe we support marking those async.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

init can definitely be marked async, but it wouldn't make sense as a completion handler function since it inherently returns a value (and thus should never be an alternative).

Regardless, it's probably better to check and fallback anyway - only adds a line or two.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah right yeah, sounds good 👍

@bnbarham bnbarham force-pushed the use-known-alternative branch from 9f26b23 to 0c9651e Compare July 31, 2021 03:10
@bnbarham
Copy link
Contributor Author

Moved the SourceEntityWalker changes to #38712. Tests will fail until that's in, so I won't bother running them yet.

A lookup on the name provided by `renamed` in `@available` returns the
VarDecl. If the name specified a getter or setter, make sure to grab the
corresponding accessor when comparing candidates.

We currently ignore the basename and parameters specified in the name
for accessors. Checking them would only cause a getter/setter not to
match, there can never be a conflict. Since this is a best effort match
anyway, this seems fine.
Update the async refactorings to use the name from the async alternative
if one is known, rather than always assuming the name matches the
synchronous function.

Note that this could also be used to determine whether results remain
optional or not, but that has been left for a future patch.

Resolves rdar://80612521
@bnbarham bnbarham force-pushed the use-known-alternative branch from 0c9651e to f6a8dab Compare August 2, 2021 22:47
@bnbarham
Copy link
Contributor Author

bnbarham commented Aug 2, 2021

@swift-ci please test

@bnbarham bnbarham merged commit 7fb85c3 into main Aug 3, 2021
@bnbarham bnbarham deleted the use-known-alternative branch August 3, 2021 03:25
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