Skip to content

Some trait method vs impl method signature difference diagnostic cleanups #134386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
@@ -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()) {
Original file line number Diff line number Diff line change
@@ -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,
@@ -279,8 +280,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
pub fn extract_inference_diagnostics_data(
&self,
arg: GenericArg<'tcx>,
highlight: Option<ty::print::RegionHighlightMode<'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,13 +302,9 @@ 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;
}
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,
@@ -325,13 +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);
if let Some(highlight) = highlight {
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,
@@ -343,13 +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);
if let Some(highlight) = highlight {
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,
@@ -422,7 +412,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
Original file line number Diff line number Diff line change
@@ -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<U>(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();
Original file line number Diff line number Diff line change
@@ -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, TypeVisitor};
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};

@@ -32,19 +33,14 @@ 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
{
// 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,11 +49,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
fn emit_err(
&self,
sp: Span,
expected: Ty<'tcx>,
found: Ty<'tcx>,
trait_def_id: DefId,
expected: ty::PolyFnSig<'tcx>,
found: ty::PolyFnSig<'tcx>,
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.
@@ -67,10 +63,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,16 +81,21 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
}

let expected_highlight = HighlightBuilder::build(expected);
let expected = self
.cx
.extract_inference_diagnostics_data(expected.into(), Some(expected_highlight))
.name;
let tcx = self.cx.tcx;
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(found.into(), Some(found_highlight)).name;
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_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 => {