Only label place where type is needed if span is meaningful

This commit is contained in:
Michael Goulet 2022-06-25 09:18:25 -07:00
parent 5b9775fe17
commit f44ae98cee
22 changed files with 51 additions and 40 deletions

View File

@ -313,11 +313,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
pub fn emit_inference_failure_err(
&self,
body_id: Option<hir::BodyId>,
span: Span,
failure_span: Span,
arg: GenericArg<'tcx>,
// FIXME(#94483): Either use this or remove it.
_impl_candidates: Vec<ty::TraitRef<'tcx>>,
error_code: TypeAnnotationNeeded,
should_label_span: bool,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
let arg = self.resolve_vars_if_possible(arg);
let arg_data = self.extract_inference_diagnostics_data(arg, None);
@ -326,7 +327,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// If we don't have any typeck results we're outside
// of a body, so we won't be able to get better info
// here.
return self.bad_inference_failure_err(span, arg_data, error_code);
return self.bad_inference_failure_err(failure_span, arg_data, error_code);
};
let typeck_results = typeck_results.borrow();
let typeck_results = &typeck_results;
@ -338,7 +339,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
let Some(InferSource { span, kind }) = local_visitor.infer_source else {
return self.bad_inference_failure_err(span, arg_data, error_code)
return self.bad_inference_failure_err(failure_span, arg_data, error_code)
};
let error_code = error_code.into();
@ -347,6 +348,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
&format!("type annotations needed{}", kind.ty_msg(self)),
error_code,
);
if should_label_span && !failure_span.overlaps(span) {
err.span_label(failure_span, "type must be known at this point");
}
match kind {
InferSourceKind::LetBinding { insert_span, pattern_name, ty } => {
let suggestion_msg = if let Some(name) = pattern_name {

View File

@ -2002,6 +2002,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
subst,
vec![],
ErrorCode::E0282,
false,
)
.emit();
}
@ -2019,6 +2020,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
subst,
impl_candidates,
ErrorCode::E0283,
false,
);
let obligation = Obligation::new(
@ -2110,7 +2112,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
return;
}
self.emit_inference_failure_err(body_id, span, arg, vec![], ErrorCode::E0282)
self.emit_inference_failure_err(body_id, span, arg, vec![], ErrorCode::E0282, false)
}
ty::PredicateKind::Subtype(data) => {
@ -2124,7 +2126,14 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
let SubtypePredicate { a_is_expected: _, a, b } = data;
// both must be type variables, or the other would've been instantiated
assert!(a.is_ty_var() && b.is_ty_var());
self.emit_inference_failure_err(body_id, span, a.into(), vec![], ErrorCode::E0282)
self.emit_inference_failure_err(
body_id,
span,
a.into(),
vec![],
ErrorCode::E0282,
false,
)
}
ty::PredicateKind::Projection(data) => {
let self_ty = data.projection_ty.self_ty();
@ -2140,6 +2149,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
self_ty.into(),
vec![],
ErrorCode::E0284,
false,
);
err.note(&format!("cannot satisfy `{}`", predicate));
err

View File

@ -1538,9 +1538,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty
} else {
if !self.is_tainted_by_errors() {
self.emit_inference_failure_err((**self).body_id, sp, ty.into(), vec![], E0282)
.note("type must be known at this point")
.emit();
self.emit_inference_failure_err(
(**self).body_id,
sp,
ty.into(),
vec![],
E0282,
true,
)
.emit();
}
let err = self.tcx.ty_error();
self.demand_suptype(sp, err, ty);

View File

@ -694,6 +694,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
t.into(),
vec![],
E0282,
false,
)
.emit();
}
@ -708,6 +709,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
c.into(),
vec![],
E0282,
false,
)
.emit();
}

View File

@ -4,7 +4,6 @@ error[E0282]: type annotations needed
LL | let [_, _] = a.into();
| ^^^^^^
|
= note: type must be known at this point
help: consider giving this pattern a type
|
LL | let [_, _]: _ = a.into();

View File

@ -27,8 +27,6 @@ error[E0282]: type annotations needed
|
LL | [] => {}
| ^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to 5 previous errors

View File

@ -3,8 +3,6 @@ error[E0282]: type annotations needed
|
LL | let b = (a + 1) as usize;
| ^^^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to previous error

View File

@ -3,16 +3,12 @@ error[E0282]: type annotations needed
|
LL | cont.reify_as();
| ^^^^ cannot infer type
|
= note: type must be known at this point
error[E0282]: type annotations needed
--> $DIR/hidden-type-is-opaque-2.rs:18:9
|
LL | cont.reify_as();
| ^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to 2 previous errors

