Skip to content

Commit 9546b11

Browse files
authored
Rollup merge of #110614 - compiler-errors:new-solver-overflow-response, r=lcnr
Clear response values for overflow in new solver When we have an overflow, return a trivial query response. This fixes an ICE with the code described in #110544: ```rust trait Trait {} struct W<T>(T); impl<T, U> Trait for W<(W<T>, W<U>)> where W<T>: Trait, W<U>: Trait, {} fn impls<T: Trait>() {} fn main() { impls::<W<_>>() } ``` Where, while proving `W<?0>: Trait`, we overflow but still apply the query response of `?0 = (W<?1>, W<?2>)`. Then while re-processing the query to validate that our evaluation result was stable, we get a different query response that looks like `?1 = (W<?3>, W<?4>), ?2 = (W<?5>, W<?6>)`, and so we trigger the ICE. Also, by returning a trivial query response we also avoid the infinite-loop/OOM behavior of the old solver. r? `@lcnr`
2 parents 2f33a82 + ee89421 commit 9546b11

File tree

5 files changed

+122
-20
lines changed

5 files changed

+122
-20
lines changed

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use rustc_infer::infer::at::ToTrace;
33
use rustc_infer::infer::canonical::CanonicalVarValues;
44
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
55
use rustc_infer::infer::{
6-
DefineOpaqueTypes, InferCtxt, InferOk, LateBoundRegionConversionTime, TyCtxtInferExt,
6+
DefineOpaqueTypes, InferCtxt, InferOk, LateBoundRegionConversionTime, RegionVariableOrigin,
7+
TyCtxtInferExt,
78
};
89
use rustc_infer::traits::query::NoSolution;
910
use rustc_infer::traits::ObligationCause;
@@ -223,18 +224,20 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
223224
{
224225
debug!("rerunning goal to check result is stable");
225226
let (_orig_values, canonical_goal) = self.canonicalize_goal(goal);
226-
let canonical_response =
227+
let new_canonical_response =
227228
EvalCtxt::evaluate_canonical_goal(self.tcx(), self.search_graph, canonical_goal)?;
228-
if !canonical_response.value.var_values.is_identity() {
229+
if !new_canonical_response.value.var_values.is_identity() {
229230
bug!(
230231
"unstable result: re-canonicalized goal={canonical_goal:#?} \
231-
response={canonical_response:#?}"
232+
first_response={canonical_response:#?} \
233+
second_response={new_canonical_response:#?}"
232234
);
233235
}
234-
if certainty != canonical_response.value.certainty {
236+
if certainty != new_canonical_response.value.certainty {
235237
bug!(
236238
"unstable certainty: {certainty:#?} re-canonicalized goal={canonical_goal:#?} \
237-
response={canonical_response:#?}"
239+
first_response={canonical_response:#?} \
240+
second_response={new_canonical_response:#?}"
238241
);
239242
}
240243
}
@@ -434,6 +437,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
434437
})
435438
}
436439

440+
pub(super) fn next_region_infer(&self) -> ty::Region<'tcx> {
441+
self.infcx.next_region_var(RegionVariableOrigin::MiscVariable(DUMMY_SP))
442+
}
443+
437444
pub(super) fn next_const_infer(&self, ty: Ty<'tcx>) -> ty::Const<'tcx> {
438445
self.infcx.next_const_var(
439446
ty,

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

+55-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_infer::infer::canonical::query_response::make_query_region_constraints
1616
use rustc_infer::infer::canonical::CanonicalVarValues;
1717
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
1818
use rustc_middle::traits::query::NoSolution;
19-
use rustc_middle::traits::solve::{ExternalConstraints, ExternalConstraintsData};
19+
use rustc_middle::traits::solve::{ExternalConstraints, ExternalConstraintsData, MaybeCause};
2020
use rustc_middle::ty::{self, BoundVar, GenericArgKind};
2121
use rustc_span::DUMMY_SP;
2222
use std::iter;
@@ -60,9 +60,27 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
6060

6161
let certainty = certainty.unify_with(goals_certainty);
6262

63-
let external_constraints = self.compute_external_query_constraints()?;
63+
let response = match certainty {
64+
Certainty::Yes | Certainty::Maybe(MaybeCause::Ambiguity) => {
65+
let external_constraints = self.compute_external_query_constraints()?;
66+
Response { var_values: self.var_values, external_constraints, certainty }
67+
}
68+
Certainty::Maybe(MaybeCause::Overflow) => {
69+
// If we have overflow, it's probable that we're substituting a type
70+
// into itself infinitely and any partial substitutions in the query
71+
// response are probably not useful anyways, so just return an empty
72+
// query response.
73+
//
74+
// This may prevent us from potentially useful inference, e.g.
75+
// 2 candidates, one ambiguous and one overflow, which both
76+
// have the same inference constraints.
77+
//
78+
// Changing this to retain some constraints in the future
79+
// won't be a breaking change, so this is good enough for now.
80+
return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow));
81+
}
82+
};
6483

65-
let response = Response { var_values: self.var_values, external_constraints, certainty };
6684
let canonical = Canonicalizer::canonicalize(
6785
self.infcx,
6886
CanonicalizeMode::Response { max_input_universe: self.max_input_universe },
@@ -72,6 +90,40 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
7290
Ok(canonical)
7391
}
7492

93+
/// Constructs a totally unconstrained, ambiguous response to a goal.
94+
///
95+
/// Take care when using this, since often it's useful to respond with
96+
/// ambiguity but return constrained variables to guide inference.
97+
pub(in crate::solve) fn make_ambiguous_response_no_constraints(
98+
&self,
99+
maybe_cause: MaybeCause,
100+
) -> CanonicalResponse<'tcx> {
101+
let unconstrained_response = Response {
102+
var_values: CanonicalVarValues {
103+
var_values: self.tcx().mk_substs_from_iter(self.var_values.var_values.iter().map(
104+
|arg| -> ty::GenericArg<'tcx> {
105+
match arg.unpack() {
106+
GenericArgKind::Lifetime(_) => self.next_region_infer().into(),
107+
GenericArgKind::Type(_) => self.next_ty_infer().into(),
108+
GenericArgKind::Const(ct) => self.next_const_infer(ct.ty()).into(),
109+
}
110+
},
111+
)),
112+
},
113+
external_constraints: self
114+
.tcx()
115+
.mk_external_constraints(ExternalConstraintsData::default()),
116+
certainty: Certainty::Maybe(maybe_cause),
117+
};
118+
119+
Canonicalizer::canonicalize(
120+
self.infcx,
121+
CanonicalizeMode::Response { max_input_universe: self.max_input_universe },
122+
&mut Default::default(),
123+
unconstrained_response,
124+
)
125+
}
126+
75127
#[instrument(level = "debug", skip(self), ret)]
76128
fn compute_external_query_constraints(&self) -> Result<ExternalConstraints<'tcx>, NoSolution> {
77129
// Cannot use `take_registered_region_obligations` as we may compute the response

compiler/rustc_trait_selection/src/solve/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,17 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
340340
if responses.is_empty() {
341341
return Err(NoSolution);
342342
}
343-
let certainty = responses.iter().fold(Certainty::AMBIGUOUS, |certainty, response| {
344-
certainty.unify_with(response.value.certainty)
345-
});
346-
347-
let response = self.evaluate_added_goals_and_make_canonical_response(certainty);
348-
if let Ok(response) = response {
349-
assert!(response.has_no_inference_or_external_constraints());
350-
Ok(response)
351-
} else {
352-
bug!("failed to make floundered response: {responses:?}");
353-
}
343+
344+
let Certainty::Maybe(maybe_cause) = responses.iter().fold(
345+
Certainty::AMBIGUOUS,
346+
|certainty, response| {
347+
certainty.unify_with(response.value.certainty)
348+
},
349+
) else {
350+
bug!("expected flounder response to be ambiguous")
351+
};
352+
353+
Ok(self.make_ambiguous_response_no_constraints(maybe_cause))
354354
}
355355
}
356356

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile-flags: -Ztrait-solver=next
2+
3+
trait Trait {}
4+
5+
struct W<T>(T);
6+
7+
impl<T, U> Trait for W<(W<T>, W<U>)>
8+
where
9+
W<T>: Trait,
10+
W<U>: Trait,
11+
{
12+
}
13+
14+
fn impls<T: Trait>() {}
15+
16+
fn main() {
17+
impls::<W<_>>();
18+
//~^ ERROR type annotations needed
19+
//~| ERROR overflow evaluating the requirement `W<_>: Trait`
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/exponential-trait-goals.rs:17:5
3+
|
4+
LL | impls::<W<_>>();
5+
| ^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `impls`
6+
7+
error[E0275]: overflow evaluating the requirement `W<_>: Trait`
8+
--> $DIR/exponential-trait-goals.rs:17:5
9+
|
10+
LL | impls::<W<_>>();
11+
| ^^^^^^^^^^^^^
12+
|
13+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`exponential_trait_goals`)
14+
note: required by a bound in `impls`
15+
--> $DIR/exponential-trait-goals.rs:14:13
16+
|
17+
LL | fn impls<T: Trait>() {}
18+
| ^^^^^ required by this bound in `impls`
19+
20+
error: aborting due to 2 previous errors
21+
22+
Some errors have detailed explanations: E0275, E0282.
23+
For more information about an error, try `rustc --explain E0275`.

0 commit comments

Comments
 (0)