Skip to content

Commit 13ee415

Browse files
committed
get_hir_params_with_generics: Don't return useless results.
1 parent 6378fbc commit 13ee415

8 files changed

+29
-36
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -2373,9 +2373,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23732373
let mut generics_with_unmatched_params = Vec::new();
23742374

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

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

26972697
// Make sure to remove both the receiver and variadic argument. Both are removed
2698-
// when matching parameter types.
2698+
// when warning about mismatched parameters.
2699+
let mut found_generic = false;
26992700
let fn_inputs = sig.decl.inputs.get(is_method as usize..)?.iter().map(|param| {
27002701
if let hir::TyKind::Path(QPath::Resolved(
27012702
_,
27022703
&hir::Path { res: Res::Def(_, res_def_id), .. },
27032704
)) = param.kind
27042705
{
2705-
generics.params.iter().find(|param| param.def_id.to_def_id() == res_def_id)
2706+
let res =
2707+
generics.params.iter().find(|param| param.def_id.to_def_id() == res_def_id);
2708+
found_generic |= res.is_some();
2709+
res
27062710
} else {
27072711
None
27082712
}
27092713
});
2710-
match (body_id, param_names) {
2714+
let res = match (body_id, param_names) {
27112715
(Some(_), Some(_)) | (None, None) => unreachable!(),
27122716
(Some(body), None) => {
27132717
let params = self.tcx.hir().body(body).params;
27142718
let params =
27152719
params.get(is_method as usize..params.len() - sig.decl.c_variadic as usize)?;
27162720
debug_assert_eq!(params.len(), fn_inputs.len());
2717-
Some(fn_inputs.zip(params.iter().map(|param| FnParam::Param(param))).collect())
2721+
fn_inputs.zip(params.iter().map(|param| FnParam::Param(param))).collect()
27182722
}
27192723
(None, Some(params)) => {
27202724
let params = params.get(is_method as usize..)?;
27212725
debug_assert_eq!(params.len(), fn_inputs.len());
2722-
Some(fn_inputs.zip(params.iter().map(|param| FnParam::Name(param))).collect())
2726+
fn_inputs.zip(params.iter().map(|param| FnParam::Name(param))).collect()
27232727
}
2724-
}
2728+
};
2729+
found_generic.then_some(res)
27252730
}
27262731
}
27272732

tests/ui/fn/signature-error-reporting-under-verbose.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ fn foo(_: i32, _: i32) {}
44

55
fn needs_ptr(_: fn(i32, u32)) {}
66
//~^ NOTE function defined here
7-
//~| NOTE
87

98
fn main() {
109
needs_ptr(foo);

tests/ui/fn/signature-error-reporting-under-verbose.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/signature-error-reporting-under-verbose.rs:10:15
2+
--> $DIR/signature-error-reporting-under-verbose.rs:9:15
33
|
44
LL | needs_ptr(foo);
55
| --------- ^^^ expected fn pointer, found fn item
@@ -12,7 +12,7 @@ note: function defined here
1212
--> $DIR/signature-error-reporting-under-verbose.rs:5:4
1313
|
1414
LL | fn needs_ptr(_: fn(i32, u32)) {}
15-
| ^^^^^^^^^ ---------------
15+
| ^^^^^^^^^
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/mismatched_types/normalize-fn-sig.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ note: function defined here
1212
--> $DIR/normalize-fn-sig.rs:11:4
1313
|
1414
LL | fn needs_i32_ref_fn(_: fn(&'static i32, i32)) {}
15-
| ^^^^^^^^^^^^^^^^ ------------------------
15+
| ^^^^^^^^^^^^^^^^
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/never_type/call-fn-never-arg-wrong-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ note: function defined here
1212
--> $DIR/call-fn-never-arg-wrong-type.rs:5:4
1313
|
1414
LL | fn foo(x: !) -> ! {
15-
| ^^^ ----
15+
| ^^^
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/parser/fn-arg-doc-comment.rs

-3
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ pub fn f( //~ NOTE function defined here
22
/// Comment
33
//~^ ERROR documentation comments cannot be applied to function parameters
44
//~| NOTE doc comments are not allowed here
5-
//~| NOTE
65
id: u8,
76
/// Other
87
//~^ ERROR documentation comments cannot be applied to function parameters
98
//~| NOTE doc comments are not allowed here
10-
//~| NOTE
119
a: u8,
1210
) {}
1311

1412
fn bar(id: #[allow(dead_code)] i32) {}
1513
//~^ ERROR attributes cannot be applied to a function parameter's type
1614
//~| NOTE attributes are not allowed here
1715
//~| NOTE function defined here
18-
//~| NOTE
1916

2017
fn main() {
2118
// verify that the parser recovered and properly typechecked the args

tests/ui/parser/fn-arg-doc-comment.stderr

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: attributes cannot be applied to a function parameter's type
2-
--> $DIR/fn-arg-doc-comment.rs:14:12
2+
--> $DIR/fn-arg-doc-comment.rs:12:12
33
|
44
LL | fn bar(id: #[allow(dead_code)] i32) {}
55
| ^^^^^^^^^^^^^^^^^^^ attributes are not allowed here
@@ -11,13 +11,13 @@ LL | /// Comment
1111
| ^^^^^^^^^^^ doc comments are not allowed here
1212

1313
error: documentation comments cannot be applied to function parameters
14-
--> $DIR/fn-arg-doc-comment.rs:7:5
14+
--> $DIR/fn-arg-doc-comment.rs:6:5
1515
|
1616
LL | /// Other
1717
| ^^^^^^^^^ doc comments are not allowed here
1818

1919
error[E0308]: arguments to this function are incorrect
20-
--> $DIR/fn-arg-doc-comment.rs:22:5
20+
--> $DIR/fn-arg-doc-comment.rs:19:5
2121
|
2222
LL | f("", "");
2323
| ^ -- -- expected `u8`, found `&str`
@@ -27,30 +27,22 @@ LL | f("", "");
2727
note: function defined here
2828
--> $DIR/fn-arg-doc-comment.rs:1:8
2929
|
30-
LL | pub fn f(
31-
| ^
32-
LL | / /// Comment
33-
... |
34-
LL | | id: u8,
35-
| |__________-
36-
LL | / /// Other
37-
... |
38-
LL | | a: u8,
39-
| |_________-
30+
LL | pub fn f(
31+
| ^
4032

4133
error[E0308]: mismatched types
42-
--> $DIR/fn-arg-doc-comment.rs:26:9
34+
--> $DIR/fn-arg-doc-comment.rs:23:9
4335
|
4436
LL | bar("");
4537
| --- ^^ expected `i32`, found `&str`
4638
| |
4739
| arguments to this function are incorrect
4840
|
4941
note: function defined here
50-
--> $DIR/fn-arg-doc-comment.rs:14:4
42+
--> $DIR/fn-arg-doc-comment.rs:12:4
5143
|
5244
LL | fn bar(id: #[allow(dead_code)] i32) {}
53-
| ^^^ ---------------------------
45+
| ^^^
5446

5547
error: aborting due to 5 previous errors
5648

tests/ui/regions/regions-fn-subtyping-return-static-fail.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ note: function defined here
1212
--> $DIR/regions-fn-subtyping-return-static-fail.rs:20:4
1313
|
1414
LL | fn want_G(f: G) {}
15-
| ^^^^^^ ----
15+
| ^^^^^^
1616

1717
error: aborting due to 1 previous error
1818

0 commit comments

Comments
 (0)