From 62a39ed526e2d0a4d191903d4a9efc69c587335a Mon Sep 17 00:00:00 2001 From: William Bain Date: Tue, 29 Dec 2020 17:34:23 -0500 Subject: [PATCH] Extract parent def handling for infer failure err --- .../infer/error_reporting/need_type_info.rs | 55 +++++++------------ 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index e2b04730b1f..159af485ba4 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -10,7 +10,7 @@ use rustc_middle::hir::map::Map; use rustc_middle::infer::unify_key::ConstVariableOriginKind; use rustc_middle::ty::print::Print; use rustc_middle::ty::subst::{GenericArg, GenericArgKind}; -use rustc_middle::ty::{self, DefIdTree, InferConst, Ty}; +use rustc_middle::ty::{self, DefIdTree, InferConst, Ty, TyCtxt}; use rustc_span::source_map::DesugaringKind; use rustc_span::symbol::kw; use rustc_span::Span; @@ -286,6 +286,7 @@ pub struct InferenceDiagnosticsData { pub parent: Option, } +/// Data on the parent definition where a generic argument was declared. pub struct InferenceDiagnosticsParentData { pub prefix: &'static str, pub name: String, @@ -320,6 +321,20 @@ impl InferenceDiagnosticsData { } } +impl InferenceDiagnosticsParentData { + fn for_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option { + let parent_def_id = tcx.parent(def_id)?; + + let parent_name = + tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string(); + + Some(InferenceDiagnosticsParentData { + prefix: tcx.def_kind(parent_def_id).descr(parent_def_id), + name: parent_name, + }) + } +} + impl UnderspecifiedArgKind { fn prefix_string(&self) -> Cow<'static, str> { match self { @@ -347,23 +362,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { if let TypeVariableOriginKind::TypeParameterDefinition(name, def_id) = var_origin.kind { - let parent_data = def_id - .and_then(|def_id| self.tcx.parent(def_id)) - .and_then(|parent_def_id| { - let parent_name = self - .tcx - .def_key(parent_def_id) - .disambiguated_data - .data - .get_opt_name()? - .to_string(); - - Some(InferenceDiagnosticsParentData { - prefix: self.tcx.def_kind(parent_def_id).descr(parent_def_id), - name: parent_name, - }) - }); - if name != kw::SelfUpper { return InferenceDiagnosticsData { name: name.to_string(), @@ -371,7 +369,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { kind: UnderspecifiedArgKind::Type { prefix: "type parameter".into(), }, - parent: parent_data, + parent: def_id.and_then(|def_id| { + InferenceDiagnosticsParentData::for_def_id(self.tcx, def_id) + }), }; } } @@ -397,26 +397,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) = origin.kind { - let parent_data = self.tcx.parent(def_id).and_then(|parent_def_id| { - let parent_name = self - .tcx - .def_key(parent_def_id) - .disambiguated_data - .data - .get_opt_name()? - .to_string(); - - Some(InferenceDiagnosticsParentData { - prefix: self.tcx.def_kind(parent_def_id).descr(parent_def_id), - name: parent_name, - }) - }); - return InferenceDiagnosticsData { name: name.to_string(), span: Some(origin.span), kind: UnderspecifiedArgKind::Const { is_parameter: true }, - parent: parent_data, + parent: InferenceDiagnosticsParentData::for_def_id(self.tcx, def_id), }; }