Skip to content

Commit 396431e

Browse files
authored
Rollup merge of #162275 - adwinwhite:half-depth, r=lcnr
Do not increase recursion depth for coroutine witness and rigid opaques when proving auto traits With deeply nested async calls, we can easily overflow evaluating auto trait goals as shown in #159228. Users have no choice but increase the per-crate `recursion_limit` which is bad for compilation time/RSS. And downstream users may encounter the same warnings when calling library async functions. We mitigate that by no longer increasing recursion depth for coroutine witness and rigid opaques when proving auto traits. See the comments for why it's okay to do so. This fix actually makes the required depth a third of what it was before for async calls. I've checked locally that `bors` and `triagebot` no longer have FCWs. r? lcnr
2 parents 734de5c + caadd68 commit 396431e

3 files changed

Lines changed: 145 additions & 28 deletions

File tree

‎compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs‎

Lines changed: 113 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -638,12 +638,119 @@ where
638638
source: GoalSource,
639639
goal: Goal<I, I::Predicate>,
640640
) -> Result<GoalEvaluation<I>, NoSolutionOrRerunNonErased> {
641-
let (normalization_nested_goals, goal_evaluation) =
642-
self.evaluate_goal_raw(source, goal, LowerAvailableDepth::Yes)?;
641+
let (normalization_nested_goals, goal_evaluation) = self.evaluate_goal_raw(source, goal)?;
643642
assert!(normalization_nested_goals.is_empty());
644643
Ok(goal_evaluation)
645644
}
646645

646+
fn evaluate_in_search_graph(
647+
&mut self,
648+
canonical_goal: I::CanonicalInput,
649+
step_kind: PathKind,
650+
) -> (Result<CanonicalResponse<I>, NoSolution>, AccessedOpaques<I>) {
651+
let increase_depth_for_nested =
652+
match canonical_goal.canonical.value.goal.predicate.kind().skip_binder() {
653+
// We don't lower the available depth for the `NormalizesTo` goal, as evaluating
654+
// it is an extra step only exists in the new solver that behaves like a function
655+
// call rather than an independent nested goal evaluation. So, decreasing the
656+
// available depth may end up regressions which hit the recursion limits for crates
657+
// compiled well with the old solver.
658+
ty::PredicateKind::NormalizesTo(_) => LowerAvailableDepth::No,
659+
// We also don't lower depth for witness and rigid opaque when proving auto traits.
660+
// This is to mitigate the overflow FCW warnings in deeply nested async calls.
661+
// See #159228.
662+
//
663+
// We're eventually going to remove the witness type so it's okay to ignore the
664+
// depth.
665+
// For rigid opaques, revealing hidden types can be viewed as normalization so it's
666+
// consistent with the `NormalizesTo` reasoning above.
667+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
668+
if self.cx().trait_is_auto(pred.trait_ref.def_id) {
669+
match pred.self_ty().kind() {
670+
ty::CoroutineWitness(..) => LowerAvailableDepth::No,
671+
ty::Alias(
672+
ty::IsRigid::Yes,
673+
ty::AliasTy { kind: ty::Opaque { def_id, .. }, .. },
674+
) => {
675+
// We only want to skip lowering depth when the proving is done via
676+
// auto trait leakage. If the goal can be proved via item bounds,
677+
// we should lower depth faithfully.
678+
//
679+
// FIXME: We can have param env candidates via TAIT or RTN.
680+
// Ideally we'd instead lower the depth for the nested goal instead.
681+
// Implementing this is a bit harder.
682+
if self
683+
.cx()
684+
.item_self_bounds(def_id.into())
685+
.skip_binder()
686+
.into_iter()
687+
.any(|bound| {
688+
bound
689+
.as_trait_clause()
690+
.is_some_and(|b| b.def_id() == pred.def_id())
691+
})
692+
{
693+
LowerAvailableDepth::Yes
694+
} else {
695+
LowerAvailableDepth::No
696+
}
697+
}
698+
ty::Bool
699+
| ty::Char
700+
| ty::Int(..)
701+
| ty::Uint(..)
702+
| ty::Float(..)
703+
| ty::Str
704+
| ty::Pat(..)
705+
| ty::FnPtr(..)
706+
| ty::Array(..)
707+
| ty::Slice(..)
708+
| ty::RawPtr(..)
709+
| ty::Never
710+
| ty::Tuple(..)
711+
| ty::UnsafeBinder(_)
712+
| ty::Param(..)
713+
| ty::Placeholder(..)
714+
| ty::Bound(..)
715+
| ty::Infer(..)
716+
| ty::Alias(_, _)
717+
| ty::Ref(_, _, _)
718+
| ty::Adt(_, _)
719+
| ty::Foreign(_)
720+
| ty::Dynamic(..)
721+
| ty::Error(_)
722+
| ty::FnDef(..)
723+
| ty::Closure(..)
724+
| ty::CoroutineClosure(..)
725+
| ty::Coroutine(..) => LowerAvailableDepth::Yes,
726+
}
727+
} else {
728+
LowerAvailableDepth::Yes
729+
}
730+
}
731+
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(_))
732+
| ty::PredicateKind::Clause(ty::ClauseKind::Projection(_))
733+
| ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(_))
734+
| ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(_))
735+
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(_, _))
736+
| ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(_))
737+
| ty::PredicateKind::Subtype(_)
738+
| ty::PredicateKind::Coerce(_)
739+
| ty::PredicateKind::DynCompatible(_)
740+
| ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_))
741+
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(_))
742+
| ty::PredicateKind::ConstEquate(_, _)
743+
| ty::PredicateKind::Ambiguous => LowerAvailableDepth::Yes,
744+
};
745+
self.search_graph.evaluate_goal(
746+
self.cx(),
747+
canonical_goal,
748+
step_kind,
749+
increase_depth_for_nested,
750+
&mut inspect::ProofTreeBuilder::new_noop(),
751+
)
752+
}
753+
647754
/// Recursively evaluates `goal`, returning the nested goals in case
648755
/// the nested goal is a `NormalizesTo` goal.
649756
///
@@ -655,7 +762,6 @@ where
655762
&mut self,
656763
source: GoalSource,
657764
goal: Goal<I, I::Predicate>,
658-
increase_depth_for_nested: LowerAvailableDepth,
659765
) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolutionOrRerunNonErased> {
660766
// We only care about one entry per `OpaqueTypeKey` here,
661767
// so we only canonicalize the lookup table and ignore
@@ -723,13 +829,8 @@ where
723829
TypingMode::ErasedNotCoherence(MayBeErased),
724830
);
725831

