Skip to content

Commit 91c03ef

Browse files
authoredJul 24, 2024
Rollup merge of #127374 - estebank:wrong-generic-args, r=oli-obk
Tweak "wrong # of generics" suggestions Fix incorrect suggestion, make verbose and change message to make more sense when it isn't a span label.
2 parents 720c6f1 + 921de9d commit 91c03ef

File tree

57 files changed

+184
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+184
-176
lines changed
 

‎compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
888888
let comma = if args.len() > 0 { ", " } else { "" };
889889
let trait_path = self.tcx.def_path_str(trait_def_id);
890890
let method_name = self.tcx.item_name(self.def_id);
891-
err.span_suggestion(
891+
err.span_suggestion_verbose(
892892
expr.span,
893893
msg,
894894
format!("{trait_path}::{generics}::{method_name}({rcvr}{comma}{rest})"),
@@ -939,18 +939,20 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
939939
}
940940
}
941941

942-
let span_lo_redundant_lt_args = lt_arg_spans[self.num_expected_lifetime_args()];
942+
let span_lo_redundant_lt_args = if self.num_expected_lifetime_args() == 0 {
943+
lt_arg_spans[0]
944+
} else {
945+
lt_arg_spans[self.num_expected_lifetime_args() - 1]
946+
};
943947
let span_hi_redundant_lt_args = lt_arg_spans[lt_arg_spans.len() - 1];
944948

945-
let span_redundant_lt_args = span_lo_redundant_lt_args.to(span_hi_redundant_lt_args);
949+
let span_redundant_lt_args =
950+
span_lo_redundant_lt_args.shrink_to_hi().to(span_hi_redundant_lt_args);
946951
debug!("span_redundant_lt_args: {:?}", span_redundant_lt_args);
947952

948953
let num_redundant_lt_args = lt_arg_spans.len() - self.num_expected_lifetime_args();
949-
let msg_lifetimes = format!(
950-
"remove {these} lifetime argument{s}",
951-
these = pluralize!("this", num_redundant_lt_args),
952-
s = pluralize!(num_redundant_lt_args),
953-
);
954+
let msg_lifetimes =
955+
format!("remove the lifetime argument{s}", s = pluralize!(num_redundant_lt_args));
954956

955957
err.span_suggestion(
956958
span_redundant_lt_args,
@@ -979,18 +981,22 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
979981
}
980982

981983
let span_lo_redundant_type_or_const_args =
982-
gen_arg_spans[self.num_expected_type_or_const_args()];
984+
if self.num_expected_type_or_const_args() == 0 {
985+
gen_arg_spans[0]
986+
} else {
987+
gen_arg_spans[self.num_expected_type_or_const_args() - 1]
988+
};
983989
let span_hi_redundant_type_or_const_args = gen_arg_spans[gen_arg_spans.len() - 1];
990+
let span_redundant_type_or_const_args = span_lo_redundant_type_or_const_args
991+
.shrink_to_hi()
992+
.to(span_hi_redundant_type_or_const_args);
984993

985-
let span_redundant_type_or_const_args =
986-
span_lo_redundant_type_or_const_args.to(span_hi_redundant_type_or_const_args);
987994
debug!("span_redundant_type_or_const_args: {:?}", span_redundant_type_or_const_args);
988995

989996
let num_redundant_gen_args =
990997
gen_arg_spans.len() - self.num_expected_type_or_const_args();
991998
let msg_types_or_consts = format!(
992-
"remove {these} generic argument{s}",
993-
these = pluralize!("this", num_redundant_gen_args),
999+
"remove the unnecessary generic argument{s}",
9941000
s = pluralize!(num_redundant_gen_args),
9951001
);
9961002

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

10381044
let msg = format!(
1039-
"remove these {}generics",
1045+
"remove the unnecessary {}generics",
10401046
if self.gen_args.parenthesized == hir::GenericArgsParentheses::ParenSugar {
10411047
"parenthetical "
10421048
} else {

‎tests/rustdoc-ui/invalid_const_in_lifetime_position.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
1818
--> $DIR/invalid_const_in_lifetime_position.rs:4:26
1919
|
2020
LL | fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
21-
| ^--- help: remove these generics
21+
| ^--- help: remove the unnecessary generics
2222
| |
2323
| expected 0 generic arguments
2424
|
@@ -49,7 +49,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
4949
--> $DIR/invalid_const_in_lifetime_position.rs:4:26
5050
|
5151
LL | fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
52-
| ^--- help: remove these generics
52+
| ^--- help: remove the unnecessary generics
5353
| |
5454
| expected 0 generic arguments
5555
|
@@ -81,7 +81,7 @@ error[E0107]: associated type takes 0 generic arguments but 1 generic argument w
8181
--> $DIR/invalid_const_in_lifetime_position.rs:4:26
8282
|
8383
LL | fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
84-
| ^--- help: remove these generics
84+
| ^--- help: remove the unnecessary generics
8585
| |
8686
| expected 0 generic arguments
8787
|

0 commit comments

Comments
 (0)
Please sign in to comment.