Skip to content

Commit f44ae98

Browse files
Only label place where type is needed if span is meaningful
1 parent 5b9775f commit f44ae98

22 files changed

+51
-40
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
313313
pub fn emit_inference_failure_err(
314314
&self,
315315
body_id: Option<hir::BodyId>,
316-
span: Span,
316+
failure_span: Span,
317317
arg: GenericArg<'tcx>,
318318
// FIXME(#94483): Either use this or remove it.
319319
_impl_candidates: Vec<ty::TraitRef<'tcx>>,
320320
error_code: TypeAnnotationNeeded,
321+
should_label_span: bool,
321322
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
322323
let arg = self.resolve_vars_if_possible(arg);
323324
let arg_data = self.extract_inference_diagnostics_data(arg, None);
@@ -326,7 +327,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
326327
// If we don't have any typeck results we're outside
327328
// of a body, so we won't be able to get better info
328329
// here.
329-
return self.bad_inference_failure_err(span, arg_data, error_code);
330+
return self.bad_inference_failure_err(failure_span, arg_data, error_code);
330331
};
331332
let typeck_results = typeck_results.borrow();
332333
let typeck_results = &typeck_results;
@@ -338,7 +339,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
338339
}
339340

340341
let Some(InferSource { span, kind }) = local_visitor.infer_source else {
341-
return self.bad_inference_failure_err(span, arg_data, error_code)
342+
return self.bad_inference_failure_err(failure_span, arg_data, error_code)
342343
};
343344

344345
let error_code = error_code.into();
@@ -347,6 +348,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
347348
&format!("type annotations needed{}", kind.ty_msg(self)),
348349
error_code,
349350
);
351+
352+
if should_label_span && !failure_span.overlaps(span) {
353+
err.span_label(failure_span, "type must be known at this point");
354+
}
355+
350356
match kind {
351357
InferSourceKind::LetBinding { insert_span, pattern_name, ty } => {
352358
let suggestion_msg = if let Some(name) = pattern_name {

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
20022002
subst,
20032003
vec![],
20042004
ErrorCode::E0282,
2005+
false,
20052006
)
20062007
.emit();
20072008
}
@@ -2019,6 +2020,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
20192020
subst,
20202021
impl_candidates,
20212022
ErrorCode::E0283,
2023+
false,
20222024
);
20232025

20242026
let obligation = Obligation::new(
@@ -2110,7 +2112,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21102112
return;
21112113
}
21122114

2113-
self.emit_inference_failure_err(body_id, span, arg, vec![], ErrorCode::E0282)
2115+
self.emit_inference_failure_err(body_id, span, arg, vec![], ErrorCode::E0282, false)
21142116
}
21152117

21162118
ty::PredicateKind::Subtype(data) => {
@@ -2124,7 +2126,14 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21242126
let SubtypePredicate { a_is_expected: _, a, b } = data;
21252127
// both must be type variables, or the other would've been instantiated
21262128
assert!(a.is_ty_var() && b.is_ty_var());
2127-
self.emit_inference_failure_err(body_id, span, a.into(), vec![], ErrorCode::E0282)
2129+
self.emit_inference_failure_err(
2130+
body_id,
2131+
span,
2132+
a.into(),
2133+
vec![],
2134+
ErrorCode::E0282,
2135+
false,
2136+
)
21282137
}
21292138
ty::PredicateKind::Projection(data) => {
21302139
let self_ty = data.projection_ty.self_ty();
@@ -2140,6 +2149,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21402149
self_ty.into(),
21412150
vec![],
21422151
ErrorCode::E0284,
2152+
false,
21432153
);
21442154
err.note(&format!("cannot satisfy `{}`", predicate));
21452155
err

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1538,9 +1538,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15381538
ty
15391539
} else {
15401540
if !self.is_tainted_by_errors() {
1541-
self.emit_inference_failure_err((**self).body_id, sp, ty.into(), vec![], E0282)
1542-
.note("type must be known at this point")
1543-
.emit();
1541+
self.emit_inference_failure_err(
1542+
(**self).body_id,
1543+
sp,
1544+
ty.into(),
1545+
vec![],
1546+
E0282,
1547+
true,
1548+
)
1549+
.emit();
15441550
}
15451551
let err = self.tcx.ty_error();
15461552
self.demand_suptype(sp, err, ty);

compiler/rustc_typeck/src/check/writeback.rs

+2
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
694694
t.into(),
695695
vec![],
696696
E0282,
697+
false,
697698
)
698699
.emit();
699700
}
@@ -708,6 +709,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
708709
c.into(),
709710
vec![],
710711
E0282,
712+
false,
711713
)
712714
.emit();
713715
}

