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( pub fn emit_inference_failure_err(
&self, &self,
body_id: Option<hir::BodyId>, body_id: Option<hir::BodyId>,
span: Span, failure_span: Span,
arg: GenericArg<'tcx>, arg: GenericArg<'tcx>,
// FIXME(#94483): Either use this or remove it. // FIXME(#94483): Either use this or remove it.
_impl_candidates: Vec<ty::TraitRef<'tcx>>, _impl_candidates: Vec<ty::TraitRef<'tcx>>,
error_code: TypeAnnotationNeeded, error_code: TypeAnnotationNeeded,
should_label_span: bool,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
let arg = self.resolve_vars_if_possible(arg); let arg = self.resolve_vars_if_possible(arg);
let arg_data = self.extract_inference_diagnostics_data(arg, None); 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 // If we don't have any typeck results we're outside
// of a body, so we won't be able to get better info // of a body, so we won't be able to get better info
// here. // 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.borrow();
let typeck_results = &typeck_results; 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 { 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(); let error_code = error_code.into();
@ -347,6 +348,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
&format!("type annotations needed{}", kind.ty_msg(self)), &format!("type annotations needed{}", kind.ty_msg(self)),
error_code, error_code,
); );
if should_label_span && !failure_span.overlaps(span) {
err.span_label(failure_span, "type must be known at this point");
}
match kind { match kind {
InferSourceKind::LetBinding { insert_span, pattern_name, ty } => { InferSourceKind::LetBinding { insert_span, pattern_name, ty } => {
let suggestion_msg = if let Some(name) = pattern_name { 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, subst,
vec![], vec![],
ErrorCode::E0282, ErrorCode::E0282,
false,
) )
.emit(); .emit();
} }
@ -2019,6 +2020,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
subst, subst,
impl_candidates, impl_candidates,
ErrorCode::E0283, ErrorCode::E0283,
false,
); );
let obligation = Obligation::new( let obligation = Obligation::new(
@ -2110,7 +2112,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
return; 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) => { 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; let SubtypePredicate { a_is_expected: _, a, b } = data;
// both must be type variables, or the other would've been instantiated // both must be type variables, or the other would've been instantiated
assert!(a.is_ty_var() && b.is_ty_var()); 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) => { ty::PredicateKind::Projection(data) => {
let self_ty = data.projection_ty.self_ty(); 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(), self_ty.into(),
vec![], vec![],
ErrorCode::E0284, ErrorCode::E0284,
false,
); );
err.note(&format!("cannot satisfy `{}`", predicate)); err.note(&format!("cannot satisfy `{}`", predicate));
err err

View File

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

View File

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

View File

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

View File

@ -27,8 +27,6 @@ error[E0282]: type annotations needed
| |
LL | [] => {} LL | [] => {}
| ^^ cannot infer type | ^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to 5 previous errors 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; LL | let b = (a + 1) as usize;
| ^^^^^^^ cannot infer type | ^^^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to previous error error: aborting due to previous error

View File

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

View File

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

View File

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

View File

@ -3,8 +3,9 @@ error[E0282]: type annotations needed
| |
LL | let x = panic!(); 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 help: consider giving `x` an explicit type
| |
LL | let x: _ = panic!(); LL | let x: _ = panic!();

View File

@ -5,7 +5,6 @@ fn main() {
*tile = 0; *tile = 0;
//~^ ERROR type annotations needed //~^ ERROR type annotations needed
//~| NOTE cannot infer type //~| 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; LL | *tile = 0;
| ^^^^^ cannot infer type | ^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to previous error error: aborting due to previous error

View File

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

View File

@ -3,8 +3,10 @@ error[E0282]: type annotations needed
| |
LL | let x; 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 help: consider giving `x` an explicit type
| |
LL | let x: _; LL | let x: _;

View File

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

View File

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

View File

@ -3,8 +3,10 @@ error[E0282]: type annotations needed
| |
LL | let mut x = Default::default(); 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 help: consider giving `x` an explicit type
| |
LL | let mut x: _ = Default::default(); LL | let mut x: _ = Default::default();
@ -15,8 +17,10 @@ error[E0282]: type annotations needed
| |
LL | let mut x = Default::default(); 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 help: consider giving `x` an explicit type
| |
LL | let mut x: _ = Default::default(); 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; 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` | ^^^ 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 help: consider specifying the generic argument
| |
LL | let _ = (vec![1,2,3]).into_iter().sum::<S>() as f64; 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 --> $DIR/closures_in_branches.rs:21:10
| |
LL | |x| x.len() 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 help: consider giving this closure parameter an explicit type
| |
LL | |x: _| x.len() LL | |x: _| x.len()

View File

@ -3,8 +3,6 @@ error[E0282]: type annotations needed
| |
LL | let x = buffer.last().unwrap().0.clone(); LL | let x = buffer.last().unwrap().0.clone();
| ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` | ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
|
= note: type must be known at this point
error[E0609]: no field `0` on type `&_` error[E0609]: no field `0` on type `&_`
--> $DIR/issue-65611.rs:59:36 --> $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 | 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 help: consider giving `closure0` an explicit type, where the placeholders `_` are specified
| |
LL | let mut closure0: Option<T> = None; LL | let mut closure0: Option<T> = None;