Skip to content

Commit 1068a24

Browse files
committed
region_names ini
1 parent 8c6ce6b commit 1068a24

File tree

3 files changed

+108
-21
lines changed

3 files changed

+108
-21
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+35-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// #![deny(rustc::untranslatable_diagnostic)]
2+
// #![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use std::fmt::{self, Display};
25
use std::iter;
36

@@ -10,6 +13,7 @@ use rustc_middle::ty::{self, DefIdTree, RegionVid, Ty};
1013
use rustc_span::symbol::{kw, sym, Ident, Symbol};
1114
use rustc_span::{Span, DUMMY_SP};
1215

16+
use crate::session_diagnostics::RegionNameLables;
1317
use crate::{nll::ToRegionVid, universal_regions::DefiningTy, MirBorrowckCtxt};
1418

1519
/// A name for a particular region used in emitting diagnostics. This name could be a generated
@@ -133,48 +137,58 @@ impl RegionName {
133137
RegionNameHighlight::MatchedAdtAndSegment(span),
134138
_,
135139
) => {
136-
diag.span_label(*span, format!("let's call this `{self}`"));
140+
diag.subdiagnostic(RegionNameLables::NameRegion { span: *span, rg_name: self });
137141
}
138142
RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::Occluded(
139143
span,
140144
type_name,
141145
)) => {
142-
diag.span_label(
143-
*span,
144-
format!("lifetime `{self}` appears in the type {type_name}"),
145-
);
146+
diag.subdiagnostic(RegionNameLables::LifetimeInType {
147+
span: *span,
148+
type_name: &type_name,
149+
rg_name: self,
150+
});
146151
}
147152
RegionNameSource::AnonRegionFromOutput(
148153
RegionNameHighlight::Occluded(span, type_name),
149154
mir_description,
150155
) => {
151-
diag.span_label(
152-
*span,
153-
format!(
154-
"return type{mir_description} `{type_name}` contains a lifetime `{self}`"
155-
),
156-
);
156+
diag.subdiagnostic(RegionNameLables::LifetimeInReturned {
157+
span: *span,
158+
mir_description,
159+
type_name: &type_name,
160+
rg_name: self,
161+
});
157162
}
158163
RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => {
159-
diag.span_label(
160-
*span,
161-
format!("lifetime `{self}` appears in the type of `{upvar_name}`"),
162-
);
164+
diag.subdiagnostic(RegionNameLables::LifetimeInTypeOf {
165+
span: *span,
166+
upvar_name: upvar_name.to_ident_string(),
167+
rg_name: self,
168+
});
163169
}
164170
RegionNameSource::AnonRegionFromOutput(
165171
RegionNameHighlight::CannotMatchHirTy(span, type_name),
166172
mir_description,
167173
) => {
168-
diag.span_label(*span, format!("return type{mir_description} is {type_name}"));
174+
diag.subdiagnostic(RegionNameLables::ReturnTypeIsTpye {
175+
span: *span,
176+
mir_description,
177+
type_name: &type_name,
178+
});
169179
}
170180
RegionNameSource::AnonRegionFromYieldTy(span, type_name) => {
171-
diag.span_label(*span, format!("yield type is {type_name}"));
181+
diag.subdiagnostic(RegionNameLables::YieldTypeIsTpye {
182+
span: *span,
183+
type_name: &type_name,
184+
});
172185
}
173186
RegionNameSource::AnonRegionFromImplSignature(span, location) => {
174-
diag.span_label(
175-
*span,
176-
format!("lifetime `{self}` appears in the `impl`'s {location}"),
177-
);
187+
diag.subdiagnostic(RegionNameLables::LifetimeInImpl {
188+
span: *span,
189+
rg_name: self,
190+
location: &location,
191+
});
178192
}
179193
RegionNameSource::Static => {}
180194
}

compiler/rustc_borrowck/src/session_diagnostics.rs

+52
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,55 @@ pub(crate) enum RequireStaticErr {
157157
multi_span: MultiSpan,
158158
},
159159
}
160+
161+
#[derive(SessionSubdiagnostic)]
162+
pub(crate) enum RegionNameLables<'a> {
163+
#[label(borrowck::name_this_region)]
164+
NameRegion {
165+
#[primary_span]
166+
span: Span,
167+
rg_name: &'a RegionName,
168+
},
169+
#[label(borrowck::lifetime_appears_in_type)]
170+
LifetimeInType {
171+
#[primary_span]
172+
span: Span,
173+
type_name: &'a str,
174+
rg_name: &'a RegionName,
175+
},
176+
#[label(borrowck::return_type_has_lifetime)]
177+
LifetimeInReturned {
178+
#[primary_span]
179+
span: Span,
180+
mir_description: &'a str,
181+
type_name: &'a str,
182+
rg_name: &'a RegionName,
183+
},
184+
#[label(borrowck::lifetime_appears_in_type_of)]
185+
LifetimeInTypeOf {
186+
#[primary_span]
187+
span: Span,
188+
upvar_name: String,
189+
rg_name: &'a RegionName,
190+
},
191+
#[label(borrowck::return_type_is_type)]
192+
ReturnTypeIsTpye {
193+
#[primary_span]
194+
span: Span,
195+
mir_description: &'a str,
196+
type_name: &'a str,
197+
},
198+
#[label(borrowck::yield_type_is_type)]
199+
YieldTypeIsTpye {
200+
#[primary_span]
201+
span: Span,
202+
type_name: &'a str,
203+
},
204+
#[label(borrowck::lifetime_appears_here_in_impl)]
205+
LifetimeInImpl {
206+
#[primary_span]
207+
span: Span,
208+
rg_name: &'a RegionName,
209+
location: &'a str,
210+
},
211+
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,24 @@ borrowck_returned_lifetime_short =
5858
5959
borrowck_used_impl_require_static =
6060
the used `impl` has a `'static` requirement
61+
62+
borrowck_name_this_region =
63+
let's call this `{$rg_name}`
64+
65+
borrowck_lifetime_appears_in_type =
66+
lifetime `{$rg_name}` appears in the type {$type_name}
67+
68+
borrowck_return_type_has_lifetime =
69+
return type{$mir_description} `{$type_name}` contains a lifetime `{$rg_name}`
70+
71+
borrowck_lifetime_appears_in_type_of =
72+
lifetime `{$rg_name}` appears in the type of `{$upvar_name}`
73+
74+
borrowck_return_type_is_type =
75+
return type{$mir_description} is {$type_name}
76+
77+
borrowck_yield_type_is_type =
78+
yield type is {$type_name}
79+
80+
borrowck_lifetime_appears_here_in_impl =
81+
lifetime `{$rg_name}` appears in the `impl`'s {$location}

0 commit comments

Comments
 (0)