src/test/ui/array-slice-vec/infer_array_len.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0282]: type annotations needed
44
LL | let [_, _] = a.into();
55
| ^^^^^^
66
|
7-
= note: type must be known at this point
87
help: consider giving this pattern a type
98
|
109
LL | let [_, _]: _ = a.into();

src/test/ui/array-slice-vec/slice-pat-type-mismatches.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ error[E0282]: type annotations needed
2727
|
2828
LL | [] => {}
2929
| ^^ cannot infer type
30-
|
31-
= note: type must be known at this point
3230

3331
error: aborting due to 5 previous errors
3432

src/test/ui/cast/issue-85586.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0282]: type annotations needed
33
|
44
LL | let b = (a + 1) as usize;
55
| ^^^^^^^ cannot infer type
6-
|
7-
= note: type must be known at this point
86

97
error: aborting due to previous error
108

src/test/ui/impl-trait/hidden-type-is-opaque-2.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ error[E0282]: type annotations needed
33
|
44
LL | cont.reify_as();
55
| ^^^^ cannot infer type
6-
|
7-
= note: type must be known at this point
86

97
error[E0282]: type annotations needed
108
--> $DIR/hidden-type-is-opaque-2.rs:18:9
119
|
1210
LL | cont.reify_as();
1311
| ^^^^ cannot infer type
14-
|
15-
= note: type must be known at this point
1612

1713
error: aborting due to 2 previous errors
1814

src/test/ui/issues/issue-15965.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | / { return () }
55
LL | |
66
LL | | ()
77
| |______^ cannot infer type
8-
|
9-
= note: type must be known at this point
108

119
error: aborting due to previous error
1210

src/test/ui/issues/issue-20261.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0282]: type annotations needed
33
|
44
LL | i.clone();
55
| ^^^^^ cannot infer type
6-
|
7-
= note: type must be known at this point
86

97
error: aborting due to previous error
108

src/test/ui/issues/issue-2151.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ error[E0282]: type annotations needed
33
|
44
LL | let x = panic!();
55
| ^
6+
LL | x.clone();
7+
| - type must be known at this point
68
|
7-
= note: type must be known at this point
89
help: consider giving `x` an explicit type
910
|
1011
LL | let x: _ = panic!();

src/test/ui/issues/issue-51116.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn main() {
55
*tile = 0;
66
//~^ ERROR type annotations needed
77
//~| NOTE cannot infer type
8-
//~| NOTE type must be known at this point
98
}
109
}
1110

src/test/ui/issues/issue-51116.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0282]: type annotations needed
33
|
44
LL | *tile = 0;
55
| ^^^^^ cannot infer type
6-
|
7-
= note: type must be known at this point
86

97
error: aborting due to previous error
108

src/test/ui/pattern/issue-88074-pat-range-type-inference-err.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ error[E0282]: type annotations needed
1212
|
1313
LL | Zero::ZERO ..= Zero::ZERO => {},
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
15-
|
16-
= note: type must be known at this point
1715

1816
error: aborting due to 2 previous errors
1917

src/test/ui/pattern/pat-tuple-bad-type.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ error[E0282]: type annotations needed
33
|
44
LL | let x;
55
| ^
6+
...
7+
LL | (..) => {}
8+
| ---- type must be known at this point
69
|
7-
= note: type must be known at this point
810
help: consider giving `x` an explicit type
911
|
1012
LL | let x: _;

