From bdb88c9d4abe3b1d2b060cb2c7aa46663c7307f1 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 16 Dec 2024 14:59:05 +0000 Subject: [PATCH 1/5] Avoid creating a fn sig type just to unwrap it again to get at its signature --- .../trait_impl_difference.rs | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs index 592ade8ede204..f73a556fef425 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs @@ -9,7 +9,7 @@ use rustc_middle::hir::nested_filter; use rustc_middle::traits::ObligationCauseCode; use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::print::RegionHighlightMode; -use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor}; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable}; use rustc_span::Span; use tracing::debug; @@ -39,12 +39,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { { // FIXME(compiler-errors): Don't like that this needs `Ty`s, but // all of the region highlighting machinery only deals with those. - let guar = self.emit_err( - var_origin.span(), - Ty::new_fn_ptr(self.cx.tcx, expected), - Ty::new_fn_ptr(self.cx.tcx, found), - *trait_item_def_id, - ); + let guar = self.emit_err(var_origin.span(), expected, found, trait_item_def_id); return Some(guar); } None @@ -53,8 +48,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { fn emit_err( &self, sp: Span, - expected: Ty<'tcx>, - found: Ty<'tcx>, + expected: ty::PolyFnSig<'tcx>, + found: ty::PolyFnSig<'tcx>, trait_def_id: DefId, ) -> ErrorGuaranteed { let trait_sp = self.tcx().def_span(trait_def_id); @@ -67,10 +62,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { } impl<'tcx> HighlightBuilder<'tcx> { - fn build(ty: Ty<'tcx>) -> RegionHighlightMode<'tcx> { + fn build(sig: ty::PolyFnSig<'tcx>) -> RegionHighlightMode<'tcx> { let mut builder = HighlightBuilder { highlight: RegionHighlightMode::default(), counter: 1 }; - builder.visit_ty(ty); + sig.visit_with(&mut builder); builder.highlight } } @@ -85,13 +80,22 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { } let expected_highlight = HighlightBuilder::build(expected); + let tcx = self.cx.tcx; let expected = self .cx - .extract_inference_diagnostics_data(expected.into(), Some(expected_highlight)) + .extract_inference_diagnostics_data( + Ty::new_fn_ptr(tcx, expected).into(), + Some(expected_highlight), + ) .name; let found_highlight = HighlightBuilder::build(found); - let found = - self.cx.extract_inference_diagnostics_data(found.into(), Some(found_highlight)).name; + let found = self + .cx + .extract_inference_diagnostics_data( + Ty::new_fn_ptr(tcx, found).into(), + Some(found_highlight), + ) + .name; // Get the span of all the used type parameters in the method. let assoc_item = self.tcx().associated_item(trait_def_id); From f387b9d9092829c036684242f20b773549c90237 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 16 Dec 2024 15:12:47 +0000 Subject: [PATCH 2/5] Properly name a def id variable --- .../infer/nice_region_error/trait_impl_difference.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs index f73a556fef425..b55b75aa2c14f 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs @@ -32,7 +32,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { _, ) = error.clone() && let (Subtype(sup_trace), Subtype(sub_trace)) = (&sup_origin, &sub_origin) - && let ObligationCauseCode::CompareImplItem { trait_item_def_id, .. } = + && let &ObligationCauseCode::CompareImplItem { trait_item_def_id, .. } = sub_trace.cause.code() && sub_trace.values == sup_trace.values && let ValuePairs::PolySigs(ExpectedFound { expected, found }) = sub_trace.values @@ -50,9 +50,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { sp: Span, expected: ty::PolyFnSig<'tcx>, found: ty::PolyFnSig<'tcx>, - trait_def_id: DefId, + trait_item_def_id: DefId, ) -> ErrorGuaranteed { - let trait_sp = self.tcx().def_span(trait_def_id); + let trait_sp = self.tcx().def_span(trait_item_def_id); // Mark all unnamed regions in the type with a number. // This diagnostic is called in response to lifetime errors, so be informative. @@ -98,7 +98,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { .name; // Get the span of all the used type parameters in the method. - let assoc_item = self.tcx().associated_item(trait_def_id); + let assoc_item = self.tcx().associated_item(trait_item_def_id); let mut visitor = TypeParamSpanVisitor { tcx: self.tcx(), types: vec![] }; match assoc_item.kind { ty::AssocKind::Fn => { From 4032b9ddbd9f386cc55c3d76000461a1dbb76cd9 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 16 Dec 2024 15:30:03 +0000 Subject: [PATCH 3/5] Avoid wrapping a trivially defaultable type in `Option` --- .../src/diagnostics/region_name.rs | 9 +++------ .../error_reporting/infer/need_type_info.rs | 20 +++++++++---------- .../trait_impl_difference.rs | 7 ++----- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index d49dee8514457..5dfc2658d2aaf 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -459,11 +459,8 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> { ) -> RegionNameHighlight { let mut highlight = RegionHighlightMode::default(); highlight.highlighting_region_vid(self.infcx.tcx, needle_fr, counter); - let type_name = self - .infcx - .err_ctxt() - .extract_inference_diagnostics_data(ty.into(), Some(highlight)) - .name; + let type_name = + self.infcx.err_ctxt().extract_inference_diagnostics_data(ty.into(), highlight).name; debug!( "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}", @@ -874,7 +871,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> { let type_name = self .infcx .err_ctxt() - .extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)) + .extract_inference_diagnostics_data(yield_ty.into(), highlight) .name; let yield_span = match tcx.hir_node(self.mir_hir_id()) { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs index af3b5e0d5d417..c99b120478827 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs @@ -279,7 +279,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { pub fn extract_inference_diagnostics_data( &self, arg: GenericArg<'tcx>, - highlight: Option>, + highlight: ty::print::RegionHighlightMode<'tcx>, ) -> InferenceDiagnosticsData { match arg.unpack() { GenericArgKind::Type(ty) => { @@ -301,9 +301,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS); - if let Some(highlight) = highlight { - printer.region_highlight_mode = highlight; - } + printer.region_highlight_mode = highlight; + ty.print(&mut printer).unwrap(); InferenceDiagnosticsData { name: printer.into_buffer(), @@ -326,9 +325,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { debug_assert!(!origin.span.is_dummy()); let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS); - if let Some(highlight) = highlight { - printer.region_highlight_mode = highlight; - } + printer.region_highlight_mode = highlight; + ct.print(&mut printer).unwrap(); InferenceDiagnosticsData { name: printer.into_buffer(), @@ -344,9 +342,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // to figure out which inference var is actually unresolved so that // this path is unreachable. let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS); - if let Some(highlight) = highlight { - printer.region_highlight_mode = highlight; - } + printer.region_highlight_mode = highlight; + ct.print(&mut printer).unwrap(); InferenceDiagnosticsData { name: printer.into_buffer(), @@ -422,7 +419,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { should_label_span: bool, ) -> Diag<'a> { 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, ty::print::RegionHighlightMode::default()); let Some(typeck_results) = &self.typeck_results else { // If we don't have any typeck results we're outside diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs index b55b75aa2c14f..26334d53f4314 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs @@ -85,16 +85,13 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { .cx .extract_inference_diagnostics_data( Ty::new_fn_ptr(tcx, expected).into(), - Some(expected_highlight), + expected_highlight, ) .name; let found_highlight = HighlightBuilder::build(found); let found = self .cx - .extract_inference_diagnostics_data( - Ty::new_fn_ptr(tcx, found).into(), - Some(found_highlight), - ) + .extract_inference_diagnostics_data(Ty::new_fn_ptr(tcx, found).into(), found_highlight) .name; // Get the span of all the used type parameters in the method. From 1c7d54eb7bb79ebe8223ee4d292c6e091e4ac8c0 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 16 Dec 2024 15:17:23 +0000 Subject: [PATCH 4/5] Deduplicate some pretty printing logic --- .../error_reporting/infer/need_type_info.rs | 23 +++++++------------ .../nice_region_error/placeholder_error.rs | 12 ++++++---- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs index c99b120478827..677b72477a279 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs @@ -21,6 +21,7 @@ use rustc_span::symbol::{Ident, sym}; use rustc_span::{BytePos, DUMMY_SP, FileName, Span}; use tracing::{debug, instrument, warn}; +use super::nice_region_error::placeholder_error::Highlighted; use crate::error_reporting::TypeErrCtxt; use crate::errors::{ AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError, @@ -281,6 +282,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { arg: GenericArg<'tcx>, highlight: ty::print::RegionHighlightMode<'tcx>, ) -> InferenceDiagnosticsData { + let tcx = self.tcx; match arg.unpack() { GenericArgKind::Type(ty) => { if let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind() { @@ -300,12 +302,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } } - let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS); - printer.region_highlight_mode = highlight; - - ty.print(&mut printer).unwrap(); InferenceDiagnosticsData { - name: printer.into_buffer(), + name: Highlighted { highlight, ns: Namespace::TypeNS, tcx, value: ty } + .to_string(), span: None, kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) }, parent: None, @@ -324,12 +323,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } debug_assert!(!origin.span.is_dummy()); - let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS); - printer.region_highlight_mode = highlight; - - ct.print(&mut printer).unwrap(); InferenceDiagnosticsData { - name: printer.into_buffer(), + name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct } + .to_string(), span: Some(origin.span), kind: UnderspecifiedArgKind::Const { is_parameter: false }, parent: None, @@ -341,12 +337,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // FIXME: Ideally we should look into the generic constant // to figure out which inference var is actually unresolved so that // this path is unreachable. - let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::ValueNS); - printer.region_highlight_mode = highlight; - - ct.print(&mut printer).unwrap(); InferenceDiagnosticsData { - name: printer.into_buffer(), + name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct } + .to_string(), span: None, kind: UnderspecifiedArgKind::Const { is_parameter: false }, parent: None, diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs index 4398af76ab2c7..aaaefd81d19bd 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/placeholder_error.rs @@ -21,9 +21,10 @@ use crate::traits::{ObligationCause, ObligationCauseCode}; // HACK(eddyb) maybe move this in a more central location. #[derive(Copy, Clone)] pub struct Highlighted<'tcx, T> { - tcx: TyCtxt<'tcx>, - highlight: RegionHighlightMode<'tcx>, - value: T, + pub tcx: TyCtxt<'tcx>, + pub highlight: RegionHighlightMode<'tcx>, + pub value: T, + pub ns: Namespace, } impl<'tcx, T> IntoDiagArg for Highlighted<'tcx, T> @@ -37,7 +38,7 @@ where impl<'tcx, T> Highlighted<'tcx, T> { fn map(self, f: impl FnOnce(T) -> U) -> Highlighted<'tcx, U> { - Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value) } + Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value), ns: self.ns } } } @@ -46,7 +47,7 @@ where T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS); + let mut printer = ty::print::FmtPrinter::new(self.tcx, self.ns); printer.region_highlight_mode = self.highlight; self.value.print(&mut printer)?; @@ -381,6 +382,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> { tcx: self.tcx(), highlight: RegionHighlightMode::default(), value: trait_ref, + ns: Namespace::TypeNS, }; let same_self_type = actual_trait_ref.self_ty() == expected_trait_ref.self_ty(); From 1d834c2257f0ff9a7d1dbf9212909b7335e3d0ce Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 16 Dec 2024 15:38:32 +0000 Subject: [PATCH 5/5] Avoid wrapping fn sig in a fn pointer when we want to just print the sig --- .../trait_impl_difference.rs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs index 26334d53f4314..95dd1b28a3994 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs @@ -2,18 +2,19 @@ use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; -use rustc_hir::def::Res; +use rustc_hir::def::{Namespace, Res}; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::Visitor; use rustc_middle::hir::nested_filter; use rustc_middle::traits::ObligationCauseCode; use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::print::RegionHighlightMode; -use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{self, TyCtxt, TypeVisitable}; use rustc_span::Span; use tracing::debug; use crate::error_reporting::infer::nice_region_error::NiceRegionError; +use crate::error_reporting::infer::nice_region_error::placeholder_error::Highlighted; use crate::errors::{ConsiderBorrowingParamHelp, RelationshipHelp, TraitImplDiff}; use crate::infer::{RegionResolutionError, Subtype, ValuePairs}; @@ -81,18 +82,17 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { let expected_highlight = HighlightBuilder::build(expected); let tcx = self.cx.tcx; - let expected = self - .cx - .extract_inference_diagnostics_data( - Ty::new_fn_ptr(tcx, expected).into(), - expected_highlight, - ) - .name; + let expected = Highlighted { + highlight: expected_highlight, + ns: Namespace::TypeNS, + tcx, + value: expected, + } + .to_string(); let found_highlight = HighlightBuilder::build(found); - let found = self - .cx - .extract_inference_diagnostics_data(Ty::new_fn_ptr(tcx, found).into(), found_highlight) - .name; + let found = + Highlighted { highlight: found_highlight, ns: Namespace::TypeNS, tcx, value: found } + .to_string(); // Get the span of all the used type parameters in the method. let assoc_item = self.tcx().associated_item(trait_item_def_id);