Skip to content

Commit 62f9962

Browse files
committed
Made ty_or_sig and trait_path use their actual types instead of String
1 parent 7ecd064 commit 62f9962

File tree

3 files changed

+118
-77
lines changed

3 files changed

+118
-77
lines changed

compiler/rustc_infer/src/errors/mod.rs

+69-32
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use hir::GenericParamKind;
22
use rustc_errors::{
33
fluent, AddToDiagnostic, Applicability, Diagnostic, DiagnosticMessage, DiagnosticStyledString,
4-
MultiSpan, SubdiagnosticMessage,
4+
IntoDiagnosticArg, MultiSpan, SubdiagnosticMessage,
55
};
66
use rustc_hir as hir;
77
use rustc_hir::FnRetTy;
88
use rustc_macros::{Diagnostic, Subdiagnostic};
9-
use rustc_middle::ty::{Region, Ty, TyCtxt};
9+
use rustc_middle::ty::print::TraitRefPrintOnlyTraitPath;
10+
use rustc_middle::ty::{Binder, FnSig, Region, Ty, TyCtxt};
1011
use rustc_span::symbol::kw;
1112
use rustc_span::Symbol;
1213
use rustc_span::{symbol::Ident, BytePos, Span};
1314

15+
use crate::infer::error_reporting::nice_region_error::placeholder_error::Highlighted;
1416
use crate::infer::error_reporting::{
1517
need_type_info::{GeneratorKindAsDiagArg, UnderspecifiedArgKind},
1618
ObligationCauseAsDiagArg,
@@ -557,91 +559,126 @@ pub enum ExplicitLifetimeRequired<'a> {
557559
},
558560
}
559561

562+
pub enum TyOrSig<'tcx> {
563+
Ty(Highlighted<'tcx, Ty<'tcx>>),
564+
ClosureSig(Highlighted<'tcx, Binder<'tcx, FnSig<'tcx>>>),
565+
}
566+
567+
impl IntoDiagnosticArg for TyOrSig<'_> {
568+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
569+
match self {
570+
TyOrSig::Ty(ty) => ty.into_diagnostic_arg(),
571+
TyOrSig::ClosureSig(sig) => sig.into_diagnostic_arg(),
572+
}
573+
}
574+
}
575+
560576
#[derive(Subdiagnostic)]
561-
pub enum ActualImplExplNotes {
577+
pub enum ActualImplExplNotes<'tcx> {
562578
#[note(infer_actual_impl_expl_expected_signature_two)]
563579
ExpectedSignatureTwo {
564580
leading_ellipsis: bool,
565-
ty_or_sig: String,
566-
trait_path: String,
581+
ty_or_sig: TyOrSig<'tcx>,
582+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
567583
lifetime_1: usize,
568584
lifetime_2: usize,
569585
},
570586
#[note(infer_actual_impl_expl_expected_signature_any)]
571587
ExpectedSignatureAny {
572588
leading_ellipsis: bool,
573-
ty_or_sig: String,
574-
trait_path: String,
589+
ty_or_sig: TyOrSig<'tcx>,
590+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
575591
lifetime_1: usize,
576592
},
577593
#[note(infer_actual_impl_expl_expected_signature_some)]
578594
ExpectedSignatureSome {
579595
leading_ellipsis: bool,
580-
ty_or_sig: String,
581-
trait_path: String,
596+
ty_or_sig: TyOrSig<'tcx>,
597+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
582598
lifetime_1: usize,
583599
},
584600
#[note(infer_actual_impl_expl_expected_signature_nothing)]
585-
ExpectedSignatureNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
601+
ExpectedSignatureNothing {
602+
leading_ellipsis: bool,
603+
ty_or_sig: TyOrSig<'tcx>,
604+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
605+
},
586606
#[note(infer_actual_impl_expl_expected_passive_two)]
587607
ExpectedPassiveTwo {
588608
leading_ellipsis: bool,
589-
ty_or_sig: String,
590-
trait_path: String,
609+
ty_or_sig: TyOrSig<'tcx>,
610+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
591611
lifetime_1: usize,
592612
lifetime_2: usize,
593613
},
594614
#[note(infer_actual_impl_expl_expected_passive_any)]
595615
ExpectedPassiveAny {
596616
leading_ellipsis: bool,
597-
ty_or_sig: String,
598-
trait_path: String,
617+
ty_or_sig: TyOrSig<'tcx>,
618+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
599619
lifetime_1: usize,
600620
},
601621
#[note(infer_actual_impl_expl_expected_passive_some)]
602622
ExpectedPassiveSome {
603623
leading_ellipsis: bool,
604-
ty_or_sig: String,
605-
trait_path: String,
624+
ty_or_sig: TyOrSig<'tcx>,
625+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
606626
lifetime_1: usize,
607627
},
608628
#[note(infer_actual_impl_expl_expected_passive_nothing)]
609-
ExpectedPassiveNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
629+
ExpectedPassiveNothing {
630+
leading_ellipsis: bool,
631+
ty_or_sig: TyOrSig<'tcx>,
632+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
633+
},
610634
#[note(infer_actual_impl_expl_expected_other_two)]
611635
ExpectedOtherTwo {
612636
leading_ellipsis: bool,
613-
ty_or_sig: String,
614-
trait_path: String,
637+
ty_or_sig: TyOrSig<'tcx>,
638+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
615639
lifetime_1: usize,
616640
lifetime_2: usize,
617641
},
618642
#[note(infer_actual_impl_expl_expected_other_any)]
619643
ExpectedOtherAny {
620644
leading_ellipsis: bool,
621-
ty_or_sig: String,
622-
trait_path: String,
645+
ty_or_sig: TyOrSig<'tcx>,
646+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
623647
lifetime_1: usize,
624648
},
625649
#[note(infer_actual_impl_expl_expected_other_some)]
626650
ExpectedOtherSome {
627651
leading_ellipsis: bool,
628-
ty_or_sig: String,
629-
trait_path: String,
652+
ty_or_sig: TyOrSig<'tcx>,
653+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
630654
lifetime_1: usize,
631655
},
632656
#[note(infer_actual_impl_expl_expected_other_nothing)]
633-
ExpectedOtherNothing { leading_ellipsis: bool, ty_or_sig: String, trait_path: String },
657+
ExpectedOtherNothing {
658+
leading_ellipsis: bool,
659+
ty_or_sig: TyOrSig<'tcx>,
660+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
661+
},
634662
#[note(infer_actual_impl_expl_but_actually_implements_trait)]
635-
ButActuallyImplementsTrait { trait_path: String, has_lifetime: bool, lifetime: usize },
663+
ButActuallyImplementsTrait {
664+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
665+
has_lifetime: bool,
666+
lifetime: usize,
667+
},
636668
#[note(infer_actual_impl_expl_but_actually_implemented_for_ty)]
637669
ButActuallyImplementedForTy {
638-
trait_path: String,
670+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
639671
has_lifetime: bool,
640672
lifetime: usize,
641673
ty: String,
642674
},
643675
#[note(infer_actual_impl_expl_but_actually_ty_implements)]
644-
ButActuallyTyImplements { trait_path: String, has_lifetime: bool, lifetime: usize, ty: String },
676+
ButActuallyTyImplements {
677+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
678+
has_lifetime: bool,
679+
lifetime: usize,
680+
ty: String,
681+
},
645682
}
646683

