Skip to content

Commit 83d8b25

Browse files
authored
Unrolled build for rust-lang#138846
Rollup merge of rust-lang#138846 - compiler-errors:stall-prereqs, r=lcnr Tweaks to writeback and `Obligation -> Goal` conversion Each of these commits are self-contained, but are prerequisites that I'd like to land before rust-lang#138845, which still needs some cleaning. The ""most controversial"" one is probably [Explicitly don't fold coroutine obligations in writeback](rust-lang@e7d27ba), which I prefer because I think using `fold_predicate` to control against not normalizing predicates seems... easy to mess up 🤔, and we could have *other things* that we don't want to normalize. Explicitly noting whether we want `resolve` to normalize is a lot clearer (and currently in writeback is limited to resolving stalled coroutine obligations), since we can attach it to a comment that explains *why*.
2 parents 7290b04 + fad34c6 commit 83d8b25

File tree

8 files changed

+49
-36
lines changed

8 files changed

+49
-36
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A utility module to inspect currently ambiguous obligations in the current context.
22
33
use rustc_infer::traits::{self, ObligationCause, PredicateObligations};
4-
use rustc_middle::traits::solve::{Goal, GoalSource};
4+
use rustc_middle::traits::solve::GoalSource;
55
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
66
use rustc_span::Span;
77
use rustc_trait_selection::solve::inspect::{
@@ -85,7 +85,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8585
root_cause: &obligation.cause,
8686
};
8787

88-
let goal = Goal::new(self.tcx, obligation.param_env, obligation.predicate);
88+
let goal = obligation.as_goal();
8989
self.visit_proof_tree(goal, &mut visitor);
9090
}
9191

compiler/rustc_hir_typeck/src/writeback.rs

+32-18
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
548548
let fcx_typeck_results = self.fcx.typeck_results.borrow();
549549
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);
550550
for (predicate, cause) in &fcx_typeck_results.coroutine_stalled_predicates {
551-
let (predicate, cause) = self.resolve((*predicate, cause.clone()), &cause.span);
551+
let (predicate, cause) =
552+
self.resolve_coroutine_predicate((*predicate, cause.clone()), &cause.span);
552553
self.typeck_results.coroutine_stalled_predicates.insert((predicate, cause));
553554
}
554555
}
@@ -730,7 +731,25 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
730731
T: TypeFoldable<TyCtxt<'tcx>>,
731732
{
732733
let value = self.fcx.resolve_vars_if_possible(value);
733-
let value = value.fold_with(&mut Resolver::new(self.fcx, span, self.body));
734+
let value = value.fold_with(&mut Resolver::new(self.fcx, span, self.body, true));
735+
assert!(!value.has_infer());
736+
737+
// We may have introduced e.g. `ty::Error`, if inference failed, make sure
738+
// to mark the `TypeckResults` as tainted in that case, so that downstream
739+
// users of the typeck results don't produce extra errors, or worse, ICEs.
740+
if let Err(guar) = value.error_reported() {
741+
self.typeck_results.tainted_by_errors = Some(guar);
742+
}
743+
744+
value
745+
}
746+
747+
fn resolve_coroutine_predicate<T>(&mut self, value: T, span: &dyn Locatable) -> T
748+
where
749+
T: TypeFoldable<TyCtxt<'tcx>>,
750+
{
751+
let value = self.fcx.resolve_vars_if_possible(value);
752+
let value = value.fold_with(&mut Resolver::new(self.fcx, span, self.body, false));
734753
assert!(!value.has_infer());
735754

736755
// We may have introduced e.g. `ty::Error`, if inference failed, make sure
@@ -774,8 +793,9 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
774793
fcx: &'cx FnCtxt<'cx, 'tcx>,
775794
span: &'cx dyn Locatable,
776795
body: &'tcx hir::Body<'tcx>,
796+
should_normalize: bool,
777797
) -> Resolver<'cx, 'tcx> {
778-
Resolver { fcx, span, body, should_normalize: fcx.next_trait_solver() }
798+
Resolver { fcx, span, body, should_normalize }
779799
}
780800

