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
feat: implement tuple return type to tuple struct assist #15696
feat: implement tuple return type to tuple struct assist #15696
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
.ancestors() | ||
.nth(5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment about the magic 5 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah sorry, this is mostly a relic, I was basically trying to handle this case where we don't want to replace the outer tuple with the struct pattern. So, I was trying to get the direct parent of the call or method call expression but I've pushed a cleaner version now.
let ((bar1, bar2), foo) = (bar(), 3);
Also, in this case it would be nice to replace the corresponding pattern but I'm not sure if/how we should do that? Like:
let (BarResult(bar1, bar2), foo) = (bar(), 3);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ye that's tricky, we can only do heuristics here I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah maybe we can leave it for now then, I think figuring out how to handle generics is more important.
@bors r+ |
☀️ Test successful - checks-actions |
This PR implements the
convert_tuple_return_type_to_struct
assist, for converting the return type of a function or method from a tuple to a tuple struct. Additionally, it moves theto_camel_case
andchar_has_case
functions fromcase_conv
tostdx
so that they can be used similar toto_lower_snake_case
.tuple_return_type_to_tuple_struct.webm
Currently, the assist puts the struct definition above the function, or above the nearest
impl
ortrait
if applicable and only rewrites literal tuples that are returned in the body of the function. Additionally, it only attempts to rewrite simple tuple pattern usages with the corresponding tuple struct pattern but does so across files and modules.I think that this is sufficient for the majority of use cases but I could be wrong. One thing I'm still not sure how to approach is handling
Self
and generics/lifetimes in the tuple type to be extracted. I was thinking of either manually figuring out what lifetimes and generics are in scope and using them (sort of similar to thegenerate_function
assist) or maybe usingctx.sema.resolve_type
andgeneric_params
onhir::Type
but this seems to not deal with lifetimes.Closes #14293