726-
let (canonical_result, accessed_opaques) = self.search_graph.evaluate_goal(
727-
self.cx(),
728-
canonical_goal,
729-
step_kind,
730-
increase_depth_for_nested,
731-
&mut inspect::ProofTreeBuilder::new_noop(),
732-
);
832+
let (canonical_result, accessed_opaques) =
833+
self.evaluate_in_search_graph(canonical_goal, step_kind);
733834

734835
let should_rerun = should_rerun_after_erased_canonicalization(
735836
accessed_opaques,
@@ -763,13 +864,8 @@ where
763864
let (orig_values, canonical_goal) =
764865
canonicalize_goal(self.delegate, goal, &opaque_types, typing_mode);
765866

766-
let (canonical_result, accessed_opaques) = self.search_graph.evaluate_goal(
767-
self.cx(),
768-
canonical_goal,
769-
step_kind,
770-
increase_depth_for_nested,
771-
&mut inspect::ProofTreeBuilder::new_noop(),
772-
);
867+
let (canonical_result, accessed_opaques) =
868+
self.evaluate_in_search_graph(canonical_goal, step_kind);
773869
assert!(
774870
!accessed_opaques.might_rerun(),
775871
"we run without TypingMode::ErasedNotCoherence, so opaques are available, and we don't retry if the outer typing mode is ErasedNotCoherence: {accessed_opaques:?} after {goal:?}"

‎compiler/rustc_next_trait_solver/src/solve/project_goals/mod.rs‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ mod free_alias;
33
mod inherent;
44
mod opaque_types;
55

6-
use rustc_type_ir::search_graph::LowerAvailableDepth;
76
use rustc_type_ir::solve::QueryResultOrRerunNonErased;
87
use rustc_type_ir::{self as ty, Interner, ProjectionClause};
98
use tracing::{instrument, trace};
@@ -70,16 +69,7 @@ where
7069
let (
7170
NestedNormalizationGoals(nested_goals),
7271
GoalEvaluation { goal: _, certainty, stalled_on: _, has_changed: _ },
73-
) = self.evaluate_goal_raw(
74-
GoalSource::TypeRelating,
75-
normalizes_to,
76-
// We don't lower thr available depth for this `NormalizesTo` goal, as evaluating
77-
// it is an extra step only exists in the new solver that behaves like a function
78-
// call rather than an independent nested goal evaluation. So, decreasing the
79-
// available depth may end up regressions which hit the recursion limits for crates
80-
// compiled well with the old solver.
81-
LowerAvailableDepth::No,
82-
)?;
72+
) = self.evaluate_goal_raw(GoalSource::TypeRelating, normalizes_to)?;
8373

8474
trace!(?nested_goals);
8575

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ compile-flags: -Znext-solver
2+
//@ edition: 2024
3+
//@ check-pass
4+
5+
// We don't increase recursion depth when proving auto traits for witness
6+
// and rigid opaques. This is to mitigate the FCW warnings in deeply nested
7+
// async calls. See #159228.
8+
9+
#![recursion_limit = "6"]
10+
11+
async fn foo1() {}
12+
13+
async fn foo2() {
14+
foo1().await
15+
}
16+
async fn foo3() {
17+
foo2().await
18+
}
19+
async fn foo4() {
20+
foo3().await
21+
}
22+
23+
async fn foo5() {
24+
foo4().await
25+
}
26+
27+
fn assert_send<T: Send>(_: T) {}
28+
29+
fn main() {
30+
assert_send(foo5());
31+
}

0 commit comments

Comments
 (0)