781801
fn report_error(&self, p: impl Into<ty::GenericArg<'tcx>>) -> ErrorGuaranteed {
@@ -805,10 +825,9 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
805825
T: Into<ty::GenericArg<'tcx>> + TypeSuperFoldable<TyCtxt<'tcx>> + Copy,
806826
{
807827
let tcx = self.fcx.tcx;
808-
// We must deeply normalize in the new solver, since later lints
809-
// expect that types that show up in the typeck are fully
810-
// normalized.
811-
let mut value = if self.should_normalize {
828+
// We must deeply normalize in the new solver, since later lints expect
829+
// that types that show up in the typeck are fully normalized.
830+
let mut value = if self.should_normalize && self.fcx.next_trait_solver() {
812831
let body_id = tcx.hir_body_owner_def_id(self.body.id());
813832
let cause = ObligationCause::misc(self.span.to_span(tcx), body_id);
814833
let at = self.fcx.at(&cause, self.fcx.param_env);
@@ -864,20 +883,15 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Resolver<'cx, 'tcx> {
864883
}
865884

866885
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
867-
self.handle_term(ct, ty::Const::outer_exclusive_binder, |tcx, guar| {
868-
ty::Const::new_error(tcx, guar)
869-
})
870-
.super_fold_with(self)
886+
self.handle_term(ct, ty::Const::outer_exclusive_binder, ty::Const::new_error)
871887
}
872888

873889
fn fold_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
874-
// Do not normalize predicates in the new solver. The new solver is
875-
// supposed to handle unnormalized predicates and incorrectly normalizing
876-
// them can be unsound, e.g. for `WellFormed` predicates.
877-
let prev = mem::replace(&mut self.should_normalize, false);
878-
let predicate = predicate.super_fold_with(self);
879-
self.should_normalize = prev;
880-
predicate
890+
assert!(
891+
!self.should_normalize,
892+
"normalizing predicates in writeback is not generally sound"
893+
);
894+
predicate.super_fold_with(self)
881895
}
882896
}
883897

compiler/rustc_infer/src/infer/opaque_types/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ impl<'tcx> InferCtxt<'tcx> {
246246
.eq(DefineOpaqueTypes::Yes, prev, hidden_ty)?
247247
.obligations
248248
.into_iter()
249-
// FIXME: Shuttling between obligations and goals is awkward.
250-
.map(Goal::from),
249+
.map(|obligation| obligation.as_goal()),
251250
);
252251
}
253252
}

compiler/rustc_infer/src/traits/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ pub struct Obligation<'tcx, T> {
5454
pub recursion_depth: usize,
5555
}
5656

57+
impl<'tcx, T: Copy> Obligation<'tcx, T> {
58+
pub fn as_goal(&self) -> solve::Goal<'tcx, T> {
59+
solve::Goal { param_env: self.param_env, predicate: self.predicate }
60+
}
61+
}
62+
5763
impl<'tcx, T: PartialEq> PartialEq<Obligation<'tcx, T>> for Obligation<'tcx, T> {
5864
#[inline]
5965
fn eq(&self, other: &Obligation<'tcx, T>) -> bool {
@@ -75,12 +81,6 @@ impl<T: Hash> Hash for Obligation<'_, T> {
7581
}
7682
}
7783

78-
impl<'tcx, P> From<Obligation<'tcx, P>> for solve::Goal<'tcx, P> {
79-
fn from(value: Obligation<'tcx, P>) -> Self {
80-
solve::Goal { param_env: value.param_env, predicate: value.predicate }
81-
}
82-
}
83-
8484
pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
8585
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::TraitPredicate<'tcx>>;
8686
pub type PolyTraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;

