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

Tweak "wrong # of generics" suggestions #127374

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
let comma = if args.len() > 0 { ", " } else { "" };
let trait_path = self.tcx.def_path_str(trait_def_id);
let method_name = self.tcx.item_name(self.def_id);
err.span_suggestion(
err.span_suggestion_verbose(
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you forget this one?

Copy link
Contributor Author

@estebank estebank Jul 23, 2024

Choose a reason for hiding this comment

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

Those correspond to these, https://github.com/rust-lang/rust/pull/127374/files/921de9d8eae16947c375febe0ab8709797b37119#diff-1d620ac138e59caa4ee1265c976245aea8bb80648f969e59af382d38bfd84dd9R14, which I could not find an example where they would be rendered on their own, as when there are more than one suggestion they then get rendered as verbose, but given how long the suggested code and span are, I'd prefer to leave these as verbose. (I personally find "help: frobnicate the expression: very::long::code::<that, goes, const { on }>(and, on)" to be quite hard to read.)

expr.span,
msg,
format!("{trait_path}::{generics}::{method_name}({rcvr}{comma}{rest})"),
Expand Down Expand Up @@ -939,18 +939,20 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}
}

let span_lo_redundant_lt_args = lt_arg_spans[self.num_expected_lifetime_args()];
let span_lo_redundant_lt_args = if self.num_expected_lifetime_args() == 0 {
lt_arg_spans[0]
} else {
lt_arg_spans[self.num_expected_lifetime_args() - 1]
};
let span_hi_redundant_lt_args = lt_arg_spans[lt_arg_spans.len() - 1];

let span_redundant_lt_args = span_lo_redundant_lt_args.to(span_hi_redundant_lt_args);
let span_redundant_lt_args =
span_lo_redundant_lt_args.shrink_to_hi().to(span_hi_redundant_lt_args);
debug!("span_redundant_lt_args: {:?}", span_redundant_lt_args);

let num_redundant_lt_args = lt_arg_spans.len() - self.num_expected_lifetime_args();
let msg_lifetimes = format!(
"remove {these} lifetime argument{s}",
these = pluralize!("this", num_redundant_lt_args),
s = pluralize!(num_redundant_lt_args),
);
let msg_lifetimes =
format!("remove the lifetime argument{s}", s = pluralize!(num_redundant_lt_args));

err.span_suggestion(
span_redundant_lt_args,
Expand Down Expand Up @@ -979,18 +981,22 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}

let span_lo_redundant_type_or_const_args =
gen_arg_spans[self.num_expected_type_or_const_args()];
if self.num_expected_type_or_const_args() == 0 {
gen_arg_spans[0]
} else {
gen_arg_spans[self.num_expected_type_or_const_args() - 1]
};
let span_hi_redundant_type_or_const_args = gen_arg_spans[gen_arg_spans.len() - 1];
let span_redundant_type_or_const_args = span_lo_redundant_type_or_const_args
.shrink_to_hi()
.to(span_hi_redundant_type_or_const_args);

let span_redundant_type_or_const_args =
span_lo_redundant_type_or_const_args.to(span_hi_redundant_type_or_const_args);
debug!("span_redundant_type_or_const_args: {:?}", span_redundant_type_or_const_args);

let num_redundant_gen_args =
gen_arg_spans.len() - self.num_expected_type_or_const_args();
let msg_types_or_consts = format!(
"remove {these} generic argument{s}",
these = pluralize!("this", num_redundant_gen_args),
"remove the unnecessary generic argument{s}",
s = pluralize!(num_redundant_gen_args),
);

Expand Down Expand Up @@ -1036,7 +1042,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
.with_lo(self.path_segment.ident.span.hi());