647684
pub enum ActualImplExpectedKind {
@@ -657,13 +694,13 @@ pub enum ActualImplExpectedLifetimeKind {
657694
Nothing,
658695
}
659696

660-
impl ActualImplExplNotes {
697+
impl<'tcx> ActualImplExplNotes<'tcx> {
661698
pub fn new_expected(
662699
kind: ActualImplExpectedKind,
663700
lt_kind: ActualImplExpectedLifetimeKind,
664701
leading_ellipsis: bool,
665-
ty_or_sig: String,
666-
trait_path: String,
702+
ty_or_sig: TyOrSig<'tcx>,
703+
trait_path: Highlighted<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>,
667704
lifetime_1: usize,
668705
lifetime_2: usize,
669706
) -> Self {
@@ -728,7 +765,7 @@ impl ActualImplExplNotes {
728765

729766
#[derive(Diagnostic)]
730767
#[diag(infer_trait_placeholder_mismatch)]
731-
pub struct TraitPlaceholderMismatch {
768+
pub struct TraitPlaceholderMismatch<'tcx> {
732769
#[primary_span]
733770
pub span: Span,
734771
#[label(label_satisfy)]
@@ -741,7 +778,7 @@ pub struct TraitPlaceholderMismatch {
741778
pub trait_def_id: String,
742779

743780
#[subdiagnostic(eager)]
744-
pub actual_impl_expl_notes: Vec<ActualImplExplNotes>,
781+
pub actual_impl_expl_notes: Vec<ActualImplExplNotes<'tcx>>,
745782
}
746783

747784
pub struct ConsiderBorrowingParamHelp {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod different_lifetimes;
99
pub mod find_anon_type;
1010
mod mismatched_static_lifetime;
1111
mod named_anon_conflict;
12-
mod placeholder_error;
12+
pub(crate) mod placeholder_error;
1313
mod placeholder_relation;
1414
mod static_impl_trait;
1515
mod trait_impl_difference;

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

+48-44
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::errors::{
22
ActualImplExpectedKind, ActualImplExpectedLifetimeKind, ActualImplExplNotes,
3-
TraitPlaceholderMismatch,
3+
TraitPlaceholderMismatch, TyOrSig,
44
};
55
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
66
use crate::infer::lexical_region_resolve::RegionResolutionError;
77
use crate::infer::ValuePairs;
88
use crate::infer::{SubregionOrigin, TypeTrace};
99
use crate::traits::{ObligationCause, ObligationCauseCode};
1010
use rustc_data_structures::intern::Interned;
11-
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
11+
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, IntoDiagnosticArg};
1212
use rustc_hir::def::Namespace;
1313
use rustc_hir::def_id::DefId;
1414
use rustc_middle::ty::error::ExpectedFound;
@@ -18,6 +18,42 @@ use rustc_middle::ty::{self, RePlaceholder, ReVar, Region, TyCtxt};
1818

1919
use std::fmt;
2020

21+
// HACK(eddyb) maybe move this in a more central location.
22+
#[derive(Copy, Clone)]
23+
pub struct Highlighted<'tcx, T> {
24+
tcx: TyCtxt<'tcx>,
25+
highlight: RegionHighlightMode<'tcx>,
26+
value: T,
27+
}
28+
29+
impl<'tcx, T> IntoDiagnosticArg for Highlighted<'tcx, T>
30+
where
31+
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error, Output = FmtPrinter<'a, 'tcx>>,
32+
{
33+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
34+
rustc_errors::DiagnosticArgValue::Str(self.to_string().into())
35+
}
36+
}
37+
38+
impl<'tcx, T> Highlighted<'tcx, T> {
39+
fn map<U>(self, f: impl FnOnce(T) -> U) -> Highlighted<'tcx, U> {
40+
Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value) }
41+
}
42+
}
43+
44+
impl<'tcx, T> fmt::Display for Highlighted<'tcx, T>
45+
where
46+
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>, Error = fmt::Error, Output = FmtPrinter<'a, 'tcx>>,
47+
{
48+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49+
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
50+
printer.region_highlight_mode = self.highlight;
51+
52+
let s = self.value.print(printer)?.into_buffer();
53+
f.write_str(&s)
54+
}
55+
}
56+
2157
impl<'tcx> NiceRegionError<'_, 'tcx> {
2258
/// When given a `ConcreteFailure` for a function with arguments containing a named region and
2359
/// an anonymous region, emit a descriptive diagnostic error.
@@ -328,39 +364,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
328364
actual_has_vid: Option<usize>,
329365
any_self_ty_has_vid: bool,
330366
leading_ellipsis: bool,
331-
) -> Vec<ActualImplExplNotes> {
332-
// HACK(eddyb) maybe move this in a more central location.
333-
#[derive(Copy, Clone)]
334-
struct Highlighted<'tcx, T> {
335-
tcx: TyCtxt<'tcx>,
336-
highlight: RegionHighlightMode<'tcx>,
337-
value: T,
338-
}
339-
340-
impl<'tcx, T> Highlighted<'tcx, T> {
341-
fn map<U>(self, f: impl FnOnce(T) -> U) -> Highlighted<'tcx, U> {
342-
Highlighted { tcx: self.tcx, highlight: self.highlight, value: f(self.value) }
343-
}
344-
}
345-
346-
impl<'tcx, T> fmt::Display for Highlighted<'tcx, T>
347-
where
348-
T: for<'a> Print<
349-
'tcx,
350-
FmtPrinter<'a, 'tcx>,
351-
Error = fmt::Error,
352-
Output = FmtPrinter<'a, 'tcx>,
353-
>,
354-
{
355-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356-
let mut printer = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS);
357-
printer.region_highlight_mode = self.highlight;
358-
359-
let s = self.value.print(printer)?.into_buffer();
360-
f.write_str(&s)
361-
}
362-
}
363-
367+
) -> Vec<ActualImplExplNotes<'tcx>> {
364368
// The weird thing here with the `maybe_highlighting_region` calls and the
365369
// the match inside is meant to be like this:
366370
//
@@ -418,27 +422,27 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
418422
});
419423
(
420424
ActualImplExpectedKind::Signature,
421-
closure_sig.to_string(),
422-
expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
425+
TyOrSig::ClosureSig(closure_sig),
426+
expected_trait_ref.map(|tr| tr.print_only_trait_path()),
423427
)
424428
} else {
425429
(
426430
ActualImplExpectedKind::Other,
427-
self_ty.to_string(),
428-
expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
431+
TyOrSig::Ty(self_ty),
432+
expected_trait_ref.map(|tr| tr.print_only_trait_path()),
429433
)
430434
}
431435
} else if passive_voice {
432436
(
433437
ActualImplExpectedKind::Passive,
434-
expected_trait_ref.map(|tr| tr.self_ty()).to_string(),
435-
expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
438+
TyOrSig::Ty(expected_trait_ref.map(|tr| tr.self_ty())),
439+
expected_trait_ref.map(|tr| tr.print_only_trait_path()),
436440
)
437441
} else {
438442
(
439443
ActualImplExpectedKind::Other,
440-
expected_trait_ref.map(|tr| tr.self_ty()).to_string(),
441-
expected_trait_ref.map(|tr| tr.print_only_trait_path()).to_string(),
444+
TyOrSig::Ty(expected_trait_ref.map(|tr| tr.self_ty())),
445+
expected_trait_ref.map(|tr| tr.print_only_trait_path()),
442446
)
443447
};
444448

@@ -474,7 +478,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
474478
None => true,
475479
};
476480

477-
let trait_path = actual_trait_ref.map(|tr| tr.print_only_trait_path()).to_string();
481+
let trait_path = actual_trait_ref.map(|tr| tr.print_only_trait_path());
478482
let ty = actual_trait_ref.map(|tr| tr.self_ty()).to_string();
479483
let has_lifetime = actual_has_vid.is_some();
480484
let lifetime = actual_has_vid.unwrap_or_default();

0 commit comments

Comments
 (0)