Skip to content

Commit b8feb63

Browse files
committed
Port WhereClauseSuggestions
1 parent 8d590dc commit b8feb63

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,6 @@ infer_ril_introduced_here = `'static` requirement introduced here
327327
infer_ril_introduced_by = requirement introduced by this return type
328328
infer_ril_because_of = because of this returned expression
329329
infer_ril_static_introduced_by = "`'static` lifetime requirement introduced by the return type
330+
331+
infer_where_remove = remove the `where` clause
332+
infer_where_copy_predicates = copy the `where` clause predicates from the trait

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,3 +980,29 @@ pub struct RefLongerThanData<'a> {
980980
#[subdiagnostic]
981981
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
982982
}
983+
984+
#[derive(Subdiagnostic)]
985+
pub enum WhereClauseSuggestions {
986+
#[suggestion(
987+
infer_where_remove,
988+
code = "",
989+
applicability = "machine-applicable",
990+
style = "verbose"
991+
)]
992+
Remove {
993+
#[primary_span]
994+
span: Span,
995+
},
996+
#[suggestion(
997+
infer_where_copy_predicates,
998+
code = "{space}where {}",
999+
applicability = "machine-applicable",
1000+
style = "verbose"
1001+
)]
1002+
CopyPredicates {
1003+
#[primary_span]
1004+
span: Span,
1005+
space: &'static str,
1006+
trait_predicates: String,
1007+
},
1008+
}

compiler/rustc_infer/src/infer/error_reporting/note.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::errors::{
22
note_and_explain, FullfillReqLifetime, LfBoundNotSatisfied, OutlivesBound, OutlivesContent,
3-
RefLongerThanData, RegionOriginNote,
3+
RefLongerThanData, RegionOriginNote, WhereClauseSuggestions,
44
};
55
use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt};
66
use crate::infer::{self, SubregionOrigin};
77
use rustc_errors::{
8-
fluent, AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
9-
IntoDiagnostic,
8+
fluent, AddToDiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
109
};
1110
use rustc_hir::def_id::{DefId, LocalDefId};
1211
use rustc_middle::traits::ObligationCauseCode;
@@ -325,22 +324,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
325324

326325
let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id) else { return; };
327326

328-
if trait_predicates.is_empty() {
329-
err.span_suggestion_verbose(
330-
generics.where_clause_span,
331-
"remove the `where` clause",
332-
String::new(),
333-
Applicability::MachineApplicable,
334-
);
327+
let suggestion = if trait_predicates.is_empty() {
328+
WhereClauseSuggestions::Remove { span: generics.where_clause_span }
335329
} else {
336330
let space = if generics.where_clause_span.is_empty() { " " } else { "" };
337-
err.span_suggestion_verbose(
338-
generics.where_clause_span,
339-
"copy the `where` clause predicates from the trait",
340-
format!("{space}where {}", trait_predicates.join(", ")),
341-
Applicability::MachineApplicable,
342-
);
343-
}
331+
WhereClauseSuggestions::CopyPredicates {
332+
span: generics.where_clause_span,
333+
space,
334+
trait_predicates: trait_predicates.join(", "),
335+
}
336+
};
337+
err.subdiagnostic(suggestion);
344338
}
345339

346340
pub(super) fn report_placeholder_failure(

0 commit comments

Comments
 (0)