let msg = format!(
"remove these {}generics",
"remove the unnecessary {}generics",
if self.gen_args.parenthesized == hir::GenericArgsParentheses::ParenSugar {
"parenthetical "
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/rustdoc-ui/invalid_const_in_lifetime_position.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
--> $DIR/invalid_const_in_lifetime_position.rs:4:26
|
LL | fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
| ^--- help: remove these generics
| ^--- help: remove the unnecessary generics
| |
| expected 0 generic arguments
|
Expand Down Expand Up @@ -49,7 +49,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
--> $DIR/invalid_const_in_lifetime_position.rs:4:26
|
LL | fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
| ^--- help: remove these generics
| ^--- help: remove the unnecessary generics
| |
| expected 0 generic arguments
|
Expand Down Expand Up @@ -81,7 +81,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
--> $DIR/invalid_const_in_lifetime_position.rs:4:26
|
LL | fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
| ^--- help: remove these generics
| ^--- help: remove the unnecessary generics
| |
| expected 0 generic arguments
|
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/mismatched_arg_count.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0107]: type alias takes 1 lifetime argument but 2 lifetime arguments were
--> $DIR/mismatched_arg_count.rs:7:29
|
LL | fn bar<'a, T: Trait<'a>>(_: Alias<'a, 'a, T>) {}
| ^^^^^ -- help: remove this lifetime argument
| ^^^^^ ---- help: remove the lifetime argument
| |
| expected 1 lifetime argument
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/issue-100154.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0107]: function takes 0 generic arguments but 1 generic argument was supp
--> $DIR/issue-100154.rs:4:5
|
LL | foo::<()>(());
| ^^^------ help: remove these generics
| ^^^------ help: remove the unnecessary generics
| |
| expected 0 generic arguments
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/borrowck/issue-82126-mismatched-subst-and-hir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0107]: struct takes 0 lifetime arguments but 1 lifetime argument was supp
--> $DIR/issue-82126-mismatched-subst-and-hir.rs:16:59
|
LL | async fn buy_lock(coroutine: &Mutex<MarketMultiplier>) -> LockedMarket<'_> {
| ^^^^^^^^^^^^---- help: remove these generics
| ^^^^^^^^^^^^---- help: remove the unnecessary generics
| |
| expected 0 lifetime arguments
|
Expand Down Expand Up @@ -32,7 +32,7 @@ error[E0107]: struct takes 0 lifetime arguments but 1 lifetime argument was supp
--> $DIR/issue-82126-mismatched-subst-and-hir.rs:16:59
|
LL | async fn buy_lock(coroutine: &Mutex<MarketMultiplier>) -> LockedMarket<'_> {
| ^^^^^^^^^^^^---- help: remove these generics
| ^^^^^^^^^^^^---- help: remove the unnecessary generics
| |
| expected 0 lifetime arguments
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0107]: trait takes at most 2 generic arguments but 3 generic arguments we
--> $DIR/transmutable-ice-110969.rs:11:14
|
LL | Dst: BikeshedIntrinsicFrom<Src, Context, ASSUME>,
| ^^^^^^^^^^^^^^^^^^^^^ ------ help: remove this generic argument
| ^^^^^^^^^^^^^^^^^^^^^ -------- help: remove the unnecessary generic argument
| |
| expected at most 2 generic arguments

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ error[E0107]: struct takes 2 generic arguments but 3 generic arguments were supp
--> $DIR/infer-arg-test.rs:18:10
|
LL | let a: All<_, _, _>;
| ^^^ - help: remove this generic argument
| ^^^ --- help: remove the unnecessary generic argument
| |
| expected 2 generic arguments
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0107]: function takes 1 generic argument but 2 generic arguments were sup
--> $DIR/issue_114151.rs:17:5
|
LL | foo::<_, L>([(); L + 1 + L]);
| ^^^ - help: remove this generic argument
| ^^^ --- help: remove the unnecessary generic argument
| |
| expected 1 generic argument
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
--> $DIR/issue-102768.rs:9:30
|
LL | fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
| ^--- help: remove these generics
| ^--- help: remove the unnecessary generics
| |
| expected 0 generic arguments
|
Expand Down Expand Up @@ -49,7 +49,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
--> $DIR/issue-102768.rs:9:30
|
LL | fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
| ^--- help: remove these generics
| ^--- help: remove the unnecessary generics
| |
| expected 0 generic arguments
|
Expand Down Expand Up @@ -81,7 +81,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
--> $DIR/issue-102768.rs:9:30
|
LL | fn f2<'a>(arg: Box<dyn X<Y<1> = &'a ()>>) {}
| ^--- help: remove these generics
| ^--- help: remove the unnecessary generics
| |
| expected 0 generic arguments
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ error[E0107]: function takes 2 generic arguments but 3 generic arguments were su
--> $DIR/incorrect-number-of-const-args.rs:9:5
|
LL | foo::<0, 0, 0>();
| ^^^ - help: remove this generic argument
| ^^^ --- help: remove the unnecessary generic argument
| |
| expected 2 generic arguments
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ help: consider moving this generic argument to the `TryInto` trait, which takes
|
LL | let _: u32 = TryInto::<32>::try_into(5i32).unwrap();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: remove these generics
help: remove the unnecessary generics
|
LL - let _: u32 = 5i32.try_into::<32>().unwrap();
LL + let _: u32 = 5i32.try_into().unwrap();
Expand All @@ -27,7 +27,7 @@ error[E0107]: struct takes 0 generic arguments but 1 generic argument was suppli
--> $DIR/invalid-const-arg-for-type-param.rs:12:5
|
LL | S::<0>;
| ^----- help: remove these generics
| ^----- help: remove the unnecessary generics
| |
| expected 0 generic arguments
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/invalid-constant-in-args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0107]: struct takes 1 generic argument but 2 generic arguments were suppl
--> $DIR/invalid-constant-in-args.rs:4:12
|
LL | let _: Cell<&str, "a"> = Cell::new("");
| ^^^^ --- help: remove this generic argument
| ^^^^ ----- help: remove the unnecessary generic argument
| |
| expected 1 generic argument

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/constructor-lifetime-args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ error[E0107]: struct takes 2 lifetime arguments but 3 lifetime arguments were su
--> $DIR/constructor-lifetime-args.rs:19:5
|
LL | S::<'static, 'static, 'static>(&0, &0);
| ^ ------- help: remove this lifetime argument
| ^ --------- help: remove the lifetime argument
| |
| expected 2 lifetime arguments
|
Expand Down Expand Up @@ -52,7 +52,7 @@ error[E0107]: enum takes 2 lifetime arguments but 3 lifetime arguments were supp
--> $DIR/constructor-lifetime-args.rs:24:8
|
LL | E::V::<'static, 'static, 'static>(&0);
| ^ ------- help: remove this lifetime argument
| ^ --------- help: remove the lifetime argument
| |
| expected 2 lifetime arguments
|
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/effect_param.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ error[E0107]: method takes 0 generic arguments but 1 generic argument was suppli
--> $DIR/effect_param.rs:11:9
|
LL | i8::checked_sub::<false>(42, 43);
| ^^^^^^^^^^^--------- help: remove these generics
| ^^^^^^^^^^^--------- help: remove the unnecessary generics
| |
| expected 0 generic arguments

