]> git.lizzy.rs Git - rust.git/blob - tests/ui/nll/user-annotations/patterns.rs
Rollup merge of #106715 - BoxyUwU:new_solver_triagebot, r=lcnr
[rust.git] / tests / ui / nll / user-annotations / patterns.rs
1 // Test that various patterns also enforce types.
2
3 fn variable_no_initializer() {
4     let x = 22;
5     let y: &'static u32;
6     y = &x; //~ ERROR
7 }
8
9 fn tuple_no_initializer() {
10
11
12     let x = 22;
13     let (y, z): (&'static u32, &'static u32);
14     y = &x; //~ ERROR
15 }
16
17 fn ref_with_ascribed_static_type() -> u32 {
18     // Check the behavior in some wacky cases.
19     let x = 22;
20     let y = &x; //~ ERROR
21     let ref z: &'static u32 = y;
22     **z
23 }
24
25 fn ref_with_ascribed_any_type() -> u32 {
26     let x = 22;
27     let y = &x;
28     let ref z: &u32 = y;
29     **z
30 }
31
32 struct Single<T> { value: T }
33
34 fn struct_no_initializer() {
35
36
37     let x = 22;
38     let Single { value: y }: Single<&'static u32>;
39     y = &x; //~ ERROR
40 }
41
42
43 fn struct_no_initializer_must_normalize() {
44     trait Indirect { type Assoc; }
45     struct StaticU32;
46     impl Indirect for StaticU32 { type Assoc = &'static u32; }
47     struct Single2<T: Indirect> { value: <T as Indirect>::Assoc }
48
49     let x = 22;
50     let Single2 { value: mut _y }: Single2<StaticU32>;
51     _y = &x; //~ ERROR
52 }
53
54 fn variable_with_initializer() {
55     let x = 22;
56     let y: &'static u32 = &x; //~ ERROR
57 }
58
59 fn underscore_with_initializer() {
60     let x = 22;
61     let _: &'static u32 = &x; //~ ERROR
62
63     let _: Vec<&'static String> = vec![&String::new()];
64     //~^ ERROR temporary value dropped while borrowed [E0716]
65
66     let (_, a): (Vec<&'static String>, _) = (vec![&String::new()], 44);
67     //~^ ERROR temporary value dropped while borrowed [E0716]
68
69     let (_a, b): (Vec<&'static String>, _) = (vec![&String::new()], 44);
70     //~^ ERROR temporary value dropped while borrowed [E0716]
71 }
72
73 fn pair_underscores_with_initializer() {
74     let x = 22;
75     let (_, _): (&'static u32, u32) = (&x, 44); //~ ERROR
76 }
77
78 fn pair_variable_with_initializer() {
79     let x = 22;
80     let (y, _): (&'static u32, u32) = (&x, 44); //~ ERROR
81 }
82
83 fn struct_single_field_variable_with_initializer() {
84     let x = 22;
85     let Single { value: y }: Single<&'static u32> = Single { value: &x }; //~ ERROR
86 }
87
88 fn struct_single_field_underscore_with_initializer() {
89     let x = 22;
90     let Single { value: _ }: Single<&'static u32> = Single { value: &x }; //~ ERROR
91 }
92
93 struct Double<T> { value1: T, value2: T }
94
95 fn struct_double_field_underscore_with_initializer() {
96     let x = 22;
97     let Double { value1: _, value2: _ }: Double<&'static u32> = Double {
98         value1: &x, //~ ERROR
99         value2: &44,
100     };
101 }
102
103 fn static_to_a_to_static_through_variable<'a>(x: &'a u32) -> &'static u32 {
104
105
106
107
108
109
110     let y: &'a u32 = &22;
111     y //~ ERROR
112 }
113
114 fn static_to_a_to_static_through_tuple<'a>(x: &'a u32) -> &'static u32 {
115
116
117
118
119
120
121
122     let (y, _z): (&'a u32, u32) = (&22, 44);
123     y //~ ERROR
124 }
125
126 fn static_to_a_to_static_through_struct<'a>(_x: &'a u32) -> &'static u32 {
127     let Single { value: y }: Single<&'a u32> = Single { value: &22 };
128     y //~ ERROR
129 }
130
131 fn a_to_static_then_static<'a>(x: &'a u32) -> &'static u32 {
132     let (y, _z): (&'static u32, u32) = (x, 44); //~ ERROR
133     y
134 }
135
136 fn main() { }