Skip to content

Commit 5988078

Browse files
committed
Restrict diagnostic context lifetime of InferCtxt to itself instead of TyCtxt
1 parent 79ac898 commit 5988078

File tree

12 files changed

+79
-53
lines changed

12 files changed

+79
-53
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+45-24
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ trait TypeOpInfo<'tcx> {
149149

150150
fn base_universe(&self) -> ty::UniverseIndex;
151151

152-
fn nice_error(
152+
fn nice_error<'cx>(
153153
&self,
154-
mbcx: &mut MirBorrowckCtxt<'_, '_, '_, 'tcx>,
154+
mbcx: &mut MirBorrowckCtxt<'_, '_, 'cx, 'tcx>,
155155
cause: ObligationCause<'tcx>,
156156
placeholder_region: ty::Region<'tcx>,
157157
error_region: Option<ty::Region<'tcx>>,
158-
) -> Option<Diag<'tcx>>;
158+
) -> Option<Diag<'cx>>;
159159

160160
#[instrument(level = "debug", skip(self, mbcx))]
161161
fn report_error(
@@ -231,18 +231,25 @@ impl<'tcx> TypeOpInfo<'tcx> for PredicateQuery<'tcx> {
231231
self.base_universe
232232
}
233233

234-
fn nice_error(
234+
fn nice_error<'cx>(
235235
&self,
236-
mbcx: &mut MirBorrowckCtxt<'_, '_, '_, 'tcx>,
236+
mbcx: &mut MirBorrowckCtxt<'_, '_, 'cx, 'tcx>,
237237
cause: ObligationCause<'tcx>,
238238
placeholder_region: ty::Region<'tcx>,
239239
error_region: Option<ty::Region<'tcx>>,
240-
) -> Option<Diag<'tcx>> {
240+
) -> Option<Diag<'cx>> {
241241
let (infcx, key, _) =
242242
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
243243
let ocx = ObligationCtxt::new(&infcx);
244244
type_op_prove_predicate_with_cause(&ocx, key, cause);
245-
try_extract_error_from_fulfill_cx(&ocx, mbcx.mir_def_id(), placeholder_region, error_region)
245+
let diag = try_extract_error_from_fulfill_cx(
246+
&ocx,
247+
mbcx.mir_def_id(),
248+
placeholder_region,
249+
error_region,
250+
)?
251+
.with_dcx(mbcx.dcx());
252+
Some(diag)
246253
}
247254
}
248255

@@ -268,13 +275,13 @@ where
268275
self.base_universe
269276
}
270277

271-
fn nice_error(
278+
fn nice_error<'cx>(
272279
&self,
273-
mbcx: &mut MirBorrowckCtxt<'_, '_, '_, 'tcx>,
280+
mbcx: &mut MirBorrowckCtxt<'_, '_, 'cx, 'tcx>,
274281
cause: ObligationCause<'tcx>,
275282
placeholder_region: ty::Region<'tcx>,
276283
error_region: Option<ty::Region<'tcx>>,
277-
) -> Option<Diag<'tcx>> {
284+
) -> Option<Diag<'cx>> {
278285
let (infcx, key, _) =
279286
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
280287
let ocx = ObligationCtxt::new(&infcx);
@@ -288,7 +295,14 @@ where
288295
let (param_env, value) = key.into_parts();
289296
let _ = ocx.normalize(&cause, param_env, value.value);
290297

291-
try_extract_error_from_fulfill_cx(&ocx, mbcx.mir_def_id(), placeholder_region, error_region)
298+
let diag = try_extract_error_from_fulfill_cx(
299+
&ocx,
300+
mbcx.mir_def_id(),
301+
placeholder_region,
302+
error_region,
303+
)?
304+
.with_dcx(mbcx.dcx());
305+
Some(diag)
292306
}
293307
}
294308

@@ -308,18 +322,25 @@ impl<'tcx> TypeOpInfo<'tcx> for AscribeUserTypeQuery<'tcx> {
308322
self.base_universe
309323
}
310324

311-
fn nice_error(
325+
fn nice_error<'cx>(
312326
&self,
313-
mbcx: &mut MirBorrowckCtxt<'_, '_, '_, 'tcx>,
327+
mbcx: &mut MirBorrowckCtxt<'_, '_, 'cx, 'tcx>,
314328
cause: ObligationCause<'tcx>,
315329
placeholder_region: ty::Region<'tcx>,
316330
error_region: Option<ty::Region<'tcx>>,
317-
) -> Option<Diag<'tcx>> {
331+
) -> Option<Diag<'cx>> {
318332
let (infcx, key, _) =
319333
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
320334
let ocx = ObligationCtxt::new(&infcx);
321335
type_op_ascribe_user_type_with_span(&ocx, key, Some(cause.span)).ok()?;
322-
try_extract_error_from_fulfill_cx(&ocx, mbcx.mir_def_id(), placeholder_region, error_region)
336+
let diag = try_extract_error_from_fulfill_cx(
337+
&ocx,
338+
mbcx.mir_def_id(),
339+
placeholder_region,
340+
error_region,
341+
)?
342+
.with_dcx(mbcx.dcx());
343+
Some(diag)
323344
}
324345
}
325346

@@ -334,13 +355,13 @@ impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
334355
self.base_universe.unwrap()
335356
}
336357

337-
fn nice_error(
358+
fn nice_error<'cx>(
338359
&self,
339-
mbcx: &mut MirBorrowckCtxt<'_, '_, '_, 'tcx>,
360+
mbcx: &mut MirBorrowckCtxt<'_, '_, 'cx, 'tcx>,
340361
_cause: ObligationCause<'tcx>,
341362
placeholder_region: ty::Region<'tcx>,
342363
error_region: Option<ty::Region<'tcx>>,
343-
) -> Option<Diag<'tcx>> {
364+
) -> Option<Diag<'cx>> {
344365
try_extract_error_from_region_constraints(
345366
mbcx.infcx,
346367
mbcx.mir_def_id(),
@@ -358,12 +379,12 @@ impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
358379
}
359380

360381
#[instrument(skip(ocx), level = "debug")]
361-
fn try_extract_error_from_fulfill_cx<'tcx>(
362-
ocx: &ObligationCtxt<'_, 'tcx>,
382+
fn try_extract_error_from_fulfill_cx<'a, 'tcx>(
383+
ocx: &ObligationCtxt<'a, 'tcx>,
363384
generic_param_scope: LocalDefId,
364385
placeholder_region: ty::Region<'tcx>,
365386
error_region: Option<ty::Region<'tcx>>,
366-
) -> Option<Diag<'tcx>> {
387+
) -> Option<Diag<'a>> {
367388
// We generally shouldn't have errors here because the query was
368389
// already run, but there's no point using `span_delayed_bug`
369390
// when we're going to emit an error here anyway.
@@ -381,15 +402,15 @@ fn try_extract_error_from_fulfill_cx<'tcx>(
381402
}
382403

383404
#[instrument(level = "debug", skip(infcx, region_var_origin, universe_of_region))]
384-
fn try_extract_error_from_region_constraints<'tcx>(
385-
infcx: &InferCtxt<'tcx>,
405+
fn try_extract_error_from_region_constraints<'a, 'tcx>(
406+
infcx: &'a InferCtxt<'tcx>,
386407
generic_param_scope: LocalDefId,
387408
placeholder_region: ty::Region<'tcx>,
388409
error_region: Option<ty::Region<'tcx>>,
389410
region_constraints: &RegionConstraintData<'tcx>,
390411
mut region_var_origin: impl FnMut(RegionVid) -> RegionVariableOrigin,
391412
mut universe_of_region: impl FnMut(RegionVid) -> UniverseIndex,
392-
) -> Option<Diag<'tcx>> {
413+
) -> Option<Diag<'a>> {
393414
let placeholder_universe = match placeholder_region.kind() {
394415
ty::RePlaceholder(p) => p.universe,
395416
ty::ReVar(vid) => universe_of_region(vid),

compiler/rustc_errors/src/diagnostic.rs

+5
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
582582
Self::new_diagnostic(dcx, DiagInner::new(level, message))
583583
}
584584

585+
/// Allow moving diagnostics between different error tainting contexts
586+
pub fn with_dcx(mut self, dcx: DiagCtxtHandle<'_>) -> Diag<'_, G> {
587+
Diag { dcx, diag: self.diag.take(), _marker: PhantomData }
588+
}
589+
585590
/// Creates a new `Diag` with an already constructed diagnostic.
586591
#[track_caller]
587592
pub(crate) fn new_diagnostic(dcx: DiagCtxtHandle<'a>, diag: DiagInner) -> Self {

compiler/rustc_hir_typeck/src/coercion.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1797,16 +1797,16 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
17971797
err.subdiagnostic(SuggestBoxingForReturnImplTrait::BoxReturnExpr { starts, ends });
17981798
}
17991799

1800-
fn report_return_mismatched_types<'a>(
1800+
fn report_return_mismatched_types<'cx>(
18011801
&self,
18021802
cause: &ObligationCause<'tcx>,
18031803
expected: Ty<'tcx>,
18041804
found: Ty<'tcx>,
18051805
ty_err: TypeError<'tcx>,
1806-
fcx: &FnCtxt<'a, 'tcx>,
1806+
fcx: &'cx FnCtxt<'_, 'tcx>,
18071807
block_or_return_id: hir::HirId,
18081808
expression: Option<&'tcx hir::Expr<'tcx>>,
1809-
) -> Diag<'a> {
1809+
) -> Diag<'cx> {
18101810
let mut err = fcx.err_ctxt().report_mismatched_types(cause, expected, found, ty_err);
18111811

18121812
let due_to_block = matches!(fcx.tcx.hir_node(block_or_return_id), hir::Node::Block(..));

compiler/rustc_hir_typeck/src/demand.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
172172
}
173173

174174
pub fn demand_suptype_diag(
175-
&self,
175+
&'a self,
176176
sp: Span,
177177
expected: Ty<'tcx>,
178178
actual: Ty<'tcx>,
179-
) -> Result<(), Diag<'tcx>> {
179+
) -> Result<(), Diag<'a>> {
180180
self.demand_suptype_with_origin(&self.misc(sp), expected, actual)
181181
}
182182

183183
#[instrument(skip(self), level = "debug")]
184184
pub fn demand_suptype_with_origin(
185-
&self,
185+
&'a self,
186186
cause: &ObligationCause<'tcx>,
187187
expected: Ty<'tcx>,
188188
actual: Ty<'tcx>,
189-
) -> Result<(), Diag<'tcx>> {
189+
) -> Result<(), Diag<'a>> {
190190
self.at(cause, self.param_env)
191191
.sup(DefineOpaqueTypes::Yes, expected, actual)
192192
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
@@ -200,20 +200,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
200200
}
201201

202202
pub fn demand_eqtype_diag(
203-
&self,
203+
&'a self,
204204
sp: Span,
205205
expected: Ty<'tcx>,
206206
actual: Ty<'tcx>,
207-
) -> Result<(), Diag<'tcx>> {
207+
) -> Result<(), Diag<'a>> {
208208
self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
209209
}
210210

211211
pub fn demand_eqtype_with_origin(
212-
&self,
212+
&'a self,
213213
cause: &ObligationCause<'tcx>,
214214
expected: Ty<'tcx>,
215215
actual: Ty<'tcx>,
216-
) -> Result<(), Diag<'tcx>> {
216+
) -> Result<(), Diag<'a>> {
217217
self.at(cause, self.param_env)
218218
.eq(DefineOpaqueTypes::Yes, expected, actual)
219219
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
@@ -247,13 +247,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
247247
/// will be permitted if the diverges flag is currently "always".
248248
#[instrument(level = "debug", skip(self, expr, expected_ty_expr, allow_two_phase))]
249249
pub fn demand_coerce_diag(
250-
&self,
250+
&'a self,
251251
mut expr: &'tcx hir::Expr<'tcx>,
252252
checked_ty: Ty<'tcx>,
253253
expected: Ty<'tcx>,
254254
mut expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
255255
allow_two_phase: AllowTwoPhase,
256-
) -> Result<Ty<'tcx>, Diag<'tcx>> {
256+
) -> Result<Ty<'tcx>, Diag<'a>> {
257257
let expected = self.resolve_vars_with_obligations(expected);
258258

259259
let e = match self.coerce(expr, checked_ty, expected, allow_two_phase, None) {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14241424
expected_ty: Ty<'tcx>,
14251425
provided_ty: Ty<'tcx>,
14261426
arg: &hir::Expr<'tcx>,
1427-
err: &mut Diag<'tcx>,
1427+
err: &mut Diag<'_>,
14281428
) {
14291429
if let ty::RawPtr(_, hir::Mutability::Mut) = expected_ty.kind()
14301430
&& let ty::RawPtr(_, hir::Mutability::Not) = provided_ty.kind()

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
145145
}
146146

147147
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'a> {
148-
self.infcx.dcx()
148+
self.root_ctxt.infcx.dcx()
149149
}
150150

