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

Rework diagnostics for wrong number of generic args (fixes #66228 and #71924) #77524

Merged
merged 1 commit into from
Jan 13, 2021

Conversation

Patryk27
Copy link
Contributor

@Patryk27 Patryk27 commented Oct 4, 2020

This PR reworks the wrong number of {} arguments message, so that it provides more details and contextual hints.

@rust-highfive
Copy link
Collaborator

r? @matthewjasper

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 4, 2020
@Patryk27 Patryk27 force-pushed the fixes/66228 branch 6 times, most recently from 17ca2ab to ea0a68d Compare October 4, 2020 17:40
@Patryk27
Copy link
Contributor Author

Patryk27 commented Oct 8, 2020

Ping, ping, just remindin' - @matthewjasper :-)

@bors
Copy link
Contributor

bors commented Oct 14, 2020

☔ The latest upstream changes (presumably #77926) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@Patryk27
Copy link
Contributor Author

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@Patryk27
Copy link
Contributor Author

also, re-pinging @matthewjasper :-)

@bors
Copy link
Contributor

bors commented Oct 20, 2020

☔ The latest upstream changes (presumably #78127) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@Patryk27
Copy link
Contributor Author

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@bors
Copy link
Contributor

bors commented Oct 26, 2020

☔ The latest upstream changes (presumably #77187) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@Patryk27
Copy link
Contributor Author

r? @estebank

@Patryk27
Copy link
Contributor Author

Patryk27 commented Nov 8, 2020

ping ping @estebank

@bors
Copy link
Contributor

bors commented Nov 15, 2020

☔ The latest upstream changes (presumably #79070) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@Patryk27
Copy link
Contributor Author

ping ping @estebank, @jonas-schievink? (sorry, I'm not quite sure who to ping or how to proceed from here 😅)

@jonas-schievink
Copy link
Contributor

The triage working group would pick this up if it doesn't get updated in a while, but every time you comment that time gets reset

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

Apologies for the delay in reviewing. I'll finish the review shortly, but in order to avoid keeping you waiting any further giving the length of the PR, I am pushing out some of the things I've found so far.

Comment on lines 4 to 5
LL | trait Trait {}
| ----------- defined here, with 0 type parameters
Copy link
Contributor

Choose a reason for hiding this comment

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

<3

src/test/ui/async-await/issues/issue-65159.stderr Outdated Show resolved Hide resolved
src/test/ui/bad/bad-mid-path-type-params.stderr Outdated Show resolved Hide resolved
format!("consider removing this {} argument", self.kind)
};

err.span_suggestion(span, &msg, String::new(), Applicability::MaybeIncorrect);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if making this

Suggested change
err.span_suggestion(span, &msg, String::new(), Applicability::MaybeIncorrect);
err.span_suggestion_verbose(span, &msg, String::new(), Applicability::MaybeIncorrect);

will look better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like span_suggestion_verbose() prints message with the suggestion already applied, yielding pearls such as:

@@ -19,7 +19,12 @@ LL | fn foo<const X: usize, const Y: usize>() -> usize {
    | ------------------------------------------------- defined here, with 2 const parameters: `X`, `Y`
 ...
 LL |     foo::<0, 0, 0>();
-   |     ^^^       --- help: remove this const argument
+   |     ^^^
+   |
+help: remove this const argument
+   |
+LL |     foo::<0, 0>();
+   |              --

or

@@ -25,7 +30,12 @@ LL | struct S;
    | --------- defined here, with 0 const parameters
 ...
 LL |     S::<0>;
-   |     ^   - help: remove this const argument
+   |     ^
+   |
+help: remove this const argument
+   |
+LL |     S::<>;
+   |        --

IMHO the inline (current) version with s/consider replacing/replace is way more legible.

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
configure: rust.channel         := nightly
configure: rust.debug-assertions := True
configure: llvm.assertions      := True
configure: dist.missing-tools   := True
configure: build.configure-args := ['--enable-sccache', '--disable-manage-submodu ...
configure: writing `config.toml` in current directory
configure: 
configure: run `python /checkout/x.py --help`
configure: 
---
Checking which error codes lack tests...
Found 435 error codes
Found 0 error codes with no tests
Done!
tidy error: /checkout/src/test/ui/async-await/issues/issue-65159.rs:5: line longer than 100 chars
tidy error: /checkout/src/test/ui/traits/trait-test-2.rs: ignoring line length unnecessarily
tidy error: /checkout/src/test/ui/seq-args.rs:4: line longer than 100 chars
tidy error: /checkout/src/test/ui/seq-args.rs:7: line longer than 100 chars
tidy error: /checkout/src/test/ui/const-generics/incorrect-number-of-const-args.rs:11: line longer than 100 chars
tidy error: /checkout/src/test/ui/const-generics/incorrect-number-of-const-args.rs:12: line longer than 100 chars
tidy error: /checkout/src/test/ui/const-generics/invalid-const-arg-for-type-param.rs:6: line longer than 100 chars
tidy error: /checkout/src/test/ui/methods/method-call-lifetime-args-fail.rs:17: line longer than 100 chars
tidy error: /checkout/src/test/ui/methods/method-call-lifetime-args-fail.rs:19: line longer than 100 chars
tidy error: /checkout/src/test/ui/methods/method-call-lifetime-args-fail.rs:64: line longer than 100 chars
tidy error: /checkout/src/test/ui/methods/method-call-lifetime-args-fail.rs:66: line longer than 100 chars
tidy error: /checkout/src/test/ui/issues/issue-18423.rs:4: line longer than 100 chars
tidy error: /checkout/src/test/ui/issues/issue-53251.rs:12: line longer than 100 chars
tidy error: /checkout/src/test/ui/issues/issue-53251.rs:13: line longer than 100 chars
tidy error: /checkout/src/test/ui/structs/structure-constructor-type-mismatch.rs:48: line longer than 100 chars
tidy error: /checkout/src/test/ui/structs/structure-constructor-type-mismatch.rs:54: line longer than 100 chars
some tidy checks failed

command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/tidy" "/checkout" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "/checkout/obj/build"
expected success, got: exit code: 1

@@ -1,8 +1,16 @@
error[E0107]: wrong number of type arguments: expected 0, found 1
error[E0107]: this trait takes 0 type arguments but 1 type argument was supplied
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we would give the error for non-Fn trait using parenthetical notation higher priority and emit it instead of these two, but that should be done in a different PR.

Comment on lines +18 to +21
help: use angle brackets to add missing type argument
|
LL | fn foo(b: Box<Bar<A>()>) {
| ^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

Because of the (incorrect) use of the parenthetical notation here, the suggestion is incorrect. That being said it might be worth it to tackle it at another time.

Comment on lines +9 to +17
note: function defined here, with 2 generic parameters: `T`, `P`
--> $DIR/issue-76595.rs:10:4
|
LL | fn test<T, const P: usize>() where Bool<{core::mem::size_of::<T>() > 4}>: True {
| ^^^^ - -
help: add missing generic argument
|
LL | test::<2, P>();
| ^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

I see here that we don't try to match and differentiate between type, lifetime and const params, but that is fine for this PR, I'd like to land it sooner rather than later. We'll have to take a look at refining that later.

Copy link
Contributor Author

@Patryk27 Patryk27 Jan 10, 2021

Choose a reason for hiding this comment

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

Actually, the first version of this message did distinguish between type, lifetime and const parameters; it was changed some time ago on the upstream:

... and I just went with it.

Comment on lines +14 to +17
help: add missing lifetime argument
|
LL | S::<'static, 'b>(&0, &0);
| ^^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

For lifetimes we would ideally look for available lifetimes (like we do in other cases) and if none use 'static, but again, that should be follow up work. (I'm just marking TODO notes for later right now.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Parallel idea: for lifetime & type generics in expression position (e.g. .collect::<Vec>()) we could suggest using placeholders (i.e. suggest Vec<_> instead of Vec<T>).

Comment on lines +48 to +74
error[E0107]: this struct takes 0 lifetime arguments but 1 lifetime argument was supplied
--> $DIR/wrong-number-of-args.rs:10:14
|
LL | type C = Ty<'static, usize>;
| ^^ --------- help: remove this lifetime argument
| |
| expected 0 lifetime arguments
|
note: struct defined here, with 0 lifetime parameters
--> $DIR/wrong-number-of-args.rs:2:12
|
LL | struct Ty;
| ^^

error[E0107]: this struct takes 0 type arguments but 1 type argument was supplied
--> $DIR/wrong-number-of-args.rs:10:14
|
LL | type C = Ty<'static, usize>;
| ^^ ------- help: remove this type argument
| |
| expected 0 type arguments
|
note: struct defined here, with 0 type parameters
--> $DIR/wrong-number-of-args.rs:2:12
|
LL | struct Ty;
| ^^
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we would tackle these two with the same error, suggesting to remove the entirety of the arguments at once, but again, this can be done later, it is not a regression.

Comment on lines +57 to +83
error[E0107]: this trait takes 0 lifetime arguments but 1 lifetime argument was supplied
--> $DIR/typeck-builtin-bound-type-parameters.rs:13:15
|
LL | fn foo2<'a, T:Copy<'a, U>, U>(x: T) {}
| ^^ unexpected lifetime argument
| ^^^^ ---- help: remove this lifetime argument
| |
| expected 0 lifetime arguments
|
note: trait defined here, with 0 lifetime parameters
--> $SRC_DIR/core/src/marker.rs:LL:COL
|
LL | pub trait Copy: Clone {
| ^^^^

error[E0107]: wrong number of type arguments: expected 0, found 1
--> $DIR/typeck-builtin-bound-type-parameters.rs:14:24
error[E0107]: this trait takes 0 type arguments but 1 type argument was supplied
--> $DIR/typeck-builtin-bound-type-parameters.rs:13:15
|
LL | fn foo2<'a, T:Copy<'a, U>, U>(x: T) {}
| ^ unexpected type argument
| ^^^^ --- help: remove this type argument
| |
| expected 0 type arguments
|
note: trait defined here, with 0 type parameters
--> $SRC_DIR/core/src/marker.rs:LL:COL
|
LL | pub trait Copy: Clone {
| ^^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

This case is somewhat unfortunate (because we would need to look at the whole thing and suggest removing both lifetimes and type params in one go, but we don't need to tackle it now.

Copy link
Contributor Author

@Patryk27 Patryk27 Jan 13, 2021

Choose a reason for hiding this comment

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

I was thinking about a dedicated message for when generics were provided where none were expected, something in the lines of:

error[E0107]: this trait is not generic
  --> $DIR/typeck-builtin-bound-type-parameters.rs:13:15
   |
LL | fn foo2<'a, T:Copy<'a, U>, U>(x: T) {}
   |               ^^^^------- help: remove these generics
   |
note: trait defined here, with no generics
  --> $SRC_DIR/core/src/marker.rs:LL:COL
   |
LL | pub trait Copy: Clone {
   |           ^^^^

I'll try tackling it in another PR, after this one gets merged :-)

Comment on lines -8 to -13
|
note: tuple variant defined here
--> $DIR/enum-variant-priority-higher-than-other-inherent.rs:5:5
|
LL | V(u8)
| ^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

Somewhat surprised by this change 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's a bit unfortunate side effect of looking up identifiers via a new query - def_ident_span - that I haven't been able to re-implement for tuple variants. Technically reviving this one should be as easy as improving that query.

@estebank
Copy link
Contributor

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Jan 13, 2021

📌 Commit d2f8e39 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 13, 2021
@bors
Copy link
Contributor

bors commented Jan 13, 2021

⌛ Testing commit d2f8e39 with merge a62a760...

@bors
Copy link
Contributor

bors commented Jan 13, 2021

☀️ Test successful - checks-actions
Approved by: estebank
Pushing a62a760 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 13, 2021
@bors bors merged commit a62a760 into rust-lang:master Jan 13, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 13, 2021
@Patryk27 Patryk27 deleted the fixes/66228 branch January 14, 2021 06:01
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jan 15, 2021
Rework diagnostics for wrong number of generic args (fixes rust-lang#66228 and rust-lang#71924)

This PR reworks the `wrong number of {} arguments` message, so that it provides more details and contextual hints.
@lcnr lcnr added the A-const-generics Area: const generics (parameters and arguments) label Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants