Skip to content

Commit db3ac4e

Browse files
committed
Fix errors in need_type_info
1 parent 34434eb commit db3ac4e

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::infer::type_variable::TypeVariableOriginKind;
1+
use crate::infer::{InferCtxtInner, type_variable::TypeVariableOriginKind};
22
use crate::infer::InferCtxt;
33
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
44
use rustc_hir as hir;
@@ -16,8 +16,8 @@ use rustc_span::symbol::kw;
1616
use rustc_span::Span;
1717
use std::borrow::Cow;
1818

19-
struct FindHirNodeVisitor<'a, 'tcx> {
20-
infcx: &'a InferCtxt<'a, 'tcx>,
19+
struct FindHirNodeVisitor<'a, 'cx, 'tcx> {
20+
infcx: &'a mut InferCtxt<'cx, 'tcx>,
2121
target: GenericArg<'tcx>,
2222
target_span: Span,
2323
found_node_ty: Option<Ty<'tcx>>,
@@ -29,8 +29,8 @@ struct FindHirNodeVisitor<'a, 'tcx> {
2929
found_use_diagnostic: Option<UseDiagnostic<'tcx>>,
3030
}
3131

32-
impl<'a, 'tcx> FindHirNodeVisitor<'a, 'tcx> {
33-
fn new(infcx: &'a InferCtxt<'a, 'tcx>, target: GenericArg<'tcx>, target_span: Span) -> Self {
32+
impl<'a, 'cx, 'tcx> FindHirNodeVisitor<'a, 'cx, 'tcx> {
33+
fn new(infcx: &'a mut InferCtxt<'cx, 'tcx>, target: GenericArg<'tcx>, target_span: Span) -> Self {
3434
Self {
3535
infcx,
3636
target,
@@ -49,7 +49,7 @@ impl<'a, 'tcx> FindHirNodeVisitor<'a, 'tcx> {
4949
self.infcx.in_progress_typeck_results?.borrow().node_type_opt(hir_id)
5050
}
5151

52-
fn node_ty_contains_target(&self, hir_id: HirId) -> Option<Ty<'tcx>> {
52+
fn node_ty_contains_target(&mut self, hir_id: HirId) -> Option<Ty<'tcx>> {
5353
self.node_type_opt(hir_id).map(|ty| self.infcx.resolve_vars_if_possible(ty)).filter(|ty| {
5454
ty.walk().any(|inner| {
5555
inner == self.target
@@ -80,7 +80,7 @@ impl<'a, 'tcx> FindHirNodeVisitor<'a, 'tcx> {
8080
}
8181
}
8282

83-
impl<'a, 'tcx> Visitor<'tcx> for FindHirNodeVisitor<'a, 'tcx> {
83+
impl<'a, 'tcx> Visitor<'tcx> for FindHirNodeVisitor<'a, '_, 'tcx> {
8484
type Map = Map<'tcx>;
8585

8686
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
@@ -345,15 +345,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
345345
/// Extracts data used by diagnostic for either types or constants
346346
/// which were stuck during inference.
347347
pub fn extract_inference_diagnostics_data(
348-
&self,
348+
&mut self,
349349
arg: GenericArg<'tcx>,
350350
highlight: Option<ty::print::RegionHighlightMode>,
351351
) -> InferenceDiagnosticsData {
352352
match arg.unpack() {
353353
GenericArgKind::Type(ty) => {
354354
if let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind() {
355-
let mut inner = self.inner;
356-
let ty_vars = &inner.type_variables();
355+
let ty_vars = &self.inner.type_variables();
357356
let var_origin = ty_vars.var_origin(ty_vid);
358357
if let TypeVariableOriginKind::TypeParameterDefinition(name, def_id) =
359358
var_origin.kind
@@ -424,7 +423,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
424423
}
425424

426425
pub fn emit_inference_failure_err(
427-
&self,
426+
&mut self,
428427
body_id: Option<hir::BodyId>,
429428
span: Span,
430429
arg: GenericArg<'tcx>,
@@ -434,11 +433,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
434433
let arg = self.resolve_vars_if_possible(arg);
435434
let arg_data = self.extract_inference_diagnostics_data(arg, None);
436435

437-
let mut local_visitor = FindHirNodeVisitor::new(&self, arg, span);
438-
let ty_to_string = |ty: Ty<'tcx>| -> String {
436+
let tcx = self.tcx;
437+
let mut local_visitor = FindHirNodeVisitor::new(self, arg, span);
438+
let ty_to_string = |ty: Ty<'tcx>, inner: &mut InferCtxtInner<'_>| -> String {
439439
let mut s = String::new();
440-
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
441-
let mut inner = self.inner;
440+
let mut printer = ty::print::FmtPrinter::new(tcx, &mut s, Namespace::TypeNS);
442441
let ty_vars = inner.type_variables();
443442
let getter = move |ty_vid| {
444443
let var_origin = ty_vars.var_origin(ty_vid);
@@ -451,15 +450,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
451450
let _ = if let ty::FnDef(..) = ty.kind() {
452451
// We don't want the regular output for `fn`s because it includes its path in
453452
// invalid pseudo-syntax, we want the `fn`-pointer output instead.
454-
ty.fn_sig(self.tcx).print(printer)
453+
ty.fn_sig(tcx).print(printer)
455454
} else {
456455
ty.print(printer)
457456
};
458457
s
459458
};
460459

461460
if let Some(body_id) = body_id {
462-
let expr = self.tcx.hir().expect_expr(body_id.hir_id);
461+
let expr = tcx.hir().expect_expr(body_id.hir_id);
463462
local_visitor.visit_expr(expr);
464463
}
465464
let err_span = if let Some(pattern) = local_visitor.found_arg_pattern {
@@ -491,7 +490,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
491490
let is_named_and_not_impl_trait = |ty: Ty<'_>| {
492491
&ty.to_string() != "_" &&
493492
// FIXME: Remove this check after `impl_trait_in_bindings` is stabilized. #63527
494-
(!ty.is_impl_trait() || self.tcx.features().impl_trait_in_bindings)
493+
(!ty.is_impl_trait() || tcx.features().impl_trait_in_bindings)
495494
};
496495

497496
let ty_msg = match (local_visitor.found_node_ty, local_visitor.found_exact_method_call) {
@@ -505,7 +504,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
505504
format!(" for the closure `fn({}) -> {}`", args, ret)
506505
}
507506
(Some(ty), _) if is_named_and_not_impl_trait(ty) => {
508-
let ty = ty_to_string(ty);
507+
let ty = ty_to_string(ty, &mut local_visitor.infcx.inner);
509508
format!(" for `{}`", ty)
510509
}
511510
_ => String::new(),
@@ -525,7 +524,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
525524
// | the type parameter `E` is specified
526525
// ```
527526
let error_code = error_code.into();
528-
let mut err = self.tcx.sess.struct_span_err_with_code(
527+
let mut err = tcx.sess.struct_span_err_with_code(
529528
err_span,
530529
&format!("type annotations needed{}", ty_msg),
531530
error_code,
@@ -555,7 +554,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
555554
closure_return_type_suggestion(
556555
&mut err,
557556
&decl.output,
558-
self.tcx.hir().body(body_id),
557+
tcx.hir().body(body_id),
559558
&ret,
560559
);
561560
// We don't want to give the other suggestions when the problem is the
@@ -575,11 +574,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
575574
format!("a boxed closure type like `Box<dyn Fn({}) -> {}>`", args, ret)
576575
}
577576
Some(ty) if is_named_and_not_impl_trait(ty) && arg_data.name == "_" => {
578-
let ty = ty_to_string(ty);
577+
let ty = ty_to_string(ty, &mut local_visitor.infcx.inner);
579578
format!("the explicit type `{}`, with the type parameters specified", ty)
580579
}
581580
Some(ty) if is_named_and_not_impl_trait(ty) && ty.to_string() != arg_data.name => {
582-
let ty = ty_to_string(ty);
581+
let ty = ty_to_string(ty, &mut local_visitor.infcx.inner);
583582
format!(
584583
"the explicit type `{}`, where the type parameter `{}` is specified",
585584
ty, arg_data.name,
@@ -616,7 +615,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
616615
// | this method call resolves to `std::option::Option<&T>`
617616
// |
618617
// = note: type must be known at this point
619-
self.annotate_method_call(segment, e, &mut err);
618+
local_visitor.infcx.annotate_method_call(segment, e, &mut err);
620619
}
621620
} else if let Some(pattern) = local_visitor.found_arg_pattern {
622621
// We don't want to show the default label for closures.
@@ -717,7 +716,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
717716
// | this method call resolves to `std::option::Option<&T>`
718717
// |
719718
// = note: type must be known at this point
720-
self.annotate_method_call(segment, e, &mut err);
719+
local_visitor.infcx.annotate_method_call(segment, e, &mut err);
721720
}
722721
}
723722
// Instead of the following:
@@ -831,7 +830,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
831830
}
832831

833832
pub fn need_type_info_err_in_generator(
834-
&self,
833+
&mut self,
835834
kind: hir::GeneratorKind,
836835
span: Span,
837836
ty: Ty<'tcx>,

0 commit comments

Comments
 (0)