Skip to content

Commit a0cd57e

Browse files
committed
Auto merge of rust-lang#134688 - lcnr:opaque-ty-with-regions, r=<try>
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`
2 parents 85c3989 + 0728a03 commit a0cd57e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

compiler/rustc_borrowck/src/nll.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ pub(crate) fn compute_regions<'a, 'tcx>(
179179
infcx.set_tainted_by_errors(guar);
180180
}
181181

182-
let remapped_opaque_tys = regioncx.infer_opaque_types(infcx, opaque_type_values);
182+
let remapped_opaque_tys = regioncx.infer_opaque_types(
183+
infcx,
184+
infcx.tcx.is_typeck_child(body.source.def_id()),
185+
opaque_type_values,
186+
);
183187

184188
NllOutput {
185189
regioncx,

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
6565
pub(crate) fn infer_opaque_types(
6666
&self,
6767
infcx: &InferCtxt<'tcx>,
68+
is_typeck_child: bool,
6869
opaque_ty_decls: FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>>,
6970
) -> FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>> {
7071
let mut result: FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>> = FxIndexMap::default();
@@ -73,6 +74,17 @@ impl<'tcx> RegionInferenceContext<'tcx> {
7374

7475
for (opaque_type_key, concrete_type) in opaque_ty_decls {
7576
debug!(?opaque_type_key, ?concrete_type);
77+
if is_typeck_child && opaque_type_key.args.iter().any(|arg| arg.as_region().is_some()) {
78+
let guar = infcx.dcx().span_err(
79+
concrete_type.span,
80+
"defining of opaque type in closure involving regions",
81+
);
82+
result.insert(opaque_type_key.def_id, OpaqueHiddenType {
83+
span: concrete_type.span,
84+
ty: Ty::new_error(infcx.tcx, guar),
85+
});
86+
continue;
87+
}
7688

7789
let mut arg_regions: Vec<(ty::RegionVid, ty::Region<'_>)> =
7890
vec![(self.universal_regions().fr_static, infcx.tcx.lifetimes.re_static)];

0 commit comments

Comments
 (0)