Skip to content

Commit 46c7c91

Browse files
Rollup merge of rust-lang#107799 - lcnr:update-provisional-result, r=oli-obk
correctly update goals in the cache we may want to actually write the response for our goal into the provisional or global cache instead of simply using the result from the last iteration '^^ r? ```@rust-lang/initiative-trait-system-refactor```
2 parents 04ebaba + 4c7c5e5 commit 46c7c91

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

compiler/rustc_trait_selection/src/solve/search_graph/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use cache::ProvisionalCache;
77
use overflow::OverflowData;
88
use rustc_index::vec::IndexVec;
99
use rustc_middle::ty::TyCtxt;
10-
use std::collections::hash_map::Entry;
10+
use std::{collections::hash_map::Entry, mem};
1111

1212
rustc_index::newtype_index! {
1313
pub struct StackDepth {}
@@ -134,12 +134,15 @@ impl<'tcx> SearchGraph<'tcx> {
134134
let provisional_entry_index = *cache.lookup_table.get(&goal).unwrap();
135135
let provisional_entry = &mut cache.entries[provisional_entry_index];
136136
let depth = provisional_entry.depth;
137+
// We eagerly update the response in the cache here. If we have to reevaluate
138+
// this goal we use the new response when hitting a cycle, and we definitely
139+
// want to access the final response whenever we look at the cache.
140+
let prev_response = mem::replace(&mut provisional_entry.response, response);
141+
137142
// Was the current goal the root of a cycle and was the provisional response
138143
// different from the final one.
139-
if has_been_used && provisional_entry.response != response {
140-
// If so, update the provisional reponse for this goal...
141-
provisional_entry.response = response;
142-
// ...remove all entries whose result depends on this goal
144+
if has_been_used && prev_response != response {
145+
// If so, remove all entries whose result depends on this goal
143146
// from the provisional cache...
144147
//
145148
// That's not completely correct, as a nested goal can also
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// known-bug: unknown
2+
// compile-flags: -Ztrait-solver=next
3+
// failure-status: 101
4+
// normalize-stderr-test "note: .*\n\n" -> ""
5+
// normalize-stderr-test "thread 'rustc' panicked.*\n" -> ""
6+
// rustc-env:RUST_BACKTRACE=0
7+
8+
// This tests checks that we update results in the provisional cache when
9+
// we pop a goal from the stack.
10+
#![feature(auto_traits)]
11+
auto trait Coinductive {}
12+
struct Foo<T>(T);
13+
struct Bar<T>(T);
14+
15+
impl<T> Coinductive for Foo<T>
16+
where
17+
Bar<T>: Coinductive
18+
{}
19+
20+
impl<T> Coinductive for Bar<T>
21+
where
22+
Foo<T>: Coinductive,
23+
Bar<T>: ConstrainInfer,
24+
{}
25+
26+
trait ConstrainInfer {}
27+
impl ConstrainInfer for Bar<u8> {}
28+
impl ConstrainInfer for Foo<u16> {}
29+
30+
fn impls<T: Coinductive>() -> T { todo!() }
31+
32+
fn constrain<T: ConstrainInfer>(_: T) {}
33+
34+
fn main() {
35+
// This should constrain `_` to `u8`.
36+
impls::<Foo<_>>();
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: the compiler unexpectedly panicked. this is a bug.
2+
3+
query stack during panic:
4+
#0 [check_well_formed] checking that `<impl at $DIR/provisional-result-done.rs:20:1: 20:31>` is well-formed
5+
#1 [check_mod_type_wf] checking that types are well-formed in top-level module
6+
end of query stack

0 commit comments

Comments
 (0)