Skip to content

Commit

Permalink
Auto merge of rust-lang#134688 - lcnr:opaque-ty-with-regions, r=<try>
Browse files Browse the repository at this point in the history
forbid defining opaque types with regions in closures

We currently only allow defining opaque types if their args are exclusively local to the closure. I would like to know whether we may change it.

r? `@compiler-errors`
  • Loading branch information
bors committed Dec 23, 2024
2 parents 85c3989 + 0728a03 commit a0cd57e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
) -> FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>> {
let mut result: FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>> = FxIndexMap::default();
Expand All @@ -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)];
Expand Down

0 comments on commit a0cd57e

Please sign in to comment.