1 // rust-lang/rust#30786: the use of `for<'b> &'b mut A: Stream<Item=T`
2 // should act as assertion that item does not borrow from its stream;
3 // but an earlier buggy rustc allowed `.map(|x: &_| x)` which does
6 // This tests double-checks that we do not allow such behavior to leak
11 fn next(self) -> Option<Self::Item>;
15 pub struct Repeat(u64);
17 impl<'a> Stream for &'a mut Repeat {
19 fn next(self) -> Option<Self::Item> {
24 pub struct Map<S, F> {
29 impl<'a, A, F, T> Stream for &'a mut Map<A, F>
32 F: FnMut(<&'a mut A as Stream>::Item) -> T,
35 fn next(self) -> Option<T> {
36 match self.stream.next() {
37 Some(item) => Some((self.func)(item)),
43 pub struct Filter<S, F> {
48 impl<'a, A, F, T> Stream for &'a mut Filter<A, F>
50 for<'b> &'b mut A: Stream<Item = T>, // <---- BAD
53 type Item = <&'a mut A as Stream>::Item;
54 fn next(self) -> Option<Self::Item> {
55 while let Some(item) = self.stream.next() {
56 if (self.func)(&item) {
66 for<'b> &'b mut Self: Stream,
68 fn mapx<F>(self, func: F) -> Map<Self, F>
71 for<'a> &'a mut Map<Self, F>: Stream,
73 Map { func: func, stream: self }
76 fn filterx<F>(self, func: F) -> Filter<Self, F>
79 for<'a> &'a mut Filter<Self, F>: Stream,
81 Filter { func: func, stream: self }
84 fn countx(mut self) -> usize
89 while let Some(_) = self.next() {
96 impl<T> StreamExt for T where for<'a> &'a mut T: Stream {}
98 fn identity<T>(x: &T) -> &T {
103 let source = Repeat(10);
105 // Here, the call to `mapx` returns a type `T` to which `StreamExt`
106 // is not applicable, because `for<'b> &'b mut T: Stream`) doesn't hold.
108 // More concretely, the type `T` is `Map<Repeat, Closure>`, and
109 // the where clause doesn't hold because the signature of the
110 // closure gets inferred to a signature like `|&'_ Stream| -> &'_`
111 // for some specific `'_`, rather than a more generic
114 // Why *exactly* we opt for this signature is a bit unclear to me,
115 // we deduce it somehow from a reuqirement that `Map: Stream` I
117 let map = source.mapx(|x: &_| x);
118 let filter = map.filterx(|x: &_| true);
119 //~^ ERROR the method
123 let source = Repeat(10);
125 // Here, we use a function, which is not subject to the vagaries
126 // of closure signature inference. In this case, we get the error
127 // on `countx` as, I think, the test originally expected.
128 let map = source.mapx(identity);
129 let filter = map.filterx(|x: &_| true);
130 let count = filter.countx();
131 //~^ ERROR the method