src/test/ui/span/issue-42234-unknown-receiver-type.full.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ error[E0282]: type annotations needed
33
|
44
LL | let x: Option<_> = None;
55
| ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
6+
LL | x.unwrap().method_that_could_exist_on_some_type();
7+
| ---------- type must be known at this point
68
|
7-
= note: type must be known at this point
89
help: consider specifying the generic argument
910
|
1011
LL | let x: Option<_> = None::<T>;
@@ -16,7 +17,6 @@ error[E0282]: type annotations needed
1617
LL | .sum::<_>()
1718
| ^^^ cannot infer type of the type parameter `S` declared on the associated function `sum`
1819
|
19-
= note: type must be known at this point
2020
help: consider specifying the generic argument
2121
|
2222
LL | .sum::<_>()

src/test/ui/span/issue-42234-unknown-receiver-type.generic_arg.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ error[E0282]: type annotations needed
33
|
44
LL | let x: Option<_> = None;
55
| ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
6+
LL | x.unwrap().method_that_could_exist_on_some_type();
7+
| ---------- type must be known at this point
68
|
7-
= note: type must be known at this point
89
help: consider specifying the generic argument
910
|
1011
LL | let x: Option<_> = None::<T>;
@@ -16,7 +17,6 @@ error[E0282]: type annotations needed
1617
LL | .sum::<_>()
1718
| ^^^ cannot infer type of the type parameter `S` declared on the associated function `sum`
1819
|
19-
= note: type must be known at this point
2020
help: consider specifying the generic argument
2121
|
2222
LL | .sum::<S>()

src/test/ui/span/method-and-field-eager-resolution.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ error[E0282]: type annotations needed
33
|
44
LL | let mut x = Default::default();
55
| ^^^^^
6+
LL |
7+
LL | x.0;
8+
| - type must be known at this point
69
|
7-
= note: type must be known at this point
810
help: consider giving `x` an explicit type
911
|
1012
LL | let mut x: _ = Default::default();
@@ -15,8 +17,10 @@ error[E0282]: type annotations needed
1517
|
1618
LL | let mut x = Default::default();
1719
| ^^^^^
20+
LL |
21+
LL | x[0];
22+
| - type must be known at this point
1823
|
19-
= note: type must be known at this point
2024
help: consider giving `x` an explicit type
2125
|
2226
LL | let mut x: _ = Default::default();

src/test/ui/span/type-annotations-needed-expr.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error[E0282]: type annotations needed
44
LL | let _ = (vec![1,2,3]).into_iter().sum() as f64;
55
| ^^^ cannot infer type of the type parameter `S` declared on the associated function `sum`
66
|
7-
= note: type must be known at this point
87
help: consider specifying the generic argument
98
|
109
LL | let _ = (vec![1,2,3]).into_iter().sum::<S>() as f64;

src/test/ui/type-alias-impl-trait/closures_in_branches.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ error[E0282]: type annotations needed
1414
--> $DIR/closures_in_branches.rs:21:10
1515
|
1616
LL | |x| x.len()
17-
| ^
17+
| ^ - type must be known at this point
1818
|
19-
= note: type must be known at this point
2019
help: consider giving this closure parameter an explicit type
2120
|
2221
LL | |x: _| x.len()

src/test/ui/typeck/issue-65611.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0282]: type annotations needed
33
|
44
LL | let x = buffer.last().unwrap().0.clone();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
6-
|
7-
= note: type must be known at this point
86

97
error[E0609]: no field `0` on type `&_`
108
--> $DIR/issue-65611.rs:59:36

src/test/ui/unboxed-closures/unboxed-closures-failed-recursive-fn-2.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ error[E0282]: type annotations needed for `Option<T>`
33
|
44
LL | let mut closure0 = None;
55
| ^^^^^^^^^^^^
6+
...
7+
LL | return c();
8+
| --- type must be known at this point
69
|
7-
= note: type must be known at this point
810
help: consider giving `closure0` an explicit type, where the placeholders `_` are specified
911
|
1012
LL | let mut closure0: Option<T> = None;

0 commit comments

Comments
 (0)