151151
pub fn cause(&self, span: Span, code: ObligationCauseCode<'tcx>) -> ObligationCause<'tcx> {

compiler/rustc_hir_typeck/src/pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct PatInfo<'tcx, 'a> {
8989
current_depth: u32,
9090
}
9191

92-
impl<'tcx> FnCtxt<'_, 'tcx> {
92+
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9393
fn pattern_cause(&self, ti: &TopInfo<'tcx>, cause_span: Span) -> ObligationCause<'tcx> {
9494
let code = ObligationCauseCode::Pattern {
9595
span: ti.span,
@@ -100,12 +100,12 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
100100
}
101101

102102
fn demand_eqtype_pat_diag(
103-
&self,
103+
&'a self,
104104
cause_span: Span,
105105
expected: Ty<'tcx>,
106106
actual: Ty<'tcx>,
107107
ti: &TopInfo<'tcx>,
108-
) -> Result<(), Diag<'tcx>> {
108+
) -> Result<(), Diag<'a>> {
109109
self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)
110110
.map_err(|mut diag| {
111111
if let Some(expr) = ti.origin_expr {

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl<'tcx> InferCtxt<'tcx> {
436436
}
437437
}
438438

439-
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
439+
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
440440
pub fn report_region_errors(
441441
&self,
442442
generic_param_scope: LocalDefId,
@@ -2206,7 +2206,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22062206
&self,
22072207
trace: TypeTrace<'tcx>,
22082208
terr: TypeError<'tcx>,
2209-
) -> Diag<'tcx> {
2209+
) -> Diag<'a> {
22102210
debug!("report_and_explain_type_error(trace={:?}, terr={:?})", trace, terr);
22112211

22122212
let span = trace.cause.span();

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl<'tcx> InferCtxt<'tcx> {
391391
span: Span,
392392
arg_data: InferenceDiagnosticsData,
393393
error_code: TypeAnnotationNeeded,
394-
) -> Diag<'tcx> {
394+
) -> Diag<'_> {
395395
let source_kind = "other";
396396
let source_name = "";
397397
let failure_span = None;
@@ -453,7 +453,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
453453
// If we don't have any typeck results we're outside
454454
// of a body, so we won't be able to get better info
455455
// here.
456-
return self.bad_inference_failure_err(failure_span, arg_data, error_code);
456+
return self.infcx.bad_inference_failure_err(failure_span, arg_data, error_code);
457457
};
458458

459459
let mut local_visitor = FindInferSourceVisitor::new(self, typeck_results, arg);
@@ -465,7 +465,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
465465
}
466466

467467
let Some(InferSource { span, kind }) = local_visitor.infer_source else {
468-
return self.bad_inference_failure_err(failure_span, arg_data, error_code);
468+
return self.infcx.bad_inference_failure_err(failure_span, arg_data, error_code);
469469
};
470470

471471
let (source_kind, name, path) = kind.ty_localized_msg(self);

compiler/rustc_infer/src/infer/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ impl<'tcx> InferOk<'tcx, ()> {
684684
}
685685

686686
impl<'tcx> InferCtxt<'tcx> {
687-
pub fn dcx(&self) -> DiagCtxtHandle<'tcx> {
687+
pub fn dcx(&self) -> DiagCtxtHandle<'_> {
688688
self.tcx.dcx()
689689
}
690690

@@ -1646,7 +1646,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16461646
expected: Ty<'tcx>,
16471647
actual: Ty<'tcx>,
16481648
err: TypeError<'tcx>,
1649-
) -> Diag<'tcx> {
1649+
) -> Diag<'a> {
16501650
self.report_and_explain_type_error(TypeTrace::types(cause, true, expected, actual), err)
16511651
}
16521652

@@ -1656,7 +1656,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16561656
expected: ty::Const<'tcx>,
16571657
actual: ty::Const<'tcx>,
16581658
err: TypeError<'tcx>,
1659-
) -> Diag<'tcx> {
1659+
) -> Diag<'a> {
16601660
self.report_and_explain_type_error(TypeTrace::consts(cause, true, expected, actual), err)
16611661
}
16621662
}

compiler/rustc_trait_selection/src/traits/error_reporting/infer_ctxt_ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'tcx> InferCtxt<'tcx> {
8888
found_args: Vec<ArgKind>,
8989
is_closure: bool,
9090
closure_arg_span: Option<Span>,
91-
) -> Diag<'tcx> {
91+
) -> Diag<'_> {
9292
let kind = if is_closure { "closure" } else { "function" };
9393

9494
let args_str = |arguments: &[ArgKind], other: &[ArgKind]| {

0 commit comments

Comments
 (0)