Skip to content

Commit 283bf92

Browse files
committed
Point at definition of item with type parameters that couldn't be inferred
When calling a function with type parameters that are unconstrained by the call expression, we mention the type parameter that couldn't be inferred, but the user previously didn't see where the name was coming from. By pointing at the function itself, including the type parameter, we give better context on what they should have done, specially when the inference machinery fails to provide any other context. ``` error[E0282]: type annotations needed --> $DIR/unresolved_type_param.rs:9:5 | LL | bar().await; | ^^^ cannot infer type of the type parameter `T` declared on the function `bar` | note: type must be known for this --> $DIR/unresolved_type_param.rs:6:1 | LL | async fn bar<T>() -> () {} | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider specifying the generic argument | LL | bar::<T>().await; | +++++ ``` We should further refine when we present this info to make sure we don't provide redundant notes in some cases. Also, there are cases (like those in issue 94969) that don't provide any new information with this change.
1 parent 35f1109 commit 283bf92

107 files changed

Lines changed: 656 additions & 5 deletions

File tree

Some content is hidden

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

compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub struct InferenceDiagnosticsData {
6868
pub struct InferenceDiagnosticsParentData {
6969
prefix: &'static str,
7070
name: String,
71+
span: Span,
7172
}
7273

7374
#[derive(Clone)]
@@ -124,10 +125,12 @@ impl InferenceDiagnosticsParentData {
124125
) -> Option<InferenceDiagnosticsParentData> {
125126
let parent_name =
126127
tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string();
128+
let span = tcx.def_span(parent_def_id);
127129

128130
Some(InferenceDiagnosticsParentData {
129131
prefix: tcx.def_descr(parent_def_id),
130132
name: parent_name,
133+
span,
131134
})
132135
}
133136

@@ -423,16 +426,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
423426
error_code: TypeAnnotationNeeded,
424427
) -> Diag<'a> {
425428
let source_kind = "other";
426-
let source_name = "";
429+
let bad_error = arg_data.make_bad_error(span);
430+
let source_name = &bad_error.name.clone();
427431
let failure_span = None;
428432
let infer_subdiags = Vec::new();
429433
let multi_suggestions = Vec::new();
430-
let bad_label = Some(arg_data.make_bad_error(span));
434+
let bad_label = Some(bad_error);
431435
match error_code {
432436
TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
433437
span,
434438
source_kind,
435439
source_name,
440+
source_span: arg_data.parent.as_ref().map(|p| p.span),
436441
failure_span,
437442
infer_subdiags,
438443
multi_suggestions,
@@ -442,6 +447,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
442447
span,
443448
source_kind,
444449
source_name,
450+
source_span: arg_data.parent.as_ref().map(|p| p.span),
445451
failure_span,
446452
infer_subdiags,
447453
multi_suggestions,
@@ -451,6 +457,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
451457
span,
452458
source_kind,
453459
source_name,
460+
source_span: arg_data.parent.as_ref().map(|p| p.span),
454461
failure_span,
455462
infer_subdiags,
456463
multi_suggestions,
@@ -651,6 +658,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
651658
span,
652659
source_kind,
653660
source_name: &name,
661+
source_span: arg_data.parent.as_ref().map(|p| p.span),
654662
failure_span,
655663
infer_subdiags,
656664
multi_suggestions,
@@ -660,6 +668,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
660668
span,
661669
source_kind,
662670
source_name: &name,
671+
source_span: arg_data.parent.as_ref().map(|p| p.span),
663672
failure_span,
664673
infer_subdiags,
665674
multi_suggestions,
@@ -669,6 +678,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
669678
span,
670679
source_kind,
671680
source_name: &name,
681+
source_span: arg_data.parent.as_ref().map(|p| p.span),
672682
failure_span,
673683
infer_subdiags,
674684
multi_suggestions,

compiler/rustc_trait_selection/src/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ pub struct AnnotationRequired<'a> {
192192
pub span: Span,
193193
pub source_kind: &'static str,
194194
pub source_name: &'a str,
195+
#[note("type must be known for type parameter in this")]
196+
pub source_span: Option<Span>,
195197
#[label("type must be known at this point")]
196198
pub failure_span: Option<Span>,
197199
#[subdiagnostic]
@@ -214,6 +216,8 @@ pub struct AmbiguousImpl<'a> {
214216
pub span: Span,
215217
pub source_kind: &'static str,
216218
pub source_name: &'a str,
219+
#[note("type must be known for type parameter in this")]
220+
pub source_span: Option<Span>,
217221
#[label("type must be known at this point")]
218222
pub failure_span: Option<Span>,
219223
#[subdiagnostic]
@@ -236,6 +240,8 @@ pub struct AmbiguousReturn<'a> {
236240
pub span: Span,
237241
pub source_kind: &'static str,
238242
pub source_name: &'a str,
243+
#[note("type must be known for type parameter in this")]
244+
pub source_span: Option<Span>,
239245
#[label("type must be known at this point")]
240246
pub failure_span: Option<Span>,
241247
#[subdiagnostic]

tests/ui/array-slice-vec/vector-no-ann.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ error[E0282]: type annotations needed for `Vec<_>`
44
LL | let _foo = Vec::new();
55
| ^^^^ ---------- type must be known at this point
66
|
7+
note: type must be known for type parameter in this
8+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
79
help: consider giving `_foo` an explicit type, where the type for type parameter `T` is specified
810
|
911
LL | let _foo: Vec<T> = Vec::new();

tests/ui/associated-type-bounds/dedup-normalized-2-higher-ranked.current.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ LL | impls(rigid);
66
| |
77
| cannot infer type of the type parameter `U` declared on the function `impls`
88
|
9+
note: type must be known for type parameter in this
10+
--> $DIR/dedup-normalized-2-higher-ranked.rs:25:1
11+
|
12+
LL | fn impls<T: for<'b> Bound<'b, U>, U>(_: T) {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
914
= note: cannot satisfy `for<'b> <P as Trait>::Rigid: Bound<'b, _>`
1015
note: required by a bound in `impls`
1116
--> $DIR/dedup-normalized-2-higher-ranked.rs:25:13

tests/ui/associated-type-bounds/duplicate-bound-err.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ error[E0282]: type annotations needed
44
LL | iter::empty()
55
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
66
|
7+
note: type must be known for type parameter in this
8+
--> $SRC_DIR/core/src/iter/sources/empty.rs:LL:COL
79
help: consider specifying the generic argument
810
|
911
LL | iter::empty::<T>()
@@ -15,6 +17,8 @@ error[E0282]: type annotations needed
1517
LL | iter::empty()
1618
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
1719
|
20+
note: type must be known for type parameter in this
21+
--> $SRC_DIR/core/src/iter/sources/empty.rs:LL:COL
1822
help: consider specifying the generic argument
1923
|
2024
LL | iter::empty::<T>()
@@ -26,6 +30,8 @@ error[E0282]: type annotations needed
2630
LL | iter::empty()
2731
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
2832
|
33+
note: type must be known for type parameter in this
34+
--> $SRC_DIR/core/src/iter/sources/empty.rs:LL:COL
2935
help: consider specifying the generic argument
3036
|
3137
LL | iter::empty::<T>()

tests/ui/async-await/unresolved_type_param.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0282]: type annotations needed
44
LL | bar().await;
55
| ^^^ cannot infer type of the type parameter `T` declared on the function `bar`
66
|
7+
note: type must be known for type parameter in this
8+
--> $DIR/unresolved_type_param.rs:6:1
9+
|
10+
LL | async fn bar<T>() -> () {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^
712
help: consider specifying the generic argument
813
|
914
LL | bar::<T>().await;

tests/ui/closure-expected-type/expect-two-infer-vars-supply-ty-with-bound-region.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ error[E0282]: type annotations needed
44
LL | with_closure(|x: u32, y| {});
55
| ^
66
|
7+
note: type must be known for type parameter in this
8+
--> $DIR/expect-two-infer-vars-supply-ty-with-bound-region.rs:1:1
9+
|
10+
LL | / fn with_closure<F, A, B>(_: F)
11+
LL | | where F: FnOnce(A, B)
12+
| |_________________________^
713
help: consider giving this closure parameter an explicit type
814
|
915
LL | with_closure(|x: u32, y: /* Type */| {});

tests/ui/closures/issue-99565.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0282]: type annotations needed
44
LL | foo(|| {});
55
| ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
66
|
7+
note: type must be known for type parameter in this
8+
--> $DIR/issue-99565.rs:3:1
9+
|
10+
LL | fn foo<T, U>(_: U) {}
11+
| ^^^^^^^^^^^^^^^^^^
712
help: consider specifying the generic arguments
813
|
914
LL | foo::<T, _>(|| {});

tests/ui/const-generics/defaults/rp_impl_trait_fail.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ error[E0284]: type annotations needed
5656
LL | uwu();
5757
| ^^^ cannot infer the value of the const parameter `N` declared on the function `uwu`
5858
|
59+
note: type must be known for type parameter in this
60+
--> $DIR/rp_impl_trait_fail.rs:16:1
61+
|
62+
LL | fn uwu<const N: u8>() -> impl Traitor<N> {
63+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5964
note: required by a const generic parameter in `uwu`
6065
--> $DIR/rp_impl_trait_fail.rs:16:8
6166
|

tests/ui/const-generics/fn-const-param-infer.adt_const_params.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ error[E0282]: type annotations needed
1919
LL | let _ = Checked::<generic>;
2020
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
2121
|
22+
note: type must be known for type parameter in this
23+
--> $DIR/fn-const-param-infer.rs:22:1
24+
|
25+
LL | fn generic<T>(val: usize) -> bool {
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2227
help: consider specifying the generic argument
2328
|
2429
LL | let _ = Checked::<generic::<T>>;

0 commit comments

Comments
 (0)