Skip to content

Commit f41722a

Browse files
committed
Use a Field in ConstraintCategory::ClosureUpvar
1 parent 5569757 commit f41722a

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_infer::infer::{
55
error_reporting::nice_region_error::NiceRegionError,
66
error_reporting::unexpected_hidden_region_diagnostic, NllRegionVariableOrigin,
77
};
8+
use rustc_middle::hir::place::PlaceBase;
89
use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
910
use rustc_middle::ty::subst::{InternalSubsts, Subst};
1011
use rustc_middle::ty::{self, RegionVid, Ty};
@@ -421,17 +422,26 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
421422

422423
diag.span_label(*span, message);
423424

424-
// FIXME(project-rfc-2229#48): This should store a captured_place not a hir id
425-
if let ReturnConstraint::ClosureUpvar(upvar) = kind {
425+
if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
426426
let def_id = match self.regioncx.universal_regions().defining_ty {
427427
DefiningTy::Closure(def_id, _) => def_id,
428428
ty => bug!("unexpected DefiningTy {:?}", ty),
429429
};
430430

431-
let upvar_def_span = self.infcx.tcx.hir().span(upvar);
432-
let upvar_span = self.infcx.tcx.upvars_mentioned(def_id).unwrap()[&upvar].span;
433-
diag.span_label(upvar_def_span, "variable defined here");
434-
diag.span_label(upvar_span, "variable captured here");
431+
let captured_place = &self.upvars[upvar_field.index()].place;
432+
let defined_hir = match captured_place.place.base {
433+
PlaceBase::Local(hirid) => Some(hirid),
434+
PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
435+
_ => None,
436+
};
437+
438+
if defined_hir.is_some() {
439+
let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
440+
let upvar_def_span = self.infcx.tcx.hir().span(defined_hir.unwrap());
441+
let upvar_span = upvars_map.get(&defined_hir.unwrap()).unwrap().span;
442+
diag.span_label(upvar_def_span, "variable defined here");
443+
diag.span_label(upvar_span, "variable captured here");
444+
}
435445
}
436446

437447
if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {

compiler/rustc_borrowck/src/type_check/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2530,9 +2530,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25302530
body,
25312531
);
25322532
let category = if let Some(field) = field {
2533-
let var_hir_id = self.borrowck_context.upvars[field.index()].place.get_root_variable();
2534-
// FIXME(project-rfc-2229#8): Use Place for better diagnostics
2535-
ConstraintCategory::ClosureUpvar(var_hir_id)
2533+
ConstraintCategory::ClosureUpvar(field)
25362534
} else {
25372535
ConstraintCategory::Boring
25382536
};

compiler/rustc_middle/src/mir/query.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ pub enum ConstraintCategory {
341341
/// like `Foo { field: my_val }`)
342342
Usage,
343343
OpaqueType,
344-
ClosureUpvar(hir::HirId),
344+
ClosureUpvar(Field),
345345

346346
/// A constraint from a user-written predicate
347347
/// with the provided span, written on the item
@@ -363,7 +363,7 @@ pub enum ConstraintCategory {
363363
#[derive(TyEncodable, TyDecodable, HashStable)]
364364
pub enum ReturnConstraint {
365365
Normal,
366-
ClosureUpvar(hir::HirId),
366+
ClosureUpvar(Field),
367367
}
368368

369369
/// The subject of a `ClosureOutlivesRequirement` -- that is, the thing

0 commit comments

Comments
 (0)