Skip to content

Use infcx.partially_normalize_associated_types_in #104835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use rustc_span::lev_distance::{
use rustc_span::symbol::sym;
use rustc_span::{symbol::Ident, Span, Symbol, DUMMY_SP};
use rustc_trait_selection::autoderef::{self, Autoderef};
use rustc_trait_selection::infer::InferCtxtExt as _;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
use rustc_trait_selection::traits::query::method_autoderef::MethodAutoderefBadTy;
use rustc_trait_selection::traits::query::method_autoderef::{
Expand Down Expand Up @@ -716,9 +717,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
// maybe shouldn't include `Param`s, but rather fresh variables or be canonicalized,
// see issue #89650
let cause = traits::ObligationCause::misc(self.span, self.body_id);
let selcx = &mut traits::SelectionContext::new(self.fcx);
let traits::Normalized { value: xform_self_ty, obligations } =
traits::normalize(selcx, self.param_env, cause, xform_self_ty);
let InferOk { value: xform_self_ty, obligations } = self
.fcx
.partially_normalize_associated_types_in(cause, self.param_env, xform_self_ty);

debug!(
"assemble_inherent_impl_probe after normalization: xform_self_ty = {:?}/{:?}",
xform_self_ty, xform_ret_ty
Expand Down Expand Up @@ -1490,7 +1492,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
let mut xform_ret_ty = probe.xform_ret_ty;
debug!(?xform_ret_ty);

let selcx = &mut traits::SelectionContext::new(self);
let cause = traits::ObligationCause::misc(self.span, self.body_id);

let mut parent_pred = None;
Expand All @@ -1504,19 +1505,28 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
// `xform_ret_ty` hasn't been normalized yet, only `xform_self_ty`,
// see the reasons mentioned in the comments in `assemble_inherent_impl_probe`
// for why this is necessary
let traits::Normalized {
let InferOk {
value: normalized_xform_ret_ty,
obligations: normalization_obligations,
} = traits::normalize(selcx, self.param_env, cause.clone(), probe.xform_ret_ty);
} = self.fcx.partially_normalize_associated_types_in(
cause.clone(),
self.param_env,
probe.xform_ret_ty,
);
xform_ret_ty = normalized_xform_ret_ty;
debug!("xform_ret_ty after normalization: {:?}", xform_ret_ty);

// Check whether the impl imposes obligations we have to worry about.
let impl_def_id = probe.item.container_id(self.tcx);
let impl_bounds = self.tcx.predicates_of(impl_def_id);
let impl_bounds = impl_bounds.instantiate(self.tcx, substs);
let traits::Normalized { value: impl_bounds, obligations: norm_obligations } =
traits::normalize(selcx, self.param_env, cause.clone(), impl_bounds);

let InferOk { value: impl_bounds, obligations: norm_obligations } =
self.fcx.partially_normalize_associated_types_in(
cause.clone(),
self.param_env,
impl_bounds,
);

// Convert the bounds into obligations.
let impl_obligations = traits::predicates_for_generics(
Expand Down
18 changes: 8 additions & 10 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::{
};
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::InferCtxtExt as _;
use crate::infer::{self, InferCtxt, TyCtxtInferExt};
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use crate::traits::query::normalize::AtExt as _;
Expand All @@ -28,7 +29,7 @@ use rustc_hir::GenericParam;
use rustc_hir::Item;
use rustc_hir::Node;
use rustc_infer::infer::error_reporting::TypeErrCtxt;
use rustc_infer::infer::TypeTrace;
use rustc_infer::infer::{InferOk, TypeTrace};
use rustc_middle::traits::select::OverflowError;
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
use rustc_middle::ty::error::ExpectedFound;
Expand Down Expand Up @@ -2528,18 +2529,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
}

self.probe(|_| {
let mut selcx = SelectionContext::new(self);

let cleaned_pred =
pred.fold_with(&mut ParamToVarFolder { infcx: self, var_map: Default::default() });

let cleaned_pred = super::project::normalize(
&mut selcx,
param_env,
ObligationCause::dummy(),
cleaned_pred,
)
.value;
let InferOk { value: cleaned_pred, .. } =
self.infcx.partially_normalize_associated_types_in(
ObligationCause::dummy(),
param_env,
cleaned_pred,
);

let obligation =
Obligation::new(self.tcx, ObligationCause::dummy(), param_env, cleaned_pred);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use super::{
DefIdOrName, Obligation, ObligationCause, ObligationCauseCode, PredicateObligation,
SelectionContext,
};
use super::{DefIdOrName, Obligation, ObligationCause, ObligationCauseCode, PredicateObligation};

use crate::autoderef::Autoderef;
use crate::infer::InferCtxt;
use crate::traits::normalize_to;

use hir::def::CtorOf;
use hir::HirId;
Expand All @@ -23,7 +19,7 @@ use rustc_hir::lang_items::LangItem;
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node};
use rustc_infer::infer::error_reporting::TypeErrCtxt;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::LateBoundRegionConversionTime;
use rustc_infer::infer::{InferOk, LateBoundRegionConversionTime};
use rustc_middle::hir::map;
use rustc_middle::ty::{
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree,
Expand Down Expand Up @@ -2986,13 +2982,12 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.tcx.mk_substs_trait(trait_pred.self_ty(), []),
)
});
let projection_ty = normalize_to(
&mut SelectionContext::new(self),
obligation.param_env,
obligation.cause.clone(),
projection_ty,
&mut vec![],
);
let InferOk { value: projection_ty, .. } = self
.partially_normalize_associated_types_in(
obligation.cause.clone(),
obligation.param_env,
projection_ty,
);

debug!(
normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)
Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use rustc_hir::def_id::DefId;
use rustc_middle::ty::{self, ImplSubject, ToPredicate, Ty, TyCtxt, TypeVisitable};
use rustc_middle::ty::{GenericArg, SubstsRef};

use super::{Normalized, Obligation, ObligationCause, PredicateObligation, SelectionContext};
use super::{Obligation, ObligationCause, PredicateObligation, SelectionContext};
use crate::infer::InferCtxtExt;
use rustc_infer::infer::InferOk;
pub use rustc_infer::traits::{self, util::*};

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -200,13 +202,15 @@ pub fn impl_subject_and_oblig<'a, 'tcx>(
) -> (ImplSubject<'tcx>, impl Iterator<Item = PredicateObligation<'tcx>>) {
let subject = selcx.tcx().bound_impl_subject(impl_def_id);
let subject = subject.subst(selcx.tcx(), impl_substs);
let Normalized { value: subject, obligations: normalization_obligations1 } =
super::normalize(selcx, param_env, ObligationCause::dummy(), subject);
let InferOk { value: subject, obligations: normalization_obligations1 } = selcx
.infcx()
.partially_normalize_associated_types_in(ObligationCause::dummy(), param_env, subject);

let predicates = selcx.tcx().predicates_of(impl_def_id);
let predicates = predicates.instantiate(selcx.tcx(), impl_substs);
let Normalized { value: predicates, obligations: normalization_obligations2 } =
super::normalize(selcx, param_env, ObligationCause::dummy(), predicates);
let InferOk { value: predicates, obligations: normalization_obligations2 } = selcx
.infcx()
.partially_normalize_associated_types_in(ObligationCause::dummy(), param_env, predicates);
let impl_obligations =
super::predicates_for_generics(|_, _| ObligationCause::dummy(), param_env, predicates);

Expand Down