compiler/rustc_trait_selection/src/solve/delegate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
9696
) -> Option<Vec<Goal<'tcx, ty::Predicate<'tcx>>>> {
9797
crate::traits::wf::unnormalized_obligations(&self.0, param_env, arg, DUMMY_SP, CRATE_DEF_ID)
9898
.map(|obligations| {
99-
obligations.into_iter().map(|obligation| obligation.into()).collect()
99+
obligations.into_iter().map(|obligation| obligation.as_goal()).collect()
100100
})
101101
}
102102

compiler/rustc_trait_selection/src/solve/fulfill.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'tcx> ObligationStorage<'tcx> {
8080
// change.
8181
// FIXME: <https://github.com/Gankra/thin-vec/pull/66> is merged, this can be removed.
8282
self.overflowed.extend(ExtractIf::new(&mut self.pending, |o| {
83-
let goal = o.clone().into();
83+
let goal = o.as_goal();
8484
let result = <&SolverDelegate<'tcx>>::from(infcx)
8585
.evaluate_root_goal(goal, GenerateProofTree::No, o.cause.span)
8686
.0;
@@ -161,7 +161,7 @@ where
161161

162162
let mut has_changed = false;
163163
for obligation in self.obligations.unstalled_for_select() {
164-
let goal = obligation.clone().into();
164+
let goal = obligation.as_goal();
165165
let result = <&SolverDelegate<'tcx>>::from(infcx)
166166
.evaluate_root_goal(goal, GenerateProofTree::No, obligation.cause.span)
167167
.0;

compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::ty::error::{ExpectedFound, TypeError};
1010
use rustc_middle::ty::{self, Ty, TyCtxt};
1111
use rustc_middle::{bug, span_bug};
1212
use rustc_next_trait_solver::solve::{GenerateProofTree, SolverDelegateEvalExt as _};
13-
use rustc_type_ir::solve::{Goal, NoSolution};
13+
use rustc_type_ir::solve::NoSolution;
1414
use tracing::{instrument, trace};
1515

1616
use crate::solve::Certainty;
@@ -89,7 +89,7 @@ pub(super) fn fulfillment_error_for_stalled<'tcx>(
8989
let (code, refine_obligation) = infcx.probe(|_| {
9090
match <&SolverDelegate<'tcx>>::from(infcx)
9191
.evaluate_root_goal(
92-
root_obligation.clone().into(),
92+
root_obligation.as_goal(),
9393
GenerateProofTree::No,
9494
root_obligation.cause.span,
9595
)
@@ -155,7 +155,7 @@ fn find_best_leaf_obligation<'tcx>(
155155
.fudge_inference_if_ok(|| {
156156
infcx
157157
.visit_proof_tree(
158-
obligation.clone().into(),
158+
obligation.as_goal(),
159159
&mut BestObligation { obligation: obligation.clone(), consider_ambiguities },
160160
)
161161
.break_value()
@@ -245,7 +245,7 @@ impl<'tcx> BestObligation<'tcx> {
245245
{
246246
let nested_goal = candidate.instantiate_proof_tree_for_nested_goal(
247247
GoalSource::Misc,
248-
Goal::new(infcx.tcx, obligation.param_env, obligation.predicate),
248+
obligation.as_goal(),
249249
self.span(),
250250
);
251251
// Skip nested goals that aren't the *reason* for our goal's failure.

compiler/rustc_trait_selection/src/traits/coherence.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ fn compute_intercrate_ambiguity_causes<'tcx>(
625625
let mut causes: FxIndexSet<IntercrateAmbiguityCause<'tcx>> = Default::default();
626626

627627
for obligation in obligations {
628-
search_ambiguity_causes(infcx, obligation.clone().into(), &mut causes);
628+
search_ambiguity_causes(infcx, obligation.as_goal(), &mut causes);
629629
}
630630

631631
causes

0 commit comments

Comments
 (0)