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
9 // revisions: migrate nll
11 // Since we are testing nll (and migration) explicitly as a separate
12 // revisions, don't worry about the --compare-mode=nll on this test.
14 // ignore-compare-mode-nll
15 // ignore-compare-mode-polonius
17 //[nll]compile-flags: -Z borrowck=mir
19 pub trait Stream { //[migrate]~ NOTE trait `Stream` defined here
21 fn next(self) -> Option<Self::Item>;
25 pub struct Repeat(u64);
27 impl<'a> Stream for &'a mut Repeat {
29 fn next(self) -> Option<Self::Item> {
34 pub struct Map<S, F> {
39 impl<'a, A, F, T> Stream for &'a mut Map<A, F>
40 where &'a mut A: Stream,
41 F: FnMut(<&'a mut A as Stream>::Item) -> T,
44 fn next(self) -> Option<T> {
45 match self.stream.next() {
46 Some(item) => Some((self.func)(item)),
52 pub struct Filter<S, F> {
57 impl<'a, A, F, T> Stream for &'a mut Filter<A, F>
58 where for<'b> &'b mut A: Stream<Item=T>, // <---- BAD
61 type Item = <&'a mut A as Stream>::Item;
62 fn next(self) -> Option<Self::Item> {
63 while let Some(item) = self.stream.next() {
64 if (self.func)(&item) {
72 pub trait StreamExt where for<'b> &'b mut Self: Stream {
73 fn map<F>(self, func: F) -> Map<Self, F>
75 for<'a> &'a mut Map<Self, F>: Stream,
83 fn filter<F>(self, func: F) -> Filter<Self, F>
85 for<'a> &'a mut Filter<Self, F>: Stream,
93 fn count(mut self) -> usize
97 while let Some(_) = self.next() {
104 impl<T> StreamExt for T where for<'a> &'a mut T: Stream { }
107 let source = Repeat(10);
108 let map = source.map(|x: &_| x);
109 //[nll]~^ ERROR higher-ranked subtype error
110 //[migrate]~^^ ERROR implementation of `Stream` is not general enough
111 //[migrate]~| NOTE `Stream` would have to be implemented for the type `&'0 mut Map
112 //[migrate]~| NOTE but `Stream` is actually implemented for the type `&'1
113 //[migrate]~| NOTE implementation of `Stream` is not general enough
114 let filter = map.filter(|x: &_| true);
115 //[nll]~^ ERROR higher-ranked subtype error
116 //[nll]~| ERROR higher-ranked subtype error
117 //[nll]~| ERROR higher-ranked subtype error
118 //[nll]~| ERROR higher-ranked subtype error
119 let count = filter.count(); // Assert that we still have a valid stream.
120 //[nll]~^ ERROR higher-ranked subtype error
121 //[nll]~| ERROR higher-ranked subtype error
122 //[nll]~| ERROR higher-ranked subtype error
123 //[nll]~| ERROR higher-ranked subtype error