Skip to content

Commit 5c0eb6e

Browse files
committed
evaluate obligations in LIFO order during closure projection
This is an annoying gotcha with the projection cache's handling of nested obligations. Nested projection obligations enter the issue in this case: ``` DEBUG:rustc::traits::project: AssociatedTypeNormalizer: depth=3 normalized <std::iter::Map<std::ops::Range<i32>, [closure@not-a-recursion-error.rs:5:30: 5:53]> as std::iter::IntoIterator>::Item to _#7t with 12 add'l obligations ``` Here the normalization result is the result of the nested impl `<[closure@not-a-recursion-error.rs:5:30: 5:53] as FnMut(i32)>::Output`, which is an additional obligation that is a part of "add'l obligations". By itself, this is proper behaviour - the additional obligation is returned, and the RFC 447 rules ensure that it is processed before the output `#_7t` is used in any way. However, the projection cache breaks this - it caches the `<std::iter::Map<std::ops::Range<i32>,[closure@not-a-recursion-error.rs:5:30: 5:53]> as std::iter::IntoIterator>::Item = #_7t` resolution. Now everybody else that attempts to look up the projection will just get `#_7t` *without* any additional obligations. This obviously causes all sorts of trouble (here a spurious `EvaluatedToAmbig` results in specializations not being discarded [here](https://github.com/rust-lang/rust/blob/9ca50bd4d50b55456e88a8c3ad8fcc9798f57522/src/librustc/traits/select.rs#L1705)). The compiler works even with this projection cache gotcha because in most cases during "one-pass evaluation". we tend to process obligations in LIFO order - after an obligation is added to the cache, we process its nested obligations before we do anything else (and if we have a cycle, we handle it specifically) - which makes sure the inference variables are resolved before they are used. That "LIFO" order That was not done when projecting out of a closure, so let's just fix that for the time being. Fixes #38033.
1 parent 39c267a commit 5c0eb6e

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/librustc/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1215,8 +1215,8 @@ fn confirm_closure_candidate<'cx, 'gcx, 'tcx>(
12151215
obligation,
12161216
&closure_type.sig,
12171217
util::TupleArgumentsFlag::No)
1218-
.with_addl_obligations(obligations)
12191218
.with_addl_obligations(vtable.nested)
1219+
.with_addl_obligations(obligations)
12201220
}
12211221

12221222
fn confirm_callable_candidate<'cx, 'gcx, 'tcx>(

src/test/run-pass/issue-38033.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::marker;
12+
use std::mem;
13+
14+
fn main() {
15+
let workers = (0..0).map(|_| result::<u32, ()>());
16+
drop(join_all(workers).poll());
17+
}
18+
19+
trait Future {
20+
type Item;
21+
type Error;
22+
23+
fn poll(&mut self) -> Result<Self::Item, Self::Error>;
24+
}
25+
26+
trait IntoFuture {
27+
type Future: Future<Item=Self::Item, Error=Self::Error>;
28+
type Item;
29+
type Error;
30+
31+
fn into_future(self) -> Self::Future;
32+
}
33+
34+
impl<F: Future> IntoFuture for F {
35+
type Future = F;
36+
type Item = F::Item;
37+
type Error = F::Error;
38+
39+
fn into_future(self) -> F {
40+
self
41+
}
42+
}
43+
44+
struct FutureResult<T, E> {
45+
_inner: marker::PhantomData<(T, E)>,
46+
}
47+
48+
fn result<T, E>() -> FutureResult<T, E> {
49+
loop {}
50+
}
51+
52+
impl<T, E> Future for FutureResult<T, E> {
53+
type Item = T;
54+
type Error = E;
55+
56+
fn poll(&mut self) -> Result<T, E> {
57+
loop {}
58+
}
59+
}
60+
61+
struct JoinAll<I>
62+
where I: IntoIterator,
63+
I::Item: IntoFuture,
64+
{
65+
elems: Vec<<I::Item as IntoFuture>::Item>,
66+
}
67+
68+
fn join_all<I>(_: I) -> JoinAll<I>
69+
where I: IntoIterator,
70+
I::Item: IntoFuture,
71+
{
72+
JoinAll { elems: vec![] }
73+
}
74+
75+
impl<I> Future for JoinAll<I>
76+
where I: IntoIterator,
77+
I::Item: IntoFuture,
78+
{
79+
type Item = Vec<<I::Item as IntoFuture>::Item>;
80+
type Error = <I::Item as IntoFuture>::Error;
81+
82+
fn poll(&mut self) -> Result<Self::Item, Self::Error> {
83+
let elems = mem::replace(&mut self.elems, Vec::new());
84+
Ok(elems.into_iter().map(|e| {
85+
e
86+
}).collect::<Vec<_>>())
87+
}
88+
}

0 commit comments

Comments
 (0)