]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-21245.rs
Rollup merge of #54680 - RalfJung:compile-pass, r=pnkfelix
[rust.git] / src / test / ui / issues / issue-21245.rs
1 // Copyright 2015 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 // compile-pass
12 #![allow(dead_code)]
13 // Regression test for issue #21245. Check that we are able to infer
14 // the types in these examples correctly. It used to be that
15 // insufficient type propagation caused the type of the iterator to be
16 // incorrectly unified with the `*const` type to which it is coerced.
17
18 // pretty-expanded FIXME #23616
19
20 use std::ptr;
21
22 trait IntoIterator {
23     type Iter: Iterator;
24
25     fn into_iter2(self) -> Self::Iter;
26 }
27
28 impl<I> IntoIterator for I where I: Iterator {
29     type Iter = I;
30
31     fn into_iter2(self) -> I {
32         self
33     }
34 }
35
36 fn desugared_for_loop_bad<T>(v: Vec<T>) {
37     match IntoIterator::into_iter2(v.iter()) {
38         mut iter => {
39             loop {
40                 match ::std::iter::Iterator::next(&mut iter) {
41                     ::std::option::Option::Some(x) => {
42                         unsafe { ptr::read(x); }
43                     },
44                     ::std::option::Option::None => break
45                 }
46             }
47         }
48     }
49 }
50
51 fn desugared_for_loop_good<T>(v: Vec<T>) {
52     match v.iter().into_iter() {
53         mut iter => {
54             loop {
55                 match ::std::iter::Iterator::next(&mut iter) {
56                     ::std::option::Option::Some(x) => {
57                         unsafe { ptr::read(x); }
58                     },
59                     ::std::option::Option::None => break
60                 }
61             }
62         }
63     }
64 }
65
66 fn main() {}