From 0728a0325dadb9a9d5a2469f5eab919464693508 Mon Sep 17 00:00:00 2001 From: lcnr Date: Mon, 23 Dec 2024 14:54:12 +0100 Subject: [PATCH] disallow opaque types with regions in closures --- compiler/rustc_borrowck/src/nll.rs | 6 +++++- .../rustc_borrowck/src/region_infer/opaque_types.rs | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_borrowck/src/nll.rs b/compiler/rustc_borrowck/src/nll.rs index abe27555b1864..f0111e75e4ad2 100644 --- a/compiler/rustc_borrowck/src/nll.rs +++ b/compiler/rustc_borrowck/src/nll.rs @@ -179,7 +179,11 @@ pub(crate) fn compute_regions<'a, 'tcx>( infcx.set_tainted_by_errors(guar); } - let remapped_opaque_tys = regioncx.infer_opaque_types(infcx, opaque_type_values); + let remapped_opaque_tys = regioncx.infer_opaque_types( + infcx, + infcx.tcx.is_typeck_child(body.source.def_id()), + opaque_type_values, + ); NllOutput { regioncx, diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs index 3e16a3ca157d2..b34a4272fcbd0 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs @@ -65,6 +65,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { pub(crate) fn infer_opaque_types( &self, infcx: &InferCtxt<'tcx>, + is_typeck_child: bool, opaque_ty_decls: FxIndexMap, OpaqueHiddenType<'tcx>>, ) -> FxIndexMap> { let mut result: FxIndexMap> = FxIndexMap::default(); @@ -73,6 +74,17 @@ impl<'tcx> RegionInferenceContext<'tcx> { for (opaque_type_key, concrete_type) in opaque_ty_decls { debug!(?opaque_type_key, ?concrete_type); + if is_typeck_child && opaque_type_key.args.iter().any(|arg| arg.as_region().is_some()) { + let guar = infcx.dcx().span_err( + concrete_type.span, + "defining of opaque type in closure involving regions", + ); + result.insert(opaque_type_key.def_id, OpaqueHiddenType { + span: concrete_type.span, + ty: Ty::new_error(infcx.tcx, guar), + }); + continue; + } let mut arg_regions: Vec<(ty::RegionVid, ty::Region<'_>)> = vec![(self.universal_regions().fr_static, infcx.tcx.lifetimes.re_static)];