Skip to content

[SE-309] CSDiag: Add a fix-it that replaces an existential parameter type with its generic counterpart #41198

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 4 commits into from
Feb 23, 2022

Conversation

AnthonyLatsis
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis commented Feb 4, 2022

If the base of an unsupported existential member access is a reference to a function/subscript parameter, offer a fix-it that replaces the existential parameter type with its generic counterpart, e.g. func foo(p: any P) → func foo<T>(p: T).

@xedin xedin self-requested a review February 4, 2022 05:45
@AnthonyLatsis AnthonyLatsis changed the title [SE-309] CSDiag: Add a fix-it that turns a parameter of existential type into one of generic type [SE-309] CSDiag: Add a fix-it that replaces an existential parameter type with its generic equivalent Feb 4, 2022
if (isa<InOutTypeRepr>(PD->getTypeRepr()))
return true;

constexpr StringRef GPNamePlaceholder = "<#generic parameter#>";
Copy link
Collaborator Author

@AnthonyLatsis AnthonyLatsis Feb 4, 2022

Choose a reason for hiding this comment

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

Unfortunately, placeholders cannot be correlated and compile to errors rather than wildcards, which may take inexperienced users by surprise. We could use T when there is no generic signature, and something neutral otherwise like GenericParameter with some number suffix for disambiguation, but that can still be abused in many ways. Or we could fall back to placeholders, now that a generic signature gives us some degree of confidence that the user is familiar with generics.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine, we shouldn't attempt to name the parameter here and let users do it instead, maybe add name after generic parameter to make it more explicit...

@DougGregor
Copy link
Member

Given that the SE-0341 review is going well-ish, should we inject some instead of the generic parameter?

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented Feb 10, 2022

should we inject some instead of the generic parameter?

We could also provide both options to honor progressive disclosure; a sudden some parameter here is probably going to startle a lot of devs in its maiden voyage.

@AnthonyLatsis
Copy link
Collaborator Author

@xedin ping

@xedin
Copy link
Contributor

xedin commented Feb 22, 2022

@AnthonyLatsis Thanks for the ping! Should be able to take a look at this a bit later today.

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

LGTM! My only concern is that multiple expressions are going to produce these fix-its and that would create chaos if fix-its where auto applied... Could you please, as a follow-up, explore whether it would be possible to adjust DiagnosticEngine to track whether exactly the same fix-it has been already applied to a particular source region before and just drop it?

if (isa<InOutTypeRepr>(PD->getTypeRepr()))
return true;

constexpr StringRef GPNamePlaceholder = "<#generic parameter#>";
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine, we shouldn't attempt to name the parameter here and let users do it instead, maybe add name after generic parameter to make it more explicit...

@AnthonyLatsis
Copy link
Collaborator Author

@xedin Thank you for the feedback! Do you have an opinion on Doug's suggestion to inject some, maybe under certain conditions, or as an additional option? I am not sure how auto-apply works when there are multiple options though..

@xedin
Copy link
Contributor

xedin commented Feb 22, 2022

@AnthonyLatsis I'm not sure, that's why I didn't comment on that. Maybe it makes sense to implement it under the the same flag that SE-0341 is using and make a decision once that flag gets removed...

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test

@AnthonyLatsis AnthonyLatsis changed the title [SE-309] CSDiag: Add a fix-it that replaces an existential parameter type with its generic equivalent [SE-309] CSDiag: Add a fix-it that replaces an existential parameter type with its generic counterpart Feb 23, 2022
@AnthonyLatsis AnthonyLatsis merged commit ddc2065 into swiftlang:main Feb 23, 2022
@AnthonyLatsis AnthonyLatsis deleted the se-309-fixit branch February 23, 2022 09:35
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