Skip to content

Commit b7a114e

Browse files
committed
Return structurally_normalize_ty and use Unnormalized::new_wip
1 parent 1f9d710 commit b7a114e

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

  • compiler/rustc_hir_analysis/src/coherence

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use rustc_hir::ItemKind;
1010
use rustc_hir::def_id::{DefId, LocalDefId};
1111
use rustc_hir::lang_items::LangItem;
1212
use rustc_infer::infer::{self, InferCtxt, RegionResolutionError, SubregionOrigin, TyCtxtInferExt};
13-
use rustc_infer::traits::Obligation;
13+
use rustc_infer::traits::{Obligation, PredicateObligations};
1414
use rustc_middle::ty::adjustment::CoerceUnsizedInfo;
1515
use rustc_middle::ty::print::PrintTraitRefExt as _;
1616
use rustc_middle::ty::relate::solver_relating::RelateExt;
1717
use rustc_middle::ty::{
18-
self, Ty, TyCtxt, TypeVisitableExt, TypingMode, suggest_constraining_type_params,
18+
self, Ty, TyCtxt, TypeVisitableExt, TypingMode, Unnormalized, suggest_constraining_type_params,
1919
};
2020
use rustc_span::{DUMMY_SP, Span, sym};
2121
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
@@ -465,6 +465,37 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
465465
}
466466
}
467467

468+
fn structurally_normalize_ty<'tcx>(
469+
tcx: TyCtxt<'tcx>,
470+
infcx: &InferCtxt<'tcx>,
471+
impl_did: LocalDefId,
472+
span: Span,
473+
ty: Unnormalized<'tcx, Ty<'tcx>>,
474+
) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
475+
let ocx = ObligationCtxt::new(infcx);
476+
let Ok(normalized_ty) = ocx.structurally_normalize_ty(
477+
&traits::ObligationCause::misc(span, impl_did),
478+
tcx.param_env(impl_did),
479+
ty,
480+
) else {
481+
// We shouldn't have errors here in the old solver, except for
482+
// evaluate/fulfill mismatches, but that's not a reason for an ICE.
483+
return None;
484+
};
485+
let errors = ocx.try_evaluate_obligations();
486+
if !errors.is_empty() {
487+
if infcx.next_trait_solver() {
488+
unreachable!();
489+
}
490+
// We shouldn't have errors here in the old solver, except for
491+
// evaluate/fulfill mismatches, but that's not a reason for an ICE.
492+
debug!(?errors, "encountered errors while fulfilling");
493+
return None;
494+
}
495+
496+
Some((normalized_ty, ocx.into_pending_obligations()))
497+
}
498+
468499
pub(crate) fn reborrow_info<'tcx>(
469500
tcx: TyCtxt<'tcx>,
470501
impl_did: LocalDefId,
@@ -583,7 +614,15 @@ pub(crate) fn coerce_shared_info<'tcx>(
583614
}
584615

585616
assert_eq!(trait_ref.def_id, coerce_shared_trait);
586-
let target = trait_ref.args.type_at(1);
617+
let Some((target, _obligations)) = structurally_normalize_ty(
618+
tcx,
619+
&infcx,
620+
impl_did,
621+
span,
622+
Unnormalized::new_wip(trait_ref.args.type_at(1)),
623+
) else {
624+
todo!("something went wrong with structurally_normalize_ty");
625+
};
587626

588627
let param_env = tcx.param_env(impl_did);
589628
assert!(!source.has_escaping_bound_vars());

0 commit comments

Comments
 (0)