Skip to content
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

[QoI] Handle out of order arguments by reordering them #10672

Merged
merged 1 commit into from Jul 2, 2017
Merged

[QoI] Handle out of order arguments by reordering them #10672

merged 1 commit into from Jul 2, 2017

Conversation

d-ronnqvist
Copy link
Contributor

Change handling of out of order arguments to reorder all arguments according to the parameter order instead of just performing one swap.

I'm not sure if this change is in the spirit of the original outOfOrderArgument method or not and if it's what @dabrahams had in mind for SR-4715 or not.

As I see it, the new diagnostic message is more general - for better and worse - but the fix-it can rearrange multiple arguments at once. In many of the existing test cases where there are only two arguments that need to be swapped this new can be a step back in terms of clarity. At the same time I find that seeing listing all the arguments can provide additional context.

As for the fix-it; in simple cases it's likely unnecessary to modify all of the arguments, but it allows this changed code to deal with many arguments that are completely out of order.

Resolves SR-4715

@jrose-apple jrose-apple requested review from xedin and rudkx June 28, 2017 20:09
Copy link
Member

@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.

Thank you for the patch, @d-ronnqvist!

I think it would be great if we could use the old style errors + fix-its but do it right instead of asking users to drag label through every argument position, which is what is happening right now unfortunately :( It seems to me that errors, in their current form, are more "actionable" and remove some of cognitive load from users comparing to the new error messages, where users would have to parse label string to figure out differences between right and wrong positions, having fix-it which replaces all of the arguments together doesn't help either.

Could you please add couple more test-cases were arguments are more complex than single integer or string literals e.g. function calls and/or closures?

@d-ronnqvist
Copy link
Contributor Author

@xedin How about this? Now it has the same error message as it previously did, but the fix-it moves the out-of-place argument into the correct location in one go. The only "extra" source manipulation is to move a ", " along with the moved argument.

I also added a few more new tests but wasn't entirely sure what to test. Suggestions are appreciated. (test/Constraints/keyword_arguments.swift also already covers some scenarios with default arguments and variadics.)

I rebased my changes. Sorry if the review requests got lost in doing so.

@xedin
Copy link
Member

xedin commented Jun 30, 2017

@d-ronnqvist Awesome! I'll take a closer look soon, just need to get few things done first. Thanks, again!

@xedin
Copy link
Member

xedin commented Jun 30, 2017

@swift-ci please smoke test

continue;
}
unsigned argIdx = 0;
// Enumerate the parameters and their bindings to see if any arguments is our of order
Copy link
Member

Choose a reason for hiding this comment

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

typo -> is/are

// expected-error @-1 {{argument 'b' must precede argument 'c'}} {{26-26=b: { (num: Int) -> String in return String(describing: num) }, }} {{38-101=}}
fun_31849281(a: { !$0 }, c: [nil, 42], b: { "\($0)" })
// expected-error @-1 {{argument 'b' must precede argument 'c'}} {{26-26=b: { "\\($0)" }, }} {{38-54=}}

Copy link
Member

Choose a reason for hiding this comment

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

This is the example I've made which had a broken fix-it before but looks correct now:

func foo(x: Int, y: Int) {
}
foo(42, x: 0)

Copy link
Member

Choose a reason for hiding this comment

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

Actually I'm not sure if its correct because it highlights the whole region, can you please verify?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's correct in highlighting the range that's being removed and the unlabeled argument it needs to precede. Having one argument in between shows that it's not the whole region:

(swift) func bar(x: Int, y: Int, z: Int) {}
(swift) bar(42, y: 10, x: 0)
error: argument 'x' must precede unnamed argument #1
bar(42, y: 10, x: 0)
    ~~       ~~^~~~
    x: 0, 

But when there's only two arguments the two ranges meet and it looks like the whole range. It might look better if it didn't highlight the preceding comma in the range that's being removed. But I don't know if the highlight when using fixItRemove can be changed.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good, please add it to the tests for completeness.

Copy link
Member

@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.

Overall LGTM now, please fix the typo I've mentioned, squash the fix-it commit and format the commit description to cut-off lines at 80 symbols or so and we should be good to go.

@xedin
Copy link
Member

xedin commented Jul 2, 2017

@d-ronnqvist And I forgot to mention - could you also please run clang-format on the changes you've made, we have a tool which integrates it with git too, so you could do git clang-format. It's located in clang/tools/clang-format, binary itself should be in the bin directory of your llvm build which comes with swift.

@d-ronnqvist
Copy link
Contributor Author

@xedin Did you want me to squash all commits or only the modifications to the second commit (this solution)?

@xedin
Copy link
Member

xedin commented Jul 2, 2017

@d-ronnqvist Sorry, please squash all 3 into a single one, I'm going to nominate it for 4.0 as well.

Change the fix-it to move the argument to its correct location in one go. This happens by removing it from one location and inserting it in the other (as opposed to the original implementation which swapped one argument with the preceding one). The commas separating the arguments are adjusted to match the moved argument.

Add new tests for reordering regular arguments, variadic arguments, and function arguments.

Resolves: SR-4715 rdar://problem/31849281
@xedin
Copy link
Member

xedin commented Jul 2, 2017

@swift-ci please smoke test

@xedin xedin merged commit 45bfefb into apple:master Jul 2, 2017
@xedin
Copy link
Member

xedin commented Jul 2, 2017

@d-ronnqvist Thank you!

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.

None yet

2 participants