Skip to content

Commit fc1b988

Browse files
committed
region_name.rs init
1 parent 9fa755f commit fc1b988

File tree

3 files changed

+108
-21
lines changed

3 files changed

+108
-21
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 35 additions & 21 deletions
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::RegionNameLabels;
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(RegionNameLabels::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(RegionNameLabels::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(RegionNameLabels::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(RegionNameLabels::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(RegionNameLabels::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(RegionNameLabels::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(RegionNameLabels::LifetimeInImpl {
188+
span: *span,
189+
rg_name: self,
190+
location: &location,
191+
});
178192
}
179193
RegionNameSource::Static => {}
180194
}

compiler/rustc_borrowck/src/session_diagnostics.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,55 @@ pub(crate) enum FnMutBumpFn<'a> {
484484
span: Span,
485485
},
486486
}
487+
488+
#[derive(SessionSubdiagnostic)]
489+
pub(crate) enum RegionNameLabels<'a> {
490+
#[label(borrowck::name_this_region)]
491+
NameRegion {
492+
#[primary_span]
493+
span: Span,
494+
rg_name: &'a RegionName,
495+
},
496+
#[label(borrowck::lifetime_appears_in_type)]
497+
LifetimeInType {
498+
#[primary_span]
499+
span: Span,
500+
type_name: &'a str,
501+
rg_name: &'a RegionName,
502+
},
503+
#[label(borrowck::return_type_has_lifetime)]
504+
LifetimeInReturned {
505+
#[primary_span]
506+
span: Span,
507+
mir_description: &'a str,
508+
type_name: &'a str,
509+
rg_name: &'a RegionName,
510+
},
511+
#[label(borrowck::lifetime_appears_in_type_of)]
512+
LifetimeInTypeOf {
513+
#[primary_span]
514+
span: Span,
515+
upvar_name: String,
516+
rg_name: &'a RegionName,
517+
},
518+
#[label(borrowck::return_type_is_type)]
519+
ReturnTypeIsTpye {
520+
#[primary_span]
521+
span: Span,
522+
mir_description: &'a str,
523+
type_name: &'a str,
524+
},
525+
#[label(borrowck::yield_type_is_type)]
526+
YieldTypeIsTpye {
527+
#[primary_span]
528+
span: Span,
529+
type_name: &'a str,
530+
},
531+
#[label(borrowck::lifetime_appears_here_in_impl)]
532+
LifetimeInImpl {
533+
#[primary_span]
534+
span: Span,
535+
rg_name: &'a RegionName,
536+
location: &'a str,
537+
},
538+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,24 @@ borrowck_in_this_closure =
197197
198198
borrowck_return_fnmut =
199199
change this to return `FnMut` instead of `Fn`
200+
201+
borrowck_name_this_region =
202+
let's call this `{$rg_name}`
203+
204+
borrowck_lifetime_appears_in_type =
205+
lifetime `{$rg_name}` appears in the type {$type_name}
206+
207+
borrowck_return_type_has_lifetime =
208+
return type{$mir_description} `{$type_name}` contains a lifetime `{$rg_name}`
209+
210+
borrowck_lifetime_appears_in_type_of =
211+
lifetime `{$rg_name}` appears in the type of `{$upvar_name}`
212+
213+
borrowck_return_type_is_type =
214+
return type{$mir_description} is {$type_name}
215+
216+
borrowck_yield_type_is_type =
217+
yield type is {$type_name}
218+
219+
borrowck_lifetime_appears_here_in_impl =
220+
lifetime `{$rg_name}` appears in the `impl`'s {$location}

0 commit comments

Comments
 (0)