Skip to content

Commit c6ab624

Browse files
committed
change definitely non-productive cycles to error
1 parent 35fbaf1 commit c6ab624

19 files changed

+228
-204
lines changed

Diff for: compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

+37-8
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,32 @@ where
271271
/// and will need to clearly document it in the rustc-dev-guide before
272272
/// stabilization.
273273
pub(super) fn step_kind_for_source(&self, source: GoalSource) -> PathKind {
274-
match (self.current_goal_kind, source) {
275-
(_, GoalSource::NormalizeGoal(step_kind)) => step_kind,
276-
(CurrentGoalKind::CoinductiveTrait, GoalSource::ImplWhereBound) => {
277-
PathKind::Coinductive
274+
match source {
275+
// We treat these goals as unknown for now. It is likely that most miscellaneous
276+
// nested goals will be converted to `GoalSource::MiscKnownInductive` over time.
277+
GoalSource::Misc => PathKind::Unknown,
278+
GoalSource::NormalizeGoal(path_kind) => path_kind,
279+
GoalSource::ImplWhereBound => {
280+
// We currently only consider a cycle coinductive if it steps
281+
// into a where-clause of a coinductive trait.
282+
//
283+
// We probably want to make all traits coinductive in the future,
284+
// so we treat cycles involving their where-clauses as ambiguous.
285+
if let CurrentGoalKind::CoinductiveTrait = self.current_goal_kind {
286+
PathKind::Coinductive
287+
} else {
288+
PathKind::Unknown
289+
}
278290
}
279-
_ => PathKind::Inductive,
291+
// A step which is clearly unproductive. Cycles exclusively involving such steps
292+
// result in `Err(NoSolution)`.
293+
GoalSource::MiscKnownInductive | GoalSource::InstantiateHigherRanked => {
294+
PathKind::Inductive
295+
}
296+
// These goal sources are likely unproductive and can be changed to
297+
// `PathKind::Inductive`. Keeping them as unknown until we're confident
298+
// about this and have an example where it is necessary.
299+
GoalSource::AliasBoundConstCondition | GoalSource::AliasWellFormed => PathKind::Unknown,
280300
}
281301
}
282302

@@ -606,7 +626,7 @@ where
606626

607627
let (NestedNormalizationGoals(nested_goals), _, certainty) = self.evaluate_goal_raw(
608628
GoalEvaluationKind::Nested,
609-
GoalSource::Misc,
629+
GoalSource::normalizes_to(),
610630
unconstrained_goal,
611631
)?;
612632
// Add the nested goals from normalization to our own nested goals.
@@ -683,7 +703,7 @@ where
683703
pub(super) fn add_normalizes_to_goal(&mut self, mut goal: Goal<I, ty::NormalizesTo<I>>) {
684704
goal.predicate = goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(
685705
self,
686-
GoalSource::Misc,
706+
GoalSource::normalizes_to(),
687707
goal.param_env,
688708
));
689709
self.inspect.add_normalizes_to_goal(self.delegate, self.max_input_universe, goal);
@@ -939,7 +959,16 @@ where
939959
rhs: T,
940960
) -> Result<(), NoSolution> {
941961
let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
942-
self.add_goals(GoalSource::Misc, goals);
962+
if cfg!(debug_assertions) {
963+
for g in goals.iter() {
964+
match g.predicate.kind().skip_binder() {
965+
ty::PredicateKind::Subtype { .. } | ty::PredicateKind::AliasRelate(..) => {}
966+
p => unreachable!("unexpected nested goal in `relate`: {p:?}"),
967+
}
968+
}
969+
}
970+
// Relating types is always unproductive.
971+
self.add_goals(GoalSource::MiscKnownInductive, goals);
943972
Ok(())
944973
}
945974

Diff for: compiler/rustc_next_trait_solver/src/solve/inspect/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> ProofTreeBuilder<D> {
421421
self.add_goal(
422422
delegate,
423423
max_input_universe,
424-
GoalSource::Misc,
424+
GoalSource::normalizes_to(),
425425
goal.with(delegate.cx(), goal.predicate),
426426
);
427427
}

Diff for: compiler/rustc_next_trait_solver/src/solve/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ where
313313
ty::AliasRelationDirection::Equate,
314314
),
315315
);
316-
self.add_goal(GoalSource::Misc, alias_relate_goal);
316+
// Normalization is always unproductive.
317+
self.add_goal(GoalSource::MiscKnownInductive, alias_relate_goal);
317318
self.try_evaluate_added_goals()?;
318319
Ok(self.resolve_vars_if_possible(normalized_term))
319320
} else {

Diff for: compiler/rustc_next_trait_solver/src/solve/project_goals.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ where
2424
ty::AliasRelationDirection::Equate,
2525
),
2626
);
27-
self.add_goal(GoalSource::Misc, goal);
27+
// Normalization is always unproductive.
28+
self.add_goal(GoalSource::MiscKnownInductive, goal);
2829
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
2930
}
3031
}

Diff for: compiler/rustc_next_trait_solver/src/solve/search_graph.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::marker::PhantomData;
33

44
use rustc_type_ir::Interner;
55
use rustc_type_ir::search_graph::{self, PathKind};
6-
use rustc_type_ir::solve::{CanonicalInput, Certainty, QueryResult};
6+
use rustc_type_ir::solve::{CanonicalInput, Certainty, NoSolution, QueryResult};
77

88
use super::inspect::ProofTreeBuilder;
99
use super::{FIXPOINT_STEP_LIMIT, has_no_inference_or_external_constraints};
@@ -47,7 +47,8 @@ where
4747
) -> QueryResult<I> {
4848
match kind {
4949
PathKind::Coinductive => response_no_constraints(cx, input, Certainty::Yes),
50-
PathKind::Inductive => response_no_constraints(cx, input, Certainty::overflow(false)),
50+
PathKind::Unknown => response_no_constraints(cx, input, Certainty::overflow(false)),
51+
PathKind::Inductive => Err(NoSolution),
5152
}
5253
}
5354

@@ -57,12 +58,7 @@ where
5758
input: CanonicalInput<I>,
5859
result: QueryResult<I>,
5960
) -> bool {
60-
match kind {
61-
PathKind::Coinductive => response_no_constraints(cx, input, Certainty::Yes) == result,
62-
PathKind::Inductive => {
63-
response_no_constraints(cx, input, Certainty::overflow(false)) == result
64-
}
65-
}
61+
Self::initial_provisional_result(cx, kind, input) == result
6662
}
6763

6864
fn on_stack_overflow(

Diff for: compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
440440
match (child_mode, nested_goal.source()) {
441441
(
442442
ChildMode::Trait(_) | ChildMode::Host(_),
443-
GoalSource::Misc | GoalSource::NormalizeGoal(_),
443+
GoalSource::Misc
444+
| GoalSource::MiscKnownInductive
445+
| GoalSource::NormalizeGoal(_),
444446
) => {
445447
continue;
446448
}

0 commit comments

Comments
 (0)