Skip to content

Commit

Permalink
get_hir_params_with_generics: Don't return useless results.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Feb 3, 2025
1 parent 6378fbc commit 51ebe10
Show file tree
Hide file tree
Showing 131 changed files with 380 additions and 487 deletions.
27 changes: 16 additions & 11 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2373,9 +2373,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut generics_with_unmatched_params = Vec::new();

let check_for_matched_generics = || {
if matched_inputs.iter().any(|x| x.is_some())
&& params_with_generics.iter().any(|(x, _)| x.is_some())
{
if matched_inputs.iter().any(|x| x.is_some()) {
for (idx, (generic, _)) in params_with_generics.iter_enumerated() {
// Param has to have a generic and be matched to be relevant
if matched_inputs[idx].is_none() {
Expand Down Expand Up @@ -2666,8 +2664,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

/// Returns the parameters of a function, with their generic parameters if those are the full
/// type of that parameter. Returns `None` if the function has no generics or the body is
/// unavailable (eg is an instrinsic).
/// type of that parameter.
///
/// Returns `None` if the function has no parameters taking generic type, or the function is
/// both not a trait function and has no available body (eg is an instrinsic).
fn get_hir_params_with_generics(
&self,
def_id: DefId,
Expand Down Expand Up @@ -2695,33 +2695,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};

// Make sure to remove both the receiver and variadic argument. Both are removed
// when matching parameter types.
// when warning about mismatched parameters.
let mut found_generic = false;
let fn_inputs = sig.decl.inputs.get(is_method as usize..)?.iter().map(|param| {
if let hir::TyKind::Path(QPath::Resolved(
_,
&hir::Path { res: Res::Def(_, res_def_id), .. },
)) = param.kind
{
generics.params.iter().find(|param| param.def_id.to_def_id() == res_def_id)
let res =
generics.params.iter().find(|param| param.def_id.to_def_id() == res_def_id);
found_generic |= res.is_some();
res
} else {
None
}
});
match (body_id, param_names) {
let res = match (body_id, param_names) {
(Some(_), Some(_)) | (None, None) => unreachable!(),
(Some(body), None) => {
let params = self.tcx.hir().body(body).params;
let params =
params.get(is_method as usize..params.len() - sig.decl.c_variadic as usize)?;
debug_assert_eq!(params.len(), fn_inputs.len());
Some(fn_inputs.zip(params.iter().map(|param| FnParam::Param(param))).collect())
fn_inputs.zip(params.iter().map(|param| FnParam::Param(param))).collect()
}
(None, Some(params)) => {
let params = params.get(is_method as usize..)?;
debug_assert_eq!(params.len(), fn_inputs.len());
Some(fn_inputs.zip(params.iter().map(|param| FnParam::Name(param))).collect())
fn_inputs.zip(params.iter().map(|param| FnParam::Name(param))).collect()
}
}
};
found_generic.then_some(res)
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/crashes/135124.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ trait A {
fn y(&self)
{
fn call() -> impl Sized {}
self.fold(call());
self.fold(call(), call());
}
fn fold(&self, &self._) {}
fn fold<T>(&self, _: T, &self._) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ note: function defined here
|
LL | fn oom(
| ^^^
LL | info: &Layout,
| -------------
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ note: function defined here
|
LL | fn oom(
| ^^^
LL | info: Layout,
| ------------
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/argument-suggestions/basic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ note: function defined here
--> $DIR/basic.rs:13:4
|
LL | fn invalid(_i: u32) {}
| ^^^^^^^ -------
| ^^^^^^^

error[E0061]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/basic.rs:21:5
Expand Down Expand Up @@ -39,7 +39,7 @@ note: function defined here
--> $DIR/basic.rs:15:4
|
LL | fn missing(_i: u32) {}
| ^^^^^^^ -------
| ^^^^^^^
help: provide the argument
|
LL | missing(/* u32 */);
Expand All @@ -57,7 +57,7 @@ note: function defined here
--> $DIR/basic.rs:16:4
|
LL | fn swapped(_i: u32, _s: &str) {}
| ^^^^^^^ ------- --------
| ^^^^^^^
help: swap these arguments
|
LL | swapped(1, "");
Expand All @@ -76,7 +76,7 @@ note: function defined here
--> $DIR/basic.rs:17:4
|
LL | fn permuted(_x: X, _y: Y, _z: Z) {}
| ^^^^^^^^ ----- ----- -----
| ^^^^^^^^
help: reorder these arguments
|
LL | permuted(X {}, Y {}, Z {});
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/complex.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ note: function defined here
--> $DIR/complex.rs:11:4
|
LL | fn complex(_i: u32, _s: &str, _e: E, _f: F, _g: G, _x: X, _y: Y, _z: Z ) {}
| ^^^^^^^ ------- -------- ----- ----- ----- ----- ----- -----
| ^^^^^^^
help: did you mean
|
LL | complex(/* u32 */, &"", /* E */, F::X2, G{}, X {}, Y {}, Z {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ note: function defined here
--> $DIR/display-is-suggestable.rs:3:4
|
LL | fn foo(x: &(dyn Display + Send)) {}
| ^^^ ------------------------
| ^^^
help: provide the argument
|
LL | foo(/* &dyn std::fmt::Display + Send */);
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/argument-suggestions/extra_arguments.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:3:4
|
LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
| ^^^^^^^^^^^^
help: remove the extra argument
|
LL - two_arg_same(1, 1, 1);
Expand All @@ -114,7 +114,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:3:4
|
LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
| ^^^^^^^^^^^^
help: remove the extra argument
|
LL - two_arg_same(1, 1, 1.0);
Expand All @@ -131,7 +131,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:4:4
|
LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
| ^^^^^^^^^^^^
help: remove the extra argument
|
LL - two_arg_diff(1, 1, "");
Expand All @@ -148,7 +148,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:4:4
|
LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
| ^^^^^^^^^^^^
help: remove the extra argument
|
LL - two_arg_diff(1, "", "");
Expand All @@ -167,7 +167,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:4:4
|
LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
| ^^^^^^^^^^^^
help: remove the extra arguments
|
LL - two_arg_diff(1, 1, "", "");
Expand All @@ -186,7 +186,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:4:4
|
LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
| ^^^^^^^^^^^^
help: remove the extra arguments
|
LL - two_arg_diff(1, "", 1, "");
Expand All @@ -203,7 +203,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:3:4
|
LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
| ^^^^^^^^^^^^
help: remove the extra argument
|
LL - two_arg_same(1, 1, "");
Expand All @@ -220,7 +220,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:4:4
|
LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
| ^^^^^^^^^^^^
help: remove the extra argument
|
LL - two_arg_diff(1, 1, "");
Expand All @@ -240,7 +240,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:3:4
|
LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
| ^^^^^^^^^^^^
help: remove the extra argument
|
LL - 1,
Expand All @@ -261,7 +261,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:4:4
|
LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
| ^^^^^^^^^^^^
help: remove the extra argument
|
LL - 1,
Expand Down
Loading

0 comments on commit 51ebe10

Please sign in to comment.