Rollup merge of #110563 - bryangarza:refactor-trait-selection-error-reporting, r=compiler-errors

Break up long function in trait selection error reporting + clean up nearby code

- Move blocks of code into their own functions
- Replace a few function argument types with their type aliases
- Create "AppendConstMessage" enum to replace a nested `Option`.
This commit is contained in:
Matthias Krüger 2023-04-25 06:46:48 +02:00 committed by GitHub
commit 5f33a8c026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 680 additions and 441 deletions

View File

@ -300,8 +300,7 @@ pub(super) fn obligation_for_method(
trait_def_id: DefId,
self_ty: Ty<'tcx>,
opt_input_types: Option<&[Ty<'tcx>]>,
) -> (traits::Obligation<'tcx, ty::Predicate<'tcx>>, &'tcx ty::List<ty::subst::GenericArg<'tcx>>)
{
) -> (traits::PredicateObligation<'tcx>, &'tcx ty::List<ty::subst::GenericArg<'tcx>>) {
// Construct a trait-reference `self_ty : Trait<input_tys>`
let substs = InternalSubsts::for_item(self.tcx, trait_def_id, |param, _| {
match param.kind {

View File

@ -123,7 +123,7 @@ pub struct FulfillmentError<'tcx> {
#[derive(Clone)]
pub enum FulfillmentErrorCode<'tcx> {
/// Inherently impossible to fulfill; this trait is implemented if and only if it is already implemented.
CodeCycle(Vec<Obligation<'tcx, ty::Predicate<'tcx>>>),
CodeCycle(Vec<PredicateObligation<'tcx>>),
CodeSelectionError(SelectionError<'tcx>),
CodeProjectionError(MismatchedProjectionTypes<'tcx>),
CodeSubtypeError(ExpectedFound<Ty<'tcx>>, TypeError<'tcx>), // always comes from a SubtypePredicate

View File

@ -591,7 +591,7 @@ fn is_self_referential_projection(&self, p: ty::PolyProjectionPredicate<'_>) ->
fn evaluate_nested_obligations(
&self,
ty: Ty<'_>,
nested: impl Iterator<Item = Obligation<'tcx, ty::Predicate<'tcx>>>,
nested: impl Iterator<Item = PredicateObligation<'tcx>>,
computed_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
fresh_preds: &mut FxHashSet<ty::Predicate<'tcx>>,
predicates: &mut VecDeque<ty::PolyTraitPredicate<'tcx>>,

View File

@ -327,7 +327,7 @@ pub struct OnUnimplementedDirective {
pub label: Option<OnUnimplementedFormatString>,
pub note: Option<OnUnimplementedFormatString>,
pub parent_label: Option<OnUnimplementedFormatString>,
pub append_const_msg: Option<Option<Symbol>>,
pub append_const_msg: Option<AppendConstMessage>,
}
/// For the `#[rustc_on_unimplemented]` attribute
@ -337,11 +337,21 @@ pub struct OnUnimplementedNote {
pub label: Option<String>,
pub note: Option<String>,
pub parent_label: Option<String>,
/// Append a message for `~const Trait` errors. `None` means not requested and
/// should fallback to a generic message, `Some(None)` suggests using the default
/// appended message, `Some(Some(s))` suggests use the `s` message instead of the
/// default one..
pub append_const_msg: Option<Option<Symbol>>,
// If none, should fall back to a generic message
pub append_const_msg: Option<AppendConstMessage>,
}
/// Append a message for `~const Trait` errors.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AppendConstMessage {
Default,
Custom(Symbol),
}
impl Default for AppendConstMessage {
fn default() -> Self {
AppendConstMessage::Default
}
}
impl<'tcx> OnUnimplementedDirective {
@ -419,10 +429,10 @@ fn parse(
}
} else if item.has_name(sym::append_const_msg) && append_const_msg.is_none() {
if let Some(msg) = item.value_str() {
append_const_msg = Some(Some(msg));
append_const_msg = Some(AppendConstMessage::Custom(msg));
continue;
} else if item.is_word() {
append_const_msg = Some(None);
append_const_msg = Some(AppendConstMessage::Default);
continue;
}
}

View File

@ -386,7 +386,7 @@ fn probe_assoc_types_at_expr(
fn maybe_suggest_convert_to_slice(
&self,
err: &mut Diagnostic,
trait_ref: ty::Binder<'tcx, ty::TraitRef<'tcx>>,
trait_ref: ty::PolyTraitRef<'tcx>,
candidate_impls: &[ImplCandidate<'tcx>],
span: Span,
);
@ -3848,7 +3848,7 @@ fn probe_assoc_types_at_expr(
fn maybe_suggest_convert_to_slice(
&self,
err: &mut Diagnostic,
trait_ref: ty::Binder<'tcx, ty::TraitRef<'tcx>>,
trait_ref: ty::PolyTraitRef<'tcx>,
candidate_impls: &[ImplCandidate<'tcx>],
span: Span,
) {