error[E0107]: method takes 0 generic arguments but 1 generic argument was supplied
--> $DIR/effect_param.rs:13:9
|
LL | i8::checked_sub::<true>(42, 43);
| ^^^^^^^^^^^-------- help: remove these generics
| ^^^^^^^^^^^-------- help: remove the unnecessary generics
| |
| expected 0 generic arguments

error[E0107]: method takes 0 generic arguments but 1 generic argument was supplied
--> $DIR/effect_param.rs:4:9
|
LL | i8::checked_sub::<true>(42, 43);
| ^^^^^^^^^^^-------- help: remove these generics
| ^^^^^^^^^^^-------- help: remove the unnecessary generics
| |
| expected 0 generic arguments

error[E0107]: method takes 0 generic arguments but 1 generic argument was supplied
--> $DIR/effect_param.rs:6:9
|
LL | i8::checked_sub::<false>(42, 43);
| ^^^^^^^^^^^--------- help: remove these generics
| ^^^^^^^^^^^--------- help: remove the unnecessary generics
| |
| expected 0 generic arguments

Expand Down
16 changes: 8 additions & 8 deletions tests/ui/error-codes/E0107.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ struct Baz<'a, 'b, 'c> {

bar: Bar<'a>,
//~^ ERROR enum takes 0 lifetime arguments
//~| HELP remove these generics
//~| HELP remove the unnecessary generics

foo2: Foo<'a, 'b, 'c>,
//~^ ERROR struct takes 1 lifetime argument
//~| HELP remove these lifetime arguments
//~| HELP remove the lifetime arguments

qux1: Qux<'a, 'b, i32>,
//~^ ERROR struct takes 1 lifetime argument
//~| HELP remove this lifetime argument
//~| HELP remove the lifetime argument

