Add API to match function parameters to arguments at call sites#3323
Draft
Steffeeen wants to merge 2 commits into
Draft
Add API to match function parameters to arguments at call sites#3323Steffeeen wants to merge 2 commits into
Steffeeen wants to merge 2 commits into
Conversation
This new API allows matching the function arguments passed at the call site to their corresponding parameters in the function declaration. This functionality is needed to implement different refactorings. For example function inlining and deleting a parameter of a function.
745cf11 to
bfa3cd2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #2047
API Design
The API currently looks like this:
I'm looking for feedback on the API. I'm especially interested in feedback on the following points:
The API currently expects the user to provide the correct function declaration that matches the call. Otherwise, the matching may fail. However, this is not explicitly checked. It would be better to fail fast in case the function declaration does not match, but I'm not sure that's possible without attempting to match. We could accept the
FunctionDeclSyntaxinstead of theFunctionParameterListSyntaxand verify that the name matches the name of the call, but that does not guarantee that the matching will succeed.The matching only works correctly if the call is actually valid. However, we can only check that the call and declaration are syntactically valid but not if they are semantically valid. The API again expects the user to only provide valid combinations of declarations and calls.
I don't like that the
argumentsarray inParameterMatchhas the type[any SyntaxProtocol]. The main problem with making this nicer is that the arguments to variadic closure parameters may be split across the "normal" arguments and the trailing closure. Example:The
matchForParameter(named)method expects the name to be the parameter name rather than the argument label. This is only communicated in the documentation of the method.I'm also not sure if the API should be located in SwiftRefactor as the original issue explicitly mentions using it in macros.
Validating correctness
I added extensive unit tests to validate that the matching works correctly. However, the main tool used for verifying the correctness is
ArgumentMatchingExhaustiveTests.swift. It generates an exhaustive list of function declarations together with function calls. It generates different variations based on the number of parameters, whether a parameter is labeled/unlabeled, whether it is variadic, whether it has a default value, etc. An example for a generated function declaration with its calls:These are then written to a file and typechecked by calling
swiftc. The results of typechecking them are used to filter out calls that are semantically invalid. After this step only valid calls remain for each generated declaration. These are again written to a file together with a body for the declaration to automatically evaluate the matching of the arguments. The generated body for declaration from the example above looks like this:The generated file is compiled and run. The argument matching can then be extracted from the output of running the compiled executable. This creates a baseline for the correct behavior. For each generated declaration the results of the baseline are then compared with the results of using the argument matching logic. In total these tests validated that 9294 declarations with a total of 160066 calls were matched correctly by the argument matching implementation.
I used these tests extensively to improve the implementation. Most of the test cases in the argument matching unit tests were also originally found by these generated test cases. Ideally, we could include these generated test cases into the CI, but I think that would slow down the CI too much. I have already optimized them to split everything into multiple files and typecheck/compile them in parallel depending on the number of available cores. I have also adjusted the generated method bodies to use more explicit type annotations to reduce the time spent on typechecking them. However, running the exhaustive tests still takes around 8 min on my M4 Pro. I don't know what kind of runners the CI runs on, but I imagine that it would take significantly longer to run them there. I have included the code in a separate commit for now, but I'm not sure if it should be included in the final merge into main. The code is also not really cleaned up for now.
RFC
I assume this would be considered part of the public API and thus needs an RFC post. I can create one, but it probably makes sense to decide on the final API first.