Skip to content

Commit 2c8bbee

Browse files
Remove fully_normalize
1 parent 0c81f94 commit 2c8bbee

File tree

4 files changed

+36
-74
lines changed

4 files changed

+36
-74
lines changed

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use rustc_middle::ty::print::{PrintTraitPredicateExt as _, TraitPredPrintModifie
55
use rustc_middle::ty::{self, fold::BottomUpFolder, Ty, TypeFoldable};
66
use rustc_session::{declare_lint, declare_lint_pass};
77
use rustc_span::{symbol::kw, Span};
8-
use rustc_trait_selection::traits;
9-
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
8+
use rustc_trait_selection::traits::{self, ObligationCtxt};
109

1110
use crate::{LateContext, LateLintPass, LintContext};
1211

@@ -130,24 +129,26 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
130129
.iter_instantiated_copied(cx.tcx, proj.projection_term.args)
131130
{
132131
let assoc_pred = assoc_pred.fold_with(proj_replacer);
133-
let Ok(assoc_pred) = traits::fully_normalize(
134-
infcx,
132+
133+
let ocx = ObligationCtxt::new(infcx);
134+
let assoc_pred =
135+
ocx.normalize(&traits::ObligationCause::dummy(), cx.param_env, assoc_pred);
136+
if !ocx.select_all_or_error().is_empty() {
137+
// Can't normalize for some reason...?
138+
continue;
139+
}
140+
141+
ocx.register_obligation(traits::Obligation::new(
142+
cx.tcx,
135143
traits::ObligationCause::dummy(),
136144
cx.param_env,
137145
assoc_pred,
138-
) else {
139-
continue;
140-
};
146+
));
141147

142148
// If that predicate doesn't hold modulo regions (but passed during type-check),
143149
// then we must've taken advantage of the hack in `project_and_unify_types` where
144150
// we replace opaques with inference vars. Emit a warning!
145-
if !infcx.predicate_must_hold_modulo_regions(&traits::Obligation::new(
146-
cx.tcx,
147-
traits::ObligationCause::dummy(),
148-
cx.param_env,
149-
assoc_pred,
150-
)) {
151+
if !ocx.select_all_or_error().is_empty() {
151152
// If it's a trait bound and an opaque that doesn't satisfy it,
152153
// then we can emit a suggestion to add the bound.
153154
let add_bound = match (proj_term.kind(), assoc_pred.kind().skip_binder()) {

compiler/rustc_trait_selection/src/traits/mod.rs

+8-38
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,14 @@ fn do_normalize_predicates<'tcx>(
271271
// them here too, and we will remove this function when
272272
// we move over to lazy normalization *anyway*.
273273
let infcx = tcx.infer_ctxt().ignoring_regions().build();
274-
let predicates = match fully_normalize(&infcx, cause, elaborated_env, predicates) {
275-
Ok(predicates) => predicates,
276-
Err(errors) => {
277-
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
278-
return Err(reported);
279-
}
280-
};
274+
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
275+
let predicates = ocx.normalize(&cause, elaborated_env, predicates);
276+
277+
let errors = ocx.select_all_or_error();
278+
if !errors.is_empty() {
279+
let reported = infcx.err_ctxt().report_fulfillment_errors(errors);
280+
return Err(reported);
281+
}
281282

282283
debug!("do_normalize_predicates: normalized predicates = {:?}", predicates);
283284

@@ -465,37 +466,6 @@ pub fn normalize_param_env_or_error<'tcx>(
465466
ty::ParamEnv::new(tcx.mk_clauses(&predicates), unnormalized_env.reveal())
466467
}
467468

468-
/// Normalize a type and process all resulting obligations, returning any errors.
469-
///
470-
/// FIXME(-Znext-solver): This should be replaced by `At::deeply_normalize`
471-
/// which has the same behavior with the new solver. Because using a separate
472-
/// fulfillment context worsens caching in the old solver, `At::deeply_normalize`
473-
/// is still lazy with the old solver as it otherwise negatively impacts perf.
474-
#[instrument(skip_all)]
475-
pub fn fully_normalize<'tcx, T>(
476-
infcx: &InferCtxt<'tcx>,
477-
cause: ObligationCause<'tcx>,
478-
param_env: ty::ParamEnv<'tcx>,
479-
value: T,
480-
) -> Result<T, Vec<FulfillmentError<'tcx>>>
481-
where
482-
T: TypeFoldable<TyCtxt<'tcx>>,
483-
{
484-
let ocx = ObligationCtxt::new_with_diagnostics(infcx);
485-
debug!(?value);
486-
let normalized_value = ocx.normalize(&cause, param_env, value);
487-
debug!(?normalized_value);
488-
debug!("select_all_or_error start");
489-
let errors = ocx.select_all_or_error();
490-
if !errors.is_empty() {
491-
return Err(errors);
492-
}
493-
debug!("select_all_or_error complete");
494-
let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
495-
debug!(?resolved_value);
496-
Ok(resolved_value)
497-
}
498-
499469
/// Normalizes the predicates and checks whether they hold in an empty environment. If this
500470
/// returns true, then either normalize encountered an error or one of the predicates did not
501471
/// hold. Used when creating vtables to check for unsatisfiable methods.

compiler/rustc_trait_selection/src/traits/normalize.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ impl<'tcx> At<'_, 'tcx> {
4242
/// same goals in both a temporary and the shared context which negatively impacts
4343
/// performance as these don't share caching.
4444
///
45-
/// FIXME(-Znext-solver): This has the same behavior as `traits::fully_normalize`
46-
/// in the new solver, but because of performance reasons, we currently reuse an
47-
/// existing fulfillment context in the old solver. Once we also eagerly prove goals with
48-
/// the old solver or have removed the old solver, remove `traits::fully_normalize` and
49-
/// rename this function to `At::fully_normalize`.
45+
/// FIXME(-Znext-solver): For performance reasons, we currently reuse an existing
46+
/// fulfillment context in the old solver. Once we have removed the old solver, we
47+
/// can remove the `fulfill_cx` parameter on this function.
5048
fn deeply_normalize<T, E>(
5149
self,
5250
value: T,

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ use crate::error_reporting::traits::to_pretty_impl_header;
1818
use crate::errors::NegativePositiveConflict;
1919
use crate::infer::{InferCtxt, InferOk, TyCtxtInferExt};
2020
use crate::traits::select::IntercrateAmbiguityCause;
21-
use crate::traits::{
22-
self, coherence, FutureCompatOverlapErrorKind, ObligationCause, ObligationCtxt,
23-
};
21+
use crate::traits::{coherence, FutureCompatOverlapErrorKind, ObligationCause, ObligationCtxt};
2422
use rustc_data_structures::fx::FxIndexSet;
2523
use rustc_errors::{codes::*, Diag, EmissionGuarantee};
2624
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -219,19 +217,17 @@ fn fulfill_implication<'tcx>(
219217
param_env, source_trait_ref, target_impl
220218
);
221219

222-
let source_trait_ref =
223-
match traits::fully_normalize(infcx, ObligationCause::dummy(), param_env, source_trait_ref)
224-
{
225-
Ok(source_trait_ref) => source_trait_ref,
226-
Err(_errors) => {
227-
infcx.dcx().span_delayed_bug(
228-
infcx.tcx.def_span(source_impl),
229-
format!("failed to fully normalize {source_trait_ref}"),
230-
);
231-
source_trait_ref
232-
}
233-
};
220+
let ocx = ObligationCtxt::new(infcx);
221+
let source_trait_ref = ocx.normalize(&ObligationCause::dummy(), param_env, source_trait_ref);
234222

223+
if !ocx.select_all_or_error().is_empty() {
224+
infcx.dcx().span_delayed_bug(
225+
infcx.tcx.def_span(source_impl),
226+
format!("failed to fully normalize {source_trait_ref}"),
227+
);
228+
}
229+
230+
let source_trait_ref = infcx.resolve_vars_if_possible(source_trait_ref);
235231
let source_trait = ImplSubject::Trait(source_trait_ref);
236232

237233
let selcx = SelectionContext::new(infcx);
@@ -253,9 +249,6 @@ fn fulfill_implication<'tcx>(
253249
return Err(());
254250
};
255251

256-
// Needs to be `in_snapshot` because this function is used to rebase
257-
// generic parameters, which may happen inside of a select within a probe.
258-
let ocx = ObligationCtxt::new(infcx);
259252
// attempt to prove all of the predicates for impl2 given those for impl1
260253
// (which are packed up in penv)
261254
ocx.register_obligations(obligations.chain(more_obligations));

0 commit comments

Comments
 (0)