Repository: rust-lang/rust
File: compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs (Rust)

Code before:
                        hidden_type.span,
                        body.source.def_id().expect_local(),
                    );
                    // We need to normalize both types in the old solver before equatingt them.
                    let actual_ty = ocx.normalize(&cause, infcx.param_env, hidden_type.ty);
                    let expected_ty = ocx.normalize(&cause, infcx.param_env, expected_ty);
                    ocx.eq(&cause, infcx.param_env, actual_ty, expected_ty).map_err(|_| NoSolution)
                },
                "equating opaque types",
            ),
        ) {
            add_hidden_type(
                tcx,
                hidden_types,
                key.def_id,
                DefinitionSiteHiddenType::new_error(tcx, guar),
            );
        }
    }
    errors
}

/// We handle `UnexpectedHiddenRegion` error lazily in the next solver.
/// Hope that it can be resolved by another fully defining use, which may not exist.
/// We detect eventually unconstrained region via this function after collecting defining uses from all
/// bodies.
pub(crate) fn handle_unconstrained_hidden_type_errors<'tcx>(
    tcx: TyCtxt<'tcx>,
    hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>,
    unconstrained_hidden_type_errors: &mut Vec<UnexpectedHiddenRegion<'tcx>>,
    collect_region_constraints_results: &mut FxIndexMap<
        LocalDefId,
        CollectRegionConstraintsResult<'tcx>,
    >,
) {
    let mut unconstrained_hidden_type_errors = std::mem::take(unconstrained_hidden_type_errors);
    unconstrained_hidden_type_errors
        .retain(|unconstrained| !hidden_types.contains_key(&unconstrained.opaque_type_key.def_id));

    unconstrained_hidden_type_errors.iter().for_each(|t| {
        tcx.dcx()
            .span_delayed_bug(t.hidden_type.span, "opaque type with non-universal region args");
    });

    // `UnexpectedHiddenRegion` error contains region var which only makes sense in the
    // corresponding `infcx`.
    // So we need to insert the error to the body where it originates from.
    for error in unconstrained_hidden_type_errors {
        let (def_id, error) = error.to_error();
        let Some(result) = collect_region_constraints_results.get_mut(&def_id) else {
            unreachable!("the body should depend on opaques type if it has opaque use");

Review comment (bug):
```suggestion
/// We handle `UnexpectedHiddenRegion` error lazily in the next solver as
/// there may be a fully defining use in another body.
///
/// In case such a defining use does not exist, we register an error here.
```

Code after:
                        hidden_type.span,
                        body.source.def_id().expect_local(),
                    );
                    // We need to normalize both types in the old solver before equatingt them.
                    let actual_ty = ocx.normalize(&cause, infcx.param_env, hidden_type.ty);
                    let expected_ty = ocx.normalize(&cause, infcx.param_env, expected_ty);
                    ocx.eq(&cause, infcx.param_env, actual_ty, expected_ty).map_err(|_| NoSolution)
                },
                "equating opaque types",
            ),
        ) {
            add_hidden_type(
                tcx,
                hidden_types,
                key.def_id,
                DefinitionSiteHiddenType::new_error(tcx, guar),
            );
        }
    }
    errors
}

/// We handle `UnexpectedHiddenRegion` error lazily in the next solver as
/// there may be a fully defining use in another body.
///
/// In case such a defining use does not exist, we register an error here.
pub(crate) fn handle_unconstrained_hidden_type_errors<'tcx>(
    tcx: TyCtxt<'tcx>,
    hidden_types: &mut FxIndexMap<LocalDefId, ty::DefinitionSiteHiddenType<'tcx>>,
    unconstrained_hidden_type_errors: &mut Vec<UnexpectedHiddenRegion<'tcx>>,
    collect_region_constraints_results: &mut FxIndexMap<
        LocalDefId,
        CollectRegionConstraintsResult<'tcx>,
    >,
) {
    let mut unconstrained_hidden_type_errors = std::mem::take(unconstrained_hidden_type_errors);
    unconstrained_hidden_type_errors
        .retain(|unconstrained| !hidden_types.contains_key(&unconstrained.opaque_type_key.def_id));

    unconstrained_hidden_type_errors.iter().for_each(|t| {
        tcx.dcx()
            .span_delayed_bug(t.hidden_type.span, "opaque type with non-universal region args");
    });

    // `UnexpectedHiddenRegion` error contains region var which only makes sense in the
    // corresponding `infcx`.
    // So we need to insert the error to the body where it originates from.
    for error in unconstrained_hidden_type_errors {
        let (def_id, error) = error.to_error();
        let Some(result) = collect_region_constraints_results.get_mut(&def_id) else {
            unreachable!("the body should depend on opaques type if it has opaque use");