Skip to content

Commit

Permalink
Rollup merge of #127374 - estebank:wrong-generic-args, r=oli-obk
Browse files Browse the repository at this point in the history
Tweak "wrong # of generics" suggestions

Fix incorrect suggestion, make verbose and change message to make more sense when it isn't a span label.
  • Loading branch information
matthiaskrgr authored Jul 24, 2024
2 parents 720c6f1 + 921de9d commit 91c03ef
Show file tree
Hide file tree
Showing 57 changed files with 184 additions and 176 deletions.
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(
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

0 comments on commit 91c03ef

Please sign in to comment.