qux2: Qux<'a, i32, 'b>,
//~^ ERROR struct takes 1 lifetime argument
//~| HELP remove this lifetime argument
//~| HELP remove the lifetime argument

qux3: Qux<'a, 'b, 'c, i32>,
//~^ ERROR struct takes 1 lifetime argument
//~| HELP remove these lifetime arguments
//~| HELP remove the lifetime arguments

qux4: Qux<'a, i32, 'b, 'c>,
//~^ ERROR struct takes 1 lifetime argument
//~| HELP remove these lifetime arguments
//~| HELP remove the lifetime arguments

qux5: Qux<'a, 'b, i32, 'c>,
//~^ ERROR struct takes 1 lifetime argument
//~| HELP remove this lifetime argument
//~| HELP remove the lifetime argument

quux: Quux<'a, i32, 'b>,
//~^ ERROR struct takes 0 lifetime arguments
//~| HELP remove this lifetime argument
//~| HELP remove the lifetime argument
}

pub trait T {
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/error-codes/E0107.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ error[E0107]: enum takes 0 lifetime arguments but 1 lifetime argument was suppli
--> $DIR/E0107.rs:17:10
|
LL | bar: Bar<'a>,
| ^^^---- help: remove these generics
| ^^^---- help: remove the unnecessary generics
| |
| expected 0 lifetime arguments
|
Expand All @@ -34,7 +34,7 @@ error[E0107]: struct takes 1 lifetime argument but 3 lifetime arguments were sup
--> $DIR/E0107.rs:21:11
|
LL | foo2: Foo<'a, 'b, 'c>,
| ^^^ ------ help: remove these lifetime arguments
| ^^^ -------- help: remove the lifetime arguments
| |
| expected 1 lifetime argument
|
Expand All @@ -48,7 +48,7 @@ error[E0107]: struct takes 1 lifetime argument but 2 lifetime arguments were sup
--> $DIR/E0107.rs:25:11
|
LL | qux1: Qux<'a, 'b, i32>,
| ^^^ -- help: remove this lifetime argument
| ^^^ ---- help: remove the lifetime argument
| |
| expected 1 lifetime argument
|
Expand All @@ -62,7 +62,7 @@ error[E0107]: struct takes 1 lifetime argument but 2 lifetime arguments were sup
--> $DIR/E0107.rs:29:11
|
LL | qux2: Qux<'a, i32, 'b>,
| ^^^ -- help: remove this lifetime argument
| ^^^ --------- help: remove the lifetime argument
| |
| expected 1 lifetime argument
|
Expand All @@ -76,7 +76,7 @@ error[E0107]: struct takes 1 lifetime argument but 3 lifetime arguments were sup
--> $DIR/E0107.rs:33:11
|
LL | qux3: Qux<'a, 'b, 'c, i32>,
| ^^^ ------ help: remove these lifetime arguments
| ^^^ -------- help: remove the lifetime arguments
| |
| expected 1 lifetime argument
|
Expand All @@ -90,7 +90,7 @@ error[E0107]: struct takes 1 lifetime argument but 3 lifetime arguments were sup
--> $DIR/E0107.rs:37:11
|
LL | qux4: Qux<'a, i32, 'b, 'c>,
| ^^^ ------ help: remove these lifetime arguments
| ^^^ ------------- help: remove the lifetime arguments
| |
| expected 1 lifetime argument
|
Expand All @@ -104,7 +104,7 @@ error[E0107]: struct takes 1 lifetime argument but 3 lifetime arguments were sup
--> $DIR/E0107.rs:41:11
|
LL | qux5: Qux<'a, 'b, i32, 'c>,
| ^^^ -- help: remove this lifetime argument
| ^^^ ---- help: remove the lifetime argument
| |
| expected 1 lifetime argument
|
Expand All @@ -118,7 +118,7 @@ error[E0107]: struct takes 0 lifetime arguments but 2 lifetime arguments were su
--> $DIR/E0107.rs:45:11
|
LL | quux: Quux<'a, i32, 'b>,
| ^^^^ -- help: remove this lifetime argument
| ^^^^ -- help: remove the lifetime argument
| |
| expected 0 lifetime arguments
|
Expand Down
Loading
Loading