View File

@ -5,8 +5,6 @@ LL | / { return () }
LL | |
LL | | ()
| |______^ cannot infer type
|
= note: type must be known at this point
error: aborting due to previous error

View File

@ -3,8 +3,6 @@ error[E0282]: type annotations needed
|
LL | i.clone();
| ^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to previous error

View File

@ -3,8 +3,9 @@ error[E0282]: type annotations needed
|
LL | let x = panic!();
| ^
LL | x.clone();
| - type must be known at this point
|
= note: type must be known at this point
help: consider giving `x` an explicit type
|
LL | let x: _ = panic!();

View File

@ -5,7 +5,6 @@ fn main() {
*tile = 0;
//~^ ERROR type annotations needed
//~| NOTE cannot infer type
//~| NOTE type must be known at this point
}
}

View File

@ -3,8 +3,6 @@ error[E0282]: type annotations needed
|
LL | *tile = 0;
| ^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to previous error

View File

@ -12,8 +12,6 @@ error[E0282]: type annotations needed
|
LL | Zero::ZERO ..= Zero::ZERO => {},
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to 2 previous errors

View File

@ -3,8 +3,10 @@ error[E0282]: type annotations needed
|
LL | let x;
| ^
...
LL | (..) => {}
| ---- type must be known at this point
|
= note: type must be known at this point
help: consider giving `x` an explicit type
|
LL | let x: _;

View File

@ -3,8 +3,9 @@ error[E0282]: type annotations needed
|
LL | let x: Option<_> = None;
| ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
LL | x.unwrap().method_that_could_exist_on_some_type();
| ---------- type must be known at this point
|
= note: type must be known at this point
help: consider specifying the generic argument
|
LL | let x: Option<_> = None::<T>;
@ -16,7 +17,6 @@ error[E0282]: type annotations needed
LL | .sum::<_>()
| ^^^ cannot infer type of the type parameter `S` declared on the associated function `sum`
|
= note: type must be known at this point
help: consider specifying the generic argument
|
LL | .sum::<_>()

View File

@ -3,8 +3,9 @@ error[E0282]: type annotations needed
|
LL | let x: Option<_> = None;
| ^^^^ cannot infer type of the type parameter `T` declared on the enum `Option`
LL | x.unwrap().method_that_could_exist_on_some_type();
| ---------- type must be known at this point
|
= note: type must be known at this point
help: consider specifying the generic argument
|
LL | let x: Option<_> = None::<T>;
@ -16,7 +17,6 @@ error[E0282]: type annotations needed
LL | .sum::<_>()
| ^^^ cannot infer type of the type parameter `S` declared on the associated function `sum`
|
= note: type must be known at this point
help: consider specifying the generic argument
|
LL | .sum::<S>()

View File

@ -3,8 +3,10 @@ error[E0282]: type annotations needed
|
LL | let mut x = Default::default();
| ^^^^^
LL |
LL | x.0;
| - type must be known at this point
|
= note: type must be known at this point
help: consider giving `x` an explicit type
|
LL | let mut x: _ = Default::default();
@ -15,8 +17,10 @@ error[E0282]: type annotations needed
|
LL | let mut x = Default::default();
| ^^^^^
LL |
LL | x[0];
| - type must be known at this point
|
= note: type must be known at this point
help: consider giving `x` an explicit type
|
LL | let mut x: _ = Default::default();

View File

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

View File

@ -14,9 +14,8 @@ error[E0282]: type annotations needed
--> $DIR/closures_in_branches.rs:21:10
|
LL | |x| x.len()
| ^
| ^ - type must be known at this point
|
= note: type must be known at this point
help: consider giving this closure parameter an explicit type
|
LL | |x: _| x.len()

View File

@ -3,8 +3,6 @@ error[E0282]: type annotations needed
|
LL | let x = buffer.last().unwrap().0.clone();
| ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
|
= note: type must be known at this point
error[E0609]: no field `0` on type `&_`
--> $DIR/issue-65611.rs:59:36

View File

@ -3,8 +3,10 @@ error[E0282]: type annotations needed for `Option<T>`
|
LL | let mut closure0 = None;
| ^^^^^^^^^^^^
...
LL | return c();
| --- type must be known at this point
|
= note: type must be known at this point
help: consider giving `closure0` an explicit type, where the placeholders `_` are specified
|
LL | let mut closure0: Option<T> = None;