Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

0 commit comments

Comments
 (0)