Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 601518e

Browse files
committed
change returns to RegionNameHighlight
1 parent 51af5af commit 601518e

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

src/librustc_mir/borrow_check/diagnostics/region_name.rs

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -336,21 +336,35 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
336336

337337
let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys
338338
[implicit_inputs + argument_index];
339-
if let Some(region_name) =
339+
if let Some(highlight) =
340340
self.give_name_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index)
341341
{
342-
return Some(region_name);
342+
return Some(RegionName {
343+
name: self.synthesize_region_name(),
344+
source: RegionNameSource::AnonRegionFromArgument(highlight),
345+
});
343346
}
344347

345-
self.give_name_if_we_cannot_match_hir_ty(fr, arg_ty)
348+
let counter = *self.next_region_name.try_borrow().unwrap();
349+
if let Some(highlight) = self.give_name_if_we_cannot_match_hir_ty(fr, arg_ty, counter) {
350+
Some(RegionName {
351+
// This counter value will already have been used, so this function will increment
352+
// it so the next value will be used next and return the region name that would
353+
// have been used.
354+
name: self.synthesize_region_name(),
355+
source: RegionNameSource::AnonRegionFromArgument(highlight),
356+
})
357+
} else {
358+
None
359+
}
346360
}
347361

348362
fn give_name_if_we_can_match_hir_ty_from_argument(
349363
&self,
350364
needle_fr: RegionVid,
351365
argument_ty: Ty<'tcx>,
352366
argument_index: usize,
353-
) -> Option<RegionName> {
367+
) -> Option<RegionNameHighlight> {
354368
let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id);
355369
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?;
356370
let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?;
@@ -381,8 +395,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
381395
&self,
382396
needle_fr: RegionVid,
383397
argument_ty: Ty<'tcx>,
384-
) -> Option<RegionName> {
385-
let counter = *self.next_region_name.try_borrow().unwrap();
398+
counter: usize,
399+
) -> Option<RegionNameHighlight> {
386400
let mut highlight = RegionHighlightMode::default();
387401
highlight.highlighting_region_vid(needle_fr, counter);
388402
let type_name = self.infcx.extract_type_name(&argument_ty, Some(highlight)).0;
@@ -391,7 +405,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
391405
"give_name_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
392406
type_name, needle_fr
393407
);
394-
let assigned_region_name = if type_name.find(&format!("'{}", counter)).is_some() {
408+
if type_name.find(&format!("'{}", counter)).is_some() {
395409
// Only add a label if we can confirm that a region was labelled.
396410
let argument_index =
397411
self.regioncx.get_argument_index_for_region(self.infcx.tcx, needle_fr)?;
@@ -401,20 +415,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
401415
argument_index,
402416
);
403417

404-
Some(RegionName {
405-
// This counter value will already have been used, so this function will increment
406-
// it so the next value will be used next and return the region name that would
407-
// have been used.
408-
name: self.synthesize_region_name(),
409-
source: RegionNameSource::AnonRegionFromArgument(
410-
RegionNameHighlight::CannotMatchHirTy(span, type_name),
411-
),
412-
})
418+
Some(RegionNameHighlight::CannotMatchHirTy(span, type_name))
413419
} else {
414420
None
415-
};
416-
417-
assigned_region_name
421+
}
418422
}
419423

420424
/// Attempts to highlight the specific part of a type annotation
@@ -443,7 +447,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
443447
needle_fr: RegionVid,
444448
argument_ty: Ty<'tcx>,
445449
argument_hir_ty: &hir::Ty<'_>,
446-
) -> Option<RegionName> {
450+
) -> Option<RegionNameHighlight> {
447451
let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty<'_>)> =
448452
&mut vec![(argument_ty, argument_hir_ty)];
449453

@@ -460,18 +464,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
460464
hir::TyKind::Rptr(_lifetime, referent_hir_ty),
461465
) => {
462466
if region.to_region_vid() == needle_fr {
463-
let region_name = self.synthesize_region_name();
464-
465467
// Just grab the first character, the `&`.
466468
let source_map = self.infcx.tcx.sess.source_map();
467469
let ampersand_span = source_map.start_point(hir_ty.span);
468470

469-
return Some(RegionName {
470-
name: region_name,
471-
source: RegionNameSource::AnonRegionFromArgument(
472-
RegionNameHighlight::MatchedHirTy(ampersand_span),
473-
),
474-
});
471+
return Some(RegionNameHighlight::MatchedHirTy(ampersand_span));
475472
}
476473

477474
// Otherwise, let's descend into the referent types.
@@ -491,13 +488,13 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
491488
Res::Def(DefKind::TyAlias, _) => (),
492489
_ => {
493490
if let Some(last_segment) = path.segments.last() {
494-
if let Some(name) = self.match_adt_and_segment(
491+
if let Some(highlight) = self.match_adt_and_segment(
495492
substs,
496493
needle_fr,
497494
last_segment,
498495
search_stack,
499496
) {
500-
return Some(name);
497+
return Some(highlight);
501498
}
502499
}
503500
}
@@ -540,7 +537,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
540537
needle_fr: RegionVid,
541538
last_segment: &'hir hir::PathSegment<'hir>,
542539
search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
543-
) -> Option<RegionName> {
540+
) -> Option<RegionNameHighlight> {
544541
// Did the user give explicit arguments? (e.g., `Foo<..>`)
545542
let args = last_segment.args.as_ref()?;
546543
let lifetime =
@@ -550,14 +547,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
550547
| hir::LifetimeName::Error
551548
| hir::LifetimeName::Static
552549
| hir::LifetimeName::Underscore => {
553-
let region_name = self.synthesize_region_name();
554550
let lifetime_span = lifetime.span;
555-
Some(RegionName {
556-
name: region_name,
557-
source: RegionNameSource::AnonRegionFromArgument(
558-
RegionNameHighlight::MatchedAdtAndSegment(lifetime_span),
559-
),
560-
})
551+
Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime_span))
561552
}
562553

563554
hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Implicit => {

0 commit comments

Comments
 (0)