Skip to content

Commit 1fdf0e1

Browse files
authored
Rollup merge of #107034 - IntQuant:issue-100717-infer-5, r=oli-obk
Migrating rustc_infer to session diagnostics (part 4) `@rustbot` label +A-translation r? rust-lang/diagnostics cc #100717
2 parents 2d14db3 + 58939b9 commit 1fdf0e1

File tree

7 files changed

+486
-231
lines changed

7 files changed

+486
-231
lines changed

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

+38-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ infer_lifetime_param_suggestion_elided = each elided lifetime in input position
140140
141141
infer_region_explanation = {$pref_kind ->
142142
*[should_not_happen] [{$pref_kind}]
143+
[ref_valid_for] ...the reference is valid for
144+
[content_valid_for] ...but the borrowed content is only valid for
145+
[type_obj_valid_for] object type is valid for
146+
[source_pointer_valid_for] source pointer is only valid for
147+
[type_satisfy] type must satisfy
148+
[type_outlive] type must outlive
149+
[lf_param_instantiated_with] lifetime parameter instantiated with
150+
[lf_param_must_outlive] but lifetime parameter must outlive
151+
[lf_instantiated_with] lifetime instantiated with
152+
[lf_must_outlive] but lifetime must outlive
153+
[pointer_valid_for] the pointer is valid for
154+
[data_valid_for] but the referenced data is only valid for
143155
[empty] {""}
144156
}{$pref_kind ->
145157
[empty] {""}
@@ -148,7 +160,6 @@ infer_region_explanation = {$pref_kind ->
148160
*[should_not_happen] [{$desc_kind}]
149161
[restatic] the static lifetime
150162
[revar] lifetime {$desc_arg}
151-
152163
[as_defined] the lifetime `{$desc_arg}` as defined here
153164
[as_defined_anon] the anonymous lifetime as defined here
154165
[defined_here] the anonymous lifetime defined here
@@ -158,8 +169,16 @@ infer_region_explanation = {$pref_kind ->
158169
*[should_not_happen] [{$suff_kind}]
159170
[empty]{""}
160171
[continues] ...
172+
[req_by_binding] {" "}as required by this binding
161173
}
162174
175+
infer_outlives_content = lifetime of reference outlives lifetime of borrowed content...
176+
infer_outlives_bound = lifetime of the source pointer does not outlive lifetime bound of the object type
177+
infer_fullfill_req_lifetime = the type `{$ty}` does not fulfill the required lifetime
178+
infer_lf_bound_not_satisfied = lifetime bound not satisfied
179+
infer_borrowed_too_long = a value of type `{$ty}` is borrowed for too long
180+
infer_ref_longer_than_data = in type `{$ty}`, reference has a longer lifetime than the data it references
181+
163182
infer_mismatched_static_lifetime = incompatible lifetime on type
164183
infer_does_not_outlive_static_from_impl = ...does not necessarily outlive the static lifetime introduced by the compatible `impl`
165184
infer_implicit_static_lifetime_note = this has an implicit `'static` lifetime requirement
@@ -308,3 +327,21 @@ infer_ril_introduced_here = `'static` requirement introduced here
308327
infer_ril_introduced_by = requirement introduced by this return type
309328
infer_ril_because_of = because of this returned expression
310329
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
333+
334+
infer_srs_remove_and_box = consider removing this semicolon and boxing the expressions
335+
infer_srs_remove = consider removing this semicolon
336+
infer_srs_add = consider returning the local binding `{$ident}`
337+
infer_srs_add_one = consider returning one of these bindings
338+
339+
infer_await_both_futures = consider `await`ing on both `Future`s
340+
infer_await_future = consider `await`ing on the `Future`
341+
infer_await_note = calling an async function returns a future
342+
343+
infer_prlf_defined_with_sub = the lifetime `{$sub_symbol}` defined here...
344+
infer_prlf_defined_without_sub = the lifetime defined here...
345+
infer_prlf_must_oultive_with_sup = ...must outlive the lifetime `{$sup_symbol}` defined here
346+
infer_prlf_must_oultive_without_sup = ...must outlive the lifetime defined here
347+
infer_prlf_known_limitation = this is a known limitation that will be removed in the future (see issue #100013 <https://github.com/rust-lang/rust/issues/100013> for more information)

compiler/rustc_infer/src/errors/mod.rs

+213
Original file line numberDiff line numberDiff line change
@@ -933,3 +933,216 @@ pub struct ButNeedsToSatisfy {
933933
pub has_lifetime: bool,
934934
pub lifetime: String,
935935
}
936+
937+
#[derive(Diagnostic)]
938+
#[diag(infer_outlives_content, code = "E0312")]
939+
pub struct OutlivesContent<'a> {
940+
#[primary_span]
941+
pub span: Span,
942+
#[subdiagnostic]
943+
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
944+
}
945+
946+
#[derive(Diagnostic)]
947+
#[diag(infer_outlives_bound, code = "E0476")]
948+
pub struct OutlivesBound<'a> {
949+
#[primary_span]
950+
pub span: Span,
951+
#[subdiagnostic]
952+
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
953+
}
954+
955+
#[derive(Diagnostic)]
956+
#[diag(infer_fullfill_req_lifetime, code = "E0477")]
957+
pub struct FullfillReqLifetime<'a> {
958+
#[primary_span]
959+
pub span: Span,
960+
pub ty: Ty<'a>,
961+
#[subdiagnostic]
962+
pub note: Option<note_and_explain::RegionExplanation<'a>>,
963+
}
964+
965+
#[derive(Diagnostic)]
966+
#[diag(infer_lf_bound_not_satisfied, code = "E0478")]
967+
pub struct LfBoundNotSatisfied<'a> {
968+
#[primary_span]
969+
pub span: Span,
970+
#[subdiagnostic]
971+
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
972+
}
973+
974+
#[derive(Diagnostic)]
975+
#[diag(infer_ref_longer_than_data, code = "E0491")]
976+
pub struct RefLongerThanData<'a> {
977+
#[primary_span]
978+
pub span: Span,
979+
pub ty: Ty<'a>,
980+
#[subdiagnostic]
981+
pub notes: Vec<note_and_explain::RegionExplanation<'a>>,
982+
}
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 {trait_predicates}",
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+
}
1009+
1010+
#[derive(Subdiagnostic)]
1011+
pub enum SuggestRemoveSemiOrReturnBinding {
1012+
#[multipart_suggestion(infer_srs_remove_and_box, applicability = "machine-applicable")]
1013+
RemoveAndBox {
1014+
#[suggestion_part(code = "Box::new(")]
1015+
first_lo: Span,
1016+
#[suggestion_part(code = ")")]
1017+
first_hi: Span,
1018+
#[suggestion_part(code = "Box::new(")]
1019+
second_lo: Span,
1020+
#[suggestion_part(code = ")")]
1021+
second_hi: Span,
1022+
#[suggestion_part(code = "")]
1023+
sp: Span,
1024+
},
1025+
#[suggestion(
1026+
infer_srs_remove,
1027+
style = "short",
1028+
code = "",
1029+
applicability = "machine-applicable"
1030+
)]
1031+
Remove {
1032+
#[primary_span]
1033+
sp: Span,
1034+
},
1035+
#[suggestion(
1036+
infer_srs_add,
1037+
style = "verbose",
1038+
code = "{code}",
1039+
applicability = "maybe-incorrect"
1040+
)]
1041+
Add {
1042+
#[primary_span]
1043+
sp: Span,
1044+
code: String,
1045+
ident: Ident,
1046+
},
1047+
#[note(infer_srs_add_one)]
1048+
AddOne {
1049+
#[primary_span]
1050+
spans: MultiSpan,
1051+
},
1052+
}
1053+
1054+
#[derive(Subdiagnostic)]
1055+
pub enum ConsiderAddingAwait {
1056+
#[help(infer_await_both_futures)]
1057+
BothFuturesHelp,
1058+
#[multipart_suggestion(infer_await_both_futures, applicability = "maybe-incorrect")]
1059+
BothFuturesSugg {
1060+
#[suggestion_part(code = ".await")]
1061+
first: Span,
1062+
#[suggestion_part(code = ".await")]
1063+
second: Span,
1064+
},
1065+
#[suggestion(
1066+
infer_await_future,
1067+
code = ".await",
1068+
style = "verbose",
1069+
applicability = "maybe-incorrect"
1070+
)]
1071+
FutureSugg {
1072+
#[primary_span]
1073+
span: Span,
1074+
},
1075+
#[note(infer_await_note)]
1076+
FutureSuggNote {
1077+
#[primary_span]
1078+
span: Span,
1079+
},
1080+
#[multipart_suggestion(
1081+
infer_await_future,
1082+
style = "verbose",
1083+
applicability = "maybe-incorrect"
1084+
)]
1085+
FutureSuggMultiple {
1086+
#[suggestion_part(code = ".await")]
1087+
spans: Vec<Span>,
1088+
},
1089+
}
1090+
1091+
#[derive(Diagnostic)]
1092+
pub enum PlaceholderRelationLfNotSatisfied {
1093+
#[diag(infer_lf_bound_not_satisfied)]
1094+
HasBoth {
1095+
#[primary_span]
1096+
span: Span,
1097+
#[note(infer_prlf_defined_with_sub)]
1098+
sub_span: Span,
1099+
#[note(infer_prlf_must_oultive_with_sup)]
1100+
sup_span: Span,
1101+
sub_symbol: Symbol,
1102+
sup_symbol: Symbol,
1103+
#[note(infer_prlf_known_limitation)]
1104+
note: (),
1105+
},
1106+
#[diag(infer_lf_bound_not_satisfied)]
1107+
HasSub {
1108+
#[primary_span]
1109+
span: Span,
1110+
#[note(infer_prlf_defined_with_sub)]
1111+
sub_span: Span,
1112+
#[note(infer_prlf_must_oultive_without_sup)]
1113+
sup_span: Span,
1114+
sub_symbol: Symbol,
1115+
#[note(infer_prlf_known_limitation)]
1116+
note: (),
1117+
},
1118+
#[diag(infer_lf_bound_not_satisfied)]
1119+
HasSup {
1120+
#[primary_span]
1121+
span: Span,
1122+
#[note(infer_prlf_defined_without_sub)]
1123+
sub_span: Span,
1124+
#[note(infer_prlf_must_oultive_with_sup)]
1125+
sup_span: Span,
1126+
sup_symbol: Symbol,
1127+
#[note(infer_prlf_known_limitation)]
1128+
note: (),
1129+
},
1130+
#[diag(infer_lf_bound_not_satisfied)]
1131+
HasNone {
1132+
#[primary_span]
1133+
span: Span,
1134+
#[note(infer_prlf_defined_without_sub)]
1135+
sub_span: Span,
1136+
#[note(infer_prlf_must_oultive_without_sup)]
1137+
sup_span: Span,
1138+
#[note(infer_prlf_known_limitation)]
1139+
note: (),
1140+
},
1141+
#[diag(infer_lf_bound_not_satisfied)]
1142+
OnlyPrimarySpan {
1143+
#[primary_span]
1144+
span: Span,
1145+
#[note(infer_prlf_known_limitation)]
1146+
note: (),
1147+
},
1148+
}

compiler/rustc_infer/src/errors/note_and_explain.rs

+37-7
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,42 @@ impl<'a> DescriptionCtx<'a> {
121121

122122
pub enum PrefixKind {
123123
Empty,
124+
RefValidFor,
125+
ContentValidFor,
126+
TypeObjValidFor,
127+
SourcePointerValidFor,
128+
TypeSatisfy,
129+
TypeOutlive,
130+
LfParamInstantiatedWith,
131+
LfParamMustOutlive,
132+
LfInstantiatedWith,
133+
LfMustOutlive,
134+
PointerValidFor,
135+
DataValidFor,
124136
}
125137

126138
pub enum SuffixKind {
139+
Empty,
127140
Continues,
141+
ReqByBinding,
128142
}
129143

130144
impl IntoDiagnosticArg for PrefixKind {
131145
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
132146
let kind = match self {
133147
Self::Empty => "empty",
148+
Self::RefValidFor => "ref_valid_for",
149+
Self::ContentValidFor => "content_valid_for",
150+
Self::TypeObjValidFor => "type_obj_valid_for",
151+
Self::SourcePointerValidFor => "source_pointer_valid_for",
152+
Self::TypeSatisfy => "type_satisfy",
153+
Self::TypeOutlive => "type_outlive",
154+
Self::LfParamInstantiatedWith => "lf_param_instantiated_with",
155+
Self::LfParamMustOutlive => "lf_param_must_outlive",
156+
Self::LfInstantiatedWith => "lf_instantiated_with",
157+
Self::LfMustOutlive => "lf_must_outlive",
158+
Self::PointerValidFor => "pointer_valid_for",
159+
Self::DataValidFor => "data_valid_for",
134160
}
135161
.into();
136162
rustc_errors::DiagnosticArgValue::Str(kind)
@@ -140,7 +166,9 @@ impl IntoDiagnosticArg for PrefixKind {
140166
impl IntoDiagnosticArg for SuffixKind {
141167
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
142168
let kind = match self {
169+
Self::Empty => "empty",
143170
Self::Continues => "continues",
171+
Self::ReqByBinding => "req_by_binding",
144172
}
145173
.into();
146174
rustc_errors::DiagnosticArgValue::Str(kind)
@@ -166,17 +194,19 @@ impl RegionExplanation<'_> {
166194
}
167195

168196
impl AddToDiagnostic for RegionExplanation<'_> {
169-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
197+
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
170198
where
171199
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
172200
{
173-
if let Some(span) = self.desc.span {
174-
diag.span_note(span, fluent::infer_region_explanation);
175-
} else {
176-
diag.note(fluent::infer_region_explanation);
177-
}
178-
self.desc.add_to(diag);
179201
diag.set_arg("pref_kind", self.prefix);
180202
diag.set_arg("suff_kind", self.suffix);
203+
let desc_span = self.desc.span;
204+
self.desc.add_to(diag);
205+
let msg = f(diag, fluent::infer_region_explanation.into());
206+
if let Some(span) = desc_span {
207+
diag.span_note(span, msg);
208+
} else {
209+
diag.note(msg);
210+
}
181211
}
182212
}

0 commit comments

Comments
 (0)