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

Commit 9fd80b0

Browse files
committed
Clean up some of the error handling code
1 parent 0702a42 commit 9fd80b0

File tree

1 file changed

+54
-73
lines changed

1 file changed

+54
-73
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

Lines changed: 54 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,10 @@ impl<'tcx> UniverseInfo<'tcx> {
5757
error_element: RegionElement,
5858
cause: ObligationCause<'tcx>,
5959
) {
60-
match *self {
61-
UniverseInfo::RelateTys { expected, found } => {
62-
let err = mbcx.infcx.err_ctxt().report_mismatched_types(
63-
&cause,
64-
mbcx.infcx.param_env,
65-
expected,
66-
found,
67-
TypeError::RegionsPlaceholderMismatch,
68-
);
69-
mbcx.buffer_error(err);
70-
}
71-
UniverseInfo::TypeOp(ref type_op_info) => {
72-
type_op_info.report_erroneous_element(mbcx, placeholder, error_element, cause);
73-
}
74-
UniverseInfo::Other => {
75-
// FIXME: This error message isn't great, but it doesn't show
76-
// up in the existing UI tests. Consider investigating this
77-
// some more.
78-
mbcx.buffer_error(
79-
mbcx.dcx().create_err(HigherRankedSubtypeError { span: cause.span }),
80-
);
81-
}
60+
if let UniverseInfo::TypeOp(ref type_op_info) = *self {
61+
type_op_info.report_erroneous_element(mbcx, placeholder, error_element, cause);
62+
} else {
63+
self.report_generic_error(mbcx, cause);
8264
}
8365
}
8466

@@ -90,7 +72,18 @@ impl<'tcx> UniverseInfo<'tcx> {
9072
cause: ObligationCause<'tcx>,
9173
placeholder_b: ty::PlaceholderRegion,
9274
) {
93-
// FIXME(amandasystems) this function is now a direct copy of the one above and that's not great.
75+
if let UniverseInfo::TypeOp(ref type_op_info) = *self {
76+
type_op_info.report_placeholder_mismatch(mbcx, placeholder_a, cause, placeholder_b);
77+
} else {
78+
self.report_generic_error(mbcx, cause);
79+
}
80+
}
81+
82+
fn report_generic_error(
83+
&self,
84+
mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
85+
cause: ObligationCause<'tcx>,
86+
) {
9487
match *self {
9588
UniverseInfo::RelateTys { expected, found } => {
9689
let err = mbcx.infcx.err_ctxt().report_mismatched_types(
@@ -102,9 +95,8 @@ impl<'tcx> UniverseInfo<'tcx> {
10295
);
10396
mbcx.buffer_error(err);
10497
}
105-
UniverseInfo::TypeOp(ref type_op_info) => {
106-
// FIXME(amandasystems) maybe...I can just use the type error above?!?!?!?!
107-
type_op_info.report_placeholder_mismatch(mbcx, placeholder_a, cause, placeholder_b);
98+
UniverseInfo::TypeOp(_) => {
99+
unreachable!("This case should already have been handled!");
108100
}
109101
UniverseInfo::Other => {
110102
// FIXME: This error message isn't great, but it doesn't show
@@ -189,46 +181,42 @@ pub(crate) trait TypeOpInfo<'tcx> {
189181
cause: ObligationCause<'tcx>,
190182
placeholder_b: ty::PlaceholderRegion,
191183
) {
192-
// FIXME(amandasystems) -- this fn is a duplicate of the one below and it shouldn't be.
193-
// This really is the dumbest version of this function.
194184
let tcx = mbcx.infcx.tcx;
195-
let base_universe = self.base_universe();
196-
debug!(?base_universe);
197185

198-
let Some(adjusted_universe_a) =
199-
placeholder_a.universe.as_u32().checked_sub(base_universe.as_u32())
200-
else {
201-
mbcx.buffer_error(self.fallback_error(tcx, cause.span));
202-
return;
203-
};
204-
205-
let Some(adjusted_universe_b) =
206-
placeholder_b.universe.as_u32().checked_sub(base_universe.as_u32())
207-
else {
208-
mbcx.buffer_error(self.fallback_error(tcx, cause.span));
209-
return;
210-
};
211-
212-
let placeholder_a = ty::Region::new_placeholder(
213-
tcx,
214-
ty::Placeholder { universe: adjusted_universe_a.into(), bound: placeholder_a.bound },
215-
);
216-
217-
let placeholder_b = ty::Region::new_placeholder(
218-
tcx,
219-
ty::Placeholder { universe: adjusted_universe_b.into(), bound: placeholder_b.bound },
220-
);
186+
let placeholder_a = self.region_with_adjusted_universe(placeholder_a, tcx);
187+
let placeholder_b = self.region_with_adjusted_universe(placeholder_b, tcx);
221188

222189
debug!(?placeholder_a, ?placeholder_b);
223190

224191
let span = cause.span;
225-
// FIXME(amandasystems) -- propagate or flatten the changes and remove error_region here.
192+
// FIXME: see note in `report_erroneous_element()` below!
226193
let nice_error = self.nice_error(mbcx, cause, placeholder_a, Some(placeholder_b));
227-
// I think this should never fail?!
228194
debug!(?nice_error);
229195
mbcx.buffer_error(nice_error.unwrap_or_else(|| self.fallback_error(tcx, span)));
230196
}
231197

198+
/// Turn a placeholder region into a Region with its universe adjusted by
199+
/// the base universe.
200+
fn region_with_adjusted_universe(
201+
&self,
202+
placeholder: ty::PlaceholderRegion,
203+
tcx: TyCtxt<'tcx>,
204+
) -> ty::Region<'tcx> {
205+
let Some(adjusted_universe) =
206+
placeholder.universe.as_u32().checked_sub(self.base_universe().as_u32())
207+
else {
208+
unreachable!(
209+
"Could not adjust universe {:?} of {placeholder:?} by base universe {:?}",
210+
placeholder.universe,
211+
self.base_universe()
212+
);
213+
};
214+
ty::Region::new_placeholder(
215+
tcx,
216+
ty::Placeholder { universe: adjusted_universe.into(), bound: placeholder.bound },
217+
)
218+
}
219+
232220
#[instrument(level = "debug", skip(self, mbcx))]
233221
fn report_erroneous_element(
234222
&self,
@@ -238,28 +226,22 @@ pub(crate) trait TypeOpInfo<'tcx> {
238226
cause: ObligationCause<'tcx>,
239227
) {
240228
let tcx = mbcx.infcx.tcx;
241-
let base_universe = self.base_universe();
242-
debug!(?base_universe);
243229

244-
let Some(adjusted_universe) =
245-
placeholder.universe.as_u32().checked_sub(base_universe.as_u32())
246-
else {
247-
mbcx.buffer_error(self.fallback_error(tcx, cause.span));
248-
return;
249-
};
250-
251-
// FIXME(amandasystems) -- construct this earlier when we have
252-
// adjusted universes and pass it in rather than placeholder
253-
let placeholder_region = ty::Region::new_placeholder(
254-
tcx,
255-
ty::Placeholder { universe: adjusted_universe.into(), bound: placeholder.bound },
256-
);
230+
// FIXME: these adjusted universes are not (always) the same ones as we compute
231+
// earlier. They probably should be, but the logic downstream is complicated,
232+
// and assumes they use whatever this is.
233+
//
234+
// In fact, this function throws away a lot of interesting information that would
235+
// probably allow bypassing lots of logic downstream for a much simpler flow.
236+
let placeholder_region = self.region_with_adjusted_universe(placeholder, tcx);
257237

258238
debug!(?placeholder_region);
259239

260240
let span = cause.span;
261-
// FIXME(amandasystems) -- propagate or flatten the changes and remove error_region here.
262-
// --- I think we always fall back!!!
241+
// FIXME: it's not good that we have one variant that always sends None,
242+
// and one variant with always sends Some. We should break out these code
243+
// paths -- but the downstream code is complicated and that's not straight-
244+
// forward.
263245
let nice_error = self.nice_error(mbcx, cause, placeholder_region, None);
264246

265247
debug!(?nice_error);
@@ -286,7 +268,6 @@ impl<'tcx> TypeOpInfo<'tcx> for PredicateQuery<'tcx> {
286268
self.base_universe
287269
}
288270

289-
// NOTE(amandasystems) this is the method being executed!
290271
fn nice_error<'infcx>(
291272
&self,
292273
mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,

0 commit comments

Comments
 (0)