]> git.lizzy.rs Git - rust.git/blob - tests/ui/implicit_hasher.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / implicit_hasher.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![allow(unused)]
11
12 use std::cmp::Eq;
13 use std::collections::{HashMap, HashSet};
14 use std::hash::{BuildHasher, Hash};
15
16 pub trait Foo<T>: Sized {
17     fn make() -> (Self, Self);
18 }
19
20 impl<K: Hash + Eq, V> Foo<i8> for HashMap<K, V> {
21     fn make() -> (Self, Self) {
22         // OK, don't suggest to modify these
23         let _: HashMap<i32, i32> = HashMap::new();
24         let _: HashSet<i32> = HashSet::new();
25
26         (HashMap::new(), HashMap::with_capacity(10))
27     }
28 }
29 impl<K: Hash + Eq, V> Foo<i8> for (HashMap<K, V>,) {
30     fn make() -> (Self, Self) {
31         ((HashMap::new(),), (HashMap::with_capacity(10),))
32     }
33 }
34 impl Foo<i16> for HashMap<String, String> {
35     fn make() -> (Self, Self) {
36         (HashMap::new(), HashMap::with_capacity(10))
37     }
38 }
39
40 impl<K: Hash + Eq, V, S: BuildHasher + Default> Foo<i32> for HashMap<K, V, S> {
41     fn make() -> (Self, Self) {
42         (HashMap::default(), HashMap::with_capacity_and_hasher(10, S::default()))
43     }
44 }
45 impl<S: BuildHasher + Default> Foo<i64> for HashMap<String, String, S> {
46     fn make() -> (Self, Self) {
47         (HashMap::default(), HashMap::with_capacity_and_hasher(10, S::default()))
48     }
49 }
50
51 impl<T: Hash + Eq> Foo<i8> for HashSet<T> {
52     fn make() -> (Self, Self) {
53         (HashSet::new(), HashSet::with_capacity(10))
54     }
55 }
56 impl Foo<i16> for HashSet<String> {
57     fn make() -> (Self, Self) {
58         (HashSet::new(), HashSet::with_capacity(10))
59     }
60 }
61
62 impl<T: Hash + Eq, S: BuildHasher + Default> Foo<i32> for HashSet<T, S> {
63     fn make() -> (Self, Self) {
64         (HashSet::default(), HashSet::with_capacity_and_hasher(10, S::default()))
65     }
66 }
67 impl<S: BuildHasher + Default> Foo<i64> for HashSet<String, S> {
68     fn make() -> (Self, Self) {
69         (HashSet::default(), HashSet::with_capacity_and_hasher(10, S::default()))
70     }
71 }
72
73 pub fn foo(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {}
74
75 macro_rules! gen {
76     (impl) => {
77         impl<K: Hash + Eq, V> Foo<u8> for HashMap<K, V> {
78             fn make() -> (Self, Self) {
79                 (HashMap::new(), HashMap::with_capacity(10))
80             }
81         }
82     };
83
84     (fn $name:ident) => {
85         pub fn $name(_map: &mut HashMap<i32, i32>, _set: &mut HashSet<i32>) {}
86     };
87 }
88 #[rustfmt::skip]
89 gen!(impl);
90 gen!(fn bar);
91
92 // When the macro is in a different file, the suggestion spans can't be combined properly
93 // and should not cause an ICE
94 // See #2707
95 #[macro_use]
96 #[path = "../auxiliary/test_macro.rs"]
97 pub mod test_macro;
98 __implicit_hasher_test_macro!(impl<K, V> for HashMap<K, V> where V: test_macro::A);
99
100 fn main() {}