Skip to content

Commit 795be51

Browse files
committed
Make RegionName Copy by (transitively) interning the few string variants
1 parent ad3d04c commit 795be51

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{universal_regions::DefiningTy, MirBorrowckCtxt};
1515

1616
/// A name for a particular region used in emitting diagnostics. This name could be a generated
1717
/// name like `'1`, a name used by the user like `'a`, or a name like `'static`.
18-
#[derive(Debug, Clone)]
18+
#[derive(Debug, Clone, Copy)]
1919
pub(crate) struct RegionName {
2020
/// The name of the region (interned).
2121
pub(crate) name: Symbol,
@@ -26,7 +26,7 @@ pub(crate) struct RegionName {
2626
/// Denotes the source of a region that is named by a `RegionName`. For example, a free region that
2727
/// was named by the user would get `NamedLateParamRegion` and `'static` lifetime would get `Static`.
2828
/// This helps to print the right kinds of diagnostics.
29-
#[derive(Debug, Clone)]
29+
#[derive(Debug, Clone, Copy)]
3030
pub(crate) enum RegionNameSource {
3131
/// A bound (not free) region that was instantiated at the def site (not an HRTB).
3232
NamedEarlyParamRegion(Span),
@@ -43,7 +43,7 @@ pub(crate) enum RegionNameSource {
4343
/// The region corresponding to the return type of a closure.
4444
AnonRegionFromOutput(RegionNameHighlight, &'static str),
4545
/// The region from a type yielded by a coroutine.
46-
AnonRegionFromYieldTy(Span, String),
46+
AnonRegionFromYieldTy(Span, Symbol),
4747
/// An anonymous region from an async fn.
4848
AnonRegionFromAsyncFn(Span),
4949
/// An anonymous region from an impl self type or trait
@@ -52,19 +52,19 @@ pub(crate) enum RegionNameSource {
5252

5353
/// Describes what to highlight to explain to the user that we're giving an anonymous region a
5454
/// synthesized name, and how to highlight it.
55-
#[derive(Debug, Clone)]
55+
#[derive(Debug, Clone, Copy)]
5656
pub(crate) enum RegionNameHighlight {
5757
/// The anonymous region corresponds to a reference that was found by traversing the type in the HIR.
5858
MatchedHirTy(Span),
5959
/// The anonymous region corresponds to a `'_` in the generics list of a struct/enum/union.
6060
MatchedAdtAndSegment(Span),
6161
/// The anonymous region corresponds to a region where the type annotation is completely missing
6262
/// from the code, e.g. in a closure arguments `|x| { ... }`, where `x` is a reference.
63-
CannotMatchHirTy(Span, String),
63+
CannotMatchHirTy(Span, Symbol),
6464
/// The anonymous region corresponds to a region where the type annotation is completely missing
6565
/// from the code, and *even if* we print out the full name of the type, the region name won't
6666
/// be included. This currently occurs for opaque types like `impl Future`.
67-
Occluded(Span, String),
67+
Occluded(Span, Symbol),
6868
}
6969

7070
impl RegionName {
@@ -249,7 +249,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
249249
assert!(self.regioncx.universal_regions().is_universal_region(fr));
250250

251251
match self.region_names.borrow_mut().entry(fr) {
252-
IndexEntry::Occupied(precomputed_name) => Some(precomputed_name.get().clone()),
252+
IndexEntry::Occupied(precomputed_name) => Some(*precomputed_name.get()),
253253
IndexEntry::Vacant(slot) => {
254254
let new_name = self
255255
.give_name_from_error_region(fr)
@@ -262,8 +262,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
262262
self.give_name_if_anonymous_region_appears_in_arg_position_impl_trait(fr)
263263
});
264264

265-
if let Some(new_name) = &new_name {
266-
slot.insert(new_name.clone());
265+
if let Some(new_name) = new_name {
266+
slot.insert(new_name);
267267
}
268268
debug!("give_region_a_name: gave name {:?}", new_name);
269269

@@ -460,9 +460,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
460460
);
461461
if type_name.contains(&format!("'{counter}")) {
462462
// Only add a label if we can confirm that a region was labelled.
463-
RegionNameHighlight::CannotMatchHirTy(span, type_name)
463+
RegionNameHighlight::CannotMatchHirTy(span, Symbol::intern(&type_name))
464464
} else {
465-
RegionNameHighlight::Occluded(span, type_name)
465+
RegionNameHighlight::Occluded(span, Symbol::intern(&type_name))
466466
}
467467
}
468468

@@ -882,7 +882,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
882882

883883
Some(RegionName {
884884
name: self.synthesize_region_name(),
885-
source: RegionNameSource::AnonRegionFromYieldTy(yield_span, type_name),
885+
source: RegionNameSource::AnonRegionFromYieldTy(yield_span, Symbol::intern(&type_name)),
886886
})
887887
}
888888

@@ -974,7 +974,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
974974
Some(RegionName {
975975
name: region_name,
976976
source: RegionNameSource::AnonRegionFromArgument(
977-
RegionNameHighlight::CannotMatchHirTy(arg_span, arg_name?.to_string()),
977+
RegionNameHighlight::CannotMatchHirTy(arg_span, arg_name?),
978978
),
979979
})
980980
} else {

0 commit comments

Comments
 (0)