Port WhereClauseSuggestions

This commit is contained in:
Nikita Tomashevich 2023-01-22 18:16:47 +03:00 committed by IQuant
parent 8d590dc303
commit b8feb63345
3 changed files with 40 additions and 17 deletions

View file

@ -327,3 +327,6 @@ infer_ril_introduced_here = `'static` requirement introduced here
infer_ril_introduced_by = requirement introduced by this return type infer_ril_introduced_by = requirement introduced by this return type
infer_ril_because_of = because of this returned expression infer_ril_because_of = because of this returned expression
infer_ril_static_introduced_by = "`'static` lifetime requirement introduced by the return type infer_ril_static_introduced_by = "`'static` lifetime requirement introduced by the return type
infer_where_remove = remove the `where` clause
infer_where_copy_predicates = copy the `where` clause predicates from the trait

View file

@ -980,3 +980,29 @@ pub struct RefLongerThanData<'a> {
#[subdiagnostic] #[subdiagnostic]
pub notes: Vec<note_and_explain::RegionExplanation<'a>>, pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
} }
#[derive(Subdiagnostic)]
pub enum WhereClauseSuggestions {
#[suggestion(
infer_where_remove,
code = "",
applicability = "machine-applicable",
style = "verbose"
)]
Remove {
#[primary_span]
span: Span,
},
#[suggestion(
infer_where_copy_predicates,
code = "{space}where {}",
applicability = "machine-applicable",
style = "verbose"
)]
CopyPredicates {
#[primary_span]
span: Span,
space: &'static str,
trait_predicates: String,
},
}

View file

@ -1,12 +1,11 @@
use crate::errors::{ use crate::errors::{
note_and_explain, FullfillReqLifetime, LfBoundNotSatisfied, OutlivesBound, OutlivesContent, note_and_explain, FullfillReqLifetime, LfBoundNotSatisfied, OutlivesBound, OutlivesContent,
RefLongerThanData, RegionOriginNote, RefLongerThanData, RegionOriginNote, WhereClauseSuggestions,
}; };
use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt}; use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt};
use crate::infer::{self, SubregionOrigin}; use crate::infer::{self, SubregionOrigin};
use rustc_errors::{ use rustc_errors::{
fluent, AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, fluent, AddToDiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
IntoDiagnostic,
}; };
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::traits::ObligationCauseCode; use rustc_middle::traits::ObligationCauseCode;
@ -325,22 +324,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id) else { return; }; let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id) else { return; };
if trait_predicates.is_empty() { let suggestion = if trait_predicates.is_empty() {
err.span_suggestion_verbose( WhereClauseSuggestions::Remove { span: generics.where_clause_span }
generics.where_clause_span,
"remove the `where` clause",
String::new(),
Applicability::MachineApplicable,
);
} else { } else {
let space = if generics.where_clause_span.is_empty() { " " } else { "" }; let space = if generics.where_clause_span.is_empty() { " " } else { "" };
err.span_suggestion_verbose( WhereClauseSuggestions::CopyPredicates {
generics.where_clause_span, span: generics.where_clause_span,
"copy the `where` clause predicates from the trait", space,
format!("{space}where {}", trait_predicates.join(", ")), trait_predicates: trait_predicates.join(", "),
Applicability::MachineApplicable, }
); };
} err.subdiagnostic(suggestion);
} }
pub(super) fn report_placeholder_failure( pub(super) fn report_placeholder_failure(