Skip to content

Commit 662df1e

Browse files
authored
Rollup merge of #104206 - compiler-errors:ocx-more-2, r=lcnr
Remove `save_and_restore_in_snapshot_flag`, use `ObligationCtxt` more r? ```@lcnr```
2 parents fcbe990 + 63217e0 commit 662df1e

File tree

4 files changed

+128
-188
lines changed

4 files changed

+128
-188
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+7-26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryRespons
1818
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
1919
use rustc_infer::infer::{InferOk, InferResult};
2020
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
21+
use rustc_middle::ty::error::TypeError;
2122
use rustc_middle::ty::fold::TypeFoldable;
2223
use rustc_middle::ty::visit::TypeVisitable;
2324
use rustc_middle::ty::{
@@ -32,9 +33,7 @@ use rustc_span::symbol::{kw, sym, Ident};
3233
use rustc_span::{Span, DUMMY_SP};
3334
use rustc_trait_selection::infer::InferCtxtExt as _;
3435
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
35-
use rustc_trait_selection::traits::{
36-
self, ObligationCause, ObligationCauseCode, TraitEngine, TraitEngineExt,
37-
};
36+
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, ObligationCtxt};
3837

3938
use std::collections::hash_map::Entry;
4039
use std::slice;
@@ -766,34 +765,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
766765

767766
let expect_args = self
768767
.fudge_inference_if_ok(|| {
768+
let ocx = ObligationCtxt::new_in_snapshot(self);
769+
769770
// Attempt to apply a subtyping relationship between the formal
770771
// return type (likely containing type variables if the function
771772
// is polymorphic) and the expected return type.
772773
// No argument expectations are produced if unification fails.
773774
let origin = self.misc(call_span);
774-
let ures = self.at(&origin, self.param_env).sup(ret_ty, formal_ret);
775-
776-
// FIXME(#27336) can't use ? here, Try::from_error doesn't default
777-
// to identity so the resulting type is not constrained.
778-
match ures {
779-
Ok(ok) => {
780-
// Process any obligations locally as much as
781-
// we can. We don't care if some things turn
782-
// out unconstrained or ambiguous, as we're
783-
// just trying to get hints here.
784-
let errors = self.save_and_restore_in_snapshot_flag(|_| {
785-
let mut fulfill = <dyn TraitEngine<'_>>::new(self.tcx);
786-
for obligation in ok.obligations {
787-
fulfill.register_predicate_obligation(self, obligation);
788-
}
789-
fulfill.select_where_possible(self)
790-
});
791-
792-
if !errors.is_empty() {
793-
return Err(());
794-
}
795-
}
796-
Err(_) => return Err(()),
775+
ocx.sup(&origin, self.param_env, ret_ty, formal_ret)?;
776+
if !ocx.select_where_possible().is_empty() {
777+
return Err(TypeError::Mismatch);
797778
}
798779

799780
// Record all the argument types, with the substitutions

compiler/rustc_infer/src/infer/mod.rs

-26
Original file line numberDiff line numberDiff line change
@@ -778,32 +778,6 @@ impl<'tcx> InferCtxt<'tcx> {
778778
}
779779
}
780780

781-
/// Clear the "currently in a snapshot" flag, invoke the closure,
782-
/// then restore the flag to its original value. This flag is a
783-
/// debugging measure designed to detect cases where we start a
784-
/// snapshot, create type variables, and register obligations
785-
/// which may involve those type variables in the fulfillment cx,
786-
/// potentially leaving "dangling type variables" behind.
787-
/// In such cases, an assertion will fail when attempting to
788-
/// register obligations, within a snapshot. Very useful, much
789-
/// better than grovelling through megabytes of `RUSTC_LOG` output.
790-
///
791-
/// HOWEVER, in some cases the flag is unhelpful. In particular, we
792-
/// sometimes create a "mini-fulfilment-cx" in which we enroll
793-
/// obligations. As long as this fulfillment cx is fully drained
794-
/// before we return, this is not a problem, as there won't be any
795-
/// escaping obligations in the main cx. In those cases, you can
796-
/// use this function.
797-
pub fn save_and_restore_in_snapshot_flag<F, R>(&self, func: F) -> R
798-
where
799-
F: FnOnce(&Self) -> R,
800-
{
801-
let flag = self.in_snapshot.replace(false);
802-
let result = func(self);
803-
self.in_snapshot.set(flag);
804-
result
805-
}
806-
807781
fn start_snapshot(&self) -> CombinedSnapshot<'tcx> {
808782
debug!("start_snapshot()");
809783

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

+24-26
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html
1111
1212
pub mod specialization_graph;
13+
use rustc_infer::traits::{TraitEngine, TraitEngineExt as _};
1314
use specialization_graph::GraphExt;
1415

1516
use crate::errors::NegativePositiveConflict;
1617
use crate::infer::{InferCtxt, InferOk, TyCtxtInferExt};
18+
use crate::traits::engine::TraitEngineExt as _;
1719
use crate::traits::select::IntercrateAmbiguityCause;
1820
use crate::traits::{self, coherence, FutureCompatOverlapErrorKind, ObligationCause};
1921
use rustc_data_structures::fx::FxIndexSet;
@@ -200,36 +202,32 @@ fn fulfill_implication<'tcx>(
200202
return Err(());
201203
};
202204

205+
// Needs to be `in_snapshot` because this function is used to rebase
206+
// substitutions, which may happen inside of a select within a probe.
207+
let mut engine = <dyn TraitEngine<'tcx>>::new_in_snapshot(infcx.tcx);
203208
// attempt to prove all of the predicates for impl2 given those for impl1
204209
// (which are packed up in penv)
210+
engine.register_predicate_obligations(infcx, obligations.chain(more_obligations));
205211

206-
infcx.save_and_restore_in_snapshot_flag(|infcx| {
207-
let errors = traits::fully_solve_obligations(&infcx, obligations.chain(more_obligations));
208-
match &errors[..] {
209-
[] => {
210-
debug!(
211-
"fulfill_implication: an impl for {:?} specializes {:?}",
212-
source_trait, target_trait
213-
);
212+
let errors = engine.select_all_or_error(infcx);
213+
if !errors.is_empty() {
214+
// no dice!
215+
debug!(
216+
"fulfill_implication: for impls on {:?} and {:?}, \
217+
could not fulfill: {:?} given {:?}",
218+
source_trait,
219+
target_trait,
220+
errors,
221+
param_env.caller_bounds()
222+
);
223+
return Err(());
224+
}
214225

215-
// Now resolve the *substitution* we built for the target earlier, replacing
216-
// the inference variables inside with whatever we got from fulfillment.
217-
Ok(infcx.resolve_vars_if_possible(target_substs))
218-
}
219-
errors => {
220-
// no dice!
221-
debug!(
222-
"fulfill_implication: for impls on {:?} and {:?}, \
223-
could not fulfill: {:?} given {:?}",
224-
source_trait,
225-
target_trait,
226-
errors,
227-
param_env.caller_bounds()
228-
);
229-
Err(())
230-
}
231-
}
232-
})
226+
debug!("fulfill_implication: an impl for {:?} specializes {:?}", source_trait, target_trait);
227+
228+
// Now resolve the *substitution* we built for the target earlier, replacing
229+
// the inference variables inside with whatever we got from fulfillment.
230+
Ok(infcx.resolve_vars_if_possible(target_substs))
233231
}
234232

235233
// Query provider for `specialization_graph_of`.

0 commit comments

Comments
 (0)