Skip to content

Commit b0935b1

Browse files
authoredJul 1, 2022
Rollup merge of #98497 - compiler-errors:span-inference-note, r=lcnr
Improve some inference diagnostics - Properly point out point location where "type must be known at this point", or else omit the note if it's not associated with a useful span. - Fix up some type ambiguity diagnostics, errors shouldn't say "cannot infer type for reference `&'a ()`" when the given type has no inference variables.
2 parents bda659e + 6711313 commit b0935b1

File tree

52 files changed

+195
-135
lines changed

Some content is hidden

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

52 files changed

+195
-135
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_middle/src/ty/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,17 @@ impl<'tcx> Term<'tcx> {
914914
pub fn ty(&self) -> Option<Ty<'tcx>> {
915915
if let Term::Ty(ty) = self { Some(*ty) } else { None }
916916
}
917+
917918
pub fn ct(&self) -> Option<Const<'tcx>> {
918919
if let Term::Const(c) = self { Some(*c) } else { None }
919920
}
921+
922+
pub fn into_arg(self) -> GenericArg<'tcx> {
923+
match self {
924+
Term::Ty(ty) => ty.into(),
925+
Term::Const(c) => c.into(),
926+
}
927+
}
920928
}
921929

922930
/// This kind of predicate has no *direct* correspondent in the

0 commit comments

Comments
 (0)
Please sign in to comment.