Skip to content

Commit ab2eecc

Browse files
committed
Add some NonZero tests
1 parent 9400c23 commit ab2eecc

6 files changed

+306
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![allow(overflowing_literals)]
2+
3+
fn main() {
4+
let x: std::num::NonZero<i8> = -128;
5+
//~^ ERROR mismatched types
6+
//~| HELP consider calling `NonZero::new`
7+
assert_eq!(x.get(), -128_i8);
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/non_zero_assigned_lit.rs:4:36
3+
|
4+
LL | let x: std::num::NonZero<i8> = -128;
5+
| --------------------- ^^^^ expected `NonZero<i8>`, found integer
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected struct `NonZero<i8>`
10+
found type `{integer}`
11+
help: consider calling `NonZero::new`
12+
|
13+
LL | let x: std::num::NonZero<i8> = NonZero::new(-128).unwrap();
14+
| +++++++++++++ ++++++++++
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![allow(overflowing_literals)]
2+
3+
fn main() {
4+
let _: std::num::NonZero<u8> = 256;
5+
//~^ ERROR mismatched types
6+
//~| HELP consider calling `NonZero::new`
7+
8+
let _: std::num::NonZero<i8> = -128;
9+
//~^ ERROR mismatched types
10+
//~| HELP consider calling `NonZero::new`
11+
let _: std::num::NonZero<i8> = -129;
12+
//~^ ERROR mismatched types
13+
//~| HELP consider calling `NonZero::new`
14+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/non_zero_assigned_oflo_lit.rs:4:36
3+
|
4+
LL | let _: std::num::NonZero<u8> = 256;
5+
| --------------------- ^^^ expected `NonZero<u8>`, found integer
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected struct `NonZero<u8>`
10+
found type `{integer}`
11+
help: consider calling `NonZero::new`
12+
|
13+
LL | let _: std::num::NonZero<u8> = NonZero::new(256).unwrap();
14+
| +++++++++++++ ++++++++++
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/non_zero_assigned_oflo_lit.rs:8:36
18+
|
19+
LL | let _: std::num::NonZero<i8> = -128;
20+
| --------------------- ^^^^ expected `NonZero<i8>`, found integer
21+
| |
22+
| expected due to this
23+
|
24+
= note: expected struct `NonZero<i8>`
25+
found type `{integer}`
26+
help: consider calling `NonZero::new`
27+
|
28+
LL | let _: std::num::NonZero<i8> = NonZero::new(-128).unwrap();
29+
| +++++++++++++ ++++++++++
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/non_zero_assigned_oflo_lit.rs:11:36
33+
|
34+
LL | let _: std::num::NonZero<i8> = -129;
35+
| --------------------- ^^^^ expected `NonZero<i8>`, found integer
36+
| |
37+
| expected due to this
38+
|
39+
= note: expected struct `NonZero<i8>`
40+
found type `{integer}`
41+
help: consider calling `NonZero::new`
42+
|
43+
LL | let _: std::num::NonZero<i8> = NonZero::new(-129).unwrap();
44+
| +++++++++++++ ++++++++++
45+
46+
error: aborting due to 3 previous errors
47+
48+
For more information about this error, try `rustc --explain E0308`.

tests/ui/mismatched_types/non_zero_assigned_something.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
11
fn main() {
2+
let _: std::num::NonZero<u64> = 0;
3+
//~^ ERROR mismatched types
4+
//~| HELP consider calling `NonZero::new`
5+
6+
let _: Option<std::num::NonZero<u64>> = 0;
7+
//~^ ERROR mismatched types
8+
//~| HELP consider calling `NonZero::new`
9+
210
let _: std::num::NonZero<u64> = 1;
311
//~^ ERROR mismatched types
412
//~| HELP consider calling `NonZero::new`
13+
let _: std::num::NonZero<u64> = 1u8;
14+
//~^ ERROR mismatched types
15+
let _: std::num::NonZero<u64> = 1u64;
16+
//~^ ERROR mismatched types
17+
//~| HELP consider calling `NonZero::new`
18+
19+
let _: std::num::NonZero<u8> = 255;
20+
//~^ ERROR mismatched types
21+
//~| HELP consider calling `NonZero::new`
22+
let _: std::num::NonZero<u8> = 256;
23+
//~^ ERROR mismatched types
24+
//~| HELP consider calling `NonZero::new`
25+
26+
let _: std::num::NonZero<u8> = -10;
27+
//~^ ERROR mismatched types
28+
//~| HELP consider calling `NonZero::new`
29+
30+
let _: std::num::NonZero<i8> = -128;
31+
//~^ ERROR mismatched types
32+
//~| HELP consider calling `NonZero::new`
33+
let _: std::num::NonZero<i8> = -129;
34+
//~^ ERROR mismatched types
35+
//~| HELP consider calling `NonZero::new`
36+
let _: std::num::NonZero<i8> = -1;
37+
//~^ ERROR mismatched types
38+
//~| HELP consider calling `NonZero::new`
39+
let _: std::num::NonZero<i8> = 0;
40+
//~^ ERROR mismatched types
41+
//~| HELP consider calling `NonZero::new`
42+
let _: std::num::NonZero<i8> = 1;
43+
//~^ ERROR mismatched types
44+
//~| HELP consider calling `NonZero::new`
545

646
let _: Option<std::num::NonZero<u64>> = 1;
747
//~^ ERROR mismatched types

tests/ui/mismatched_types/non_zero_assigned_something.stderr

Lines changed: 178 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
error[E0308]: mismatched types
22
--> $DIR/non_zero_assigned_something.rs:2:37
33
|
4+
LL | let _: std::num::NonZero<u64> = 0;
5+
| ---------------------- ^ expected `NonZero<u64>`, found integer
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected struct `NonZero<u64>`
10+
found type `{integer}`
11+
help: consider calling `NonZero::new`
12+
|
13+
LL | let _: std::num::NonZero<u64> = NonZero::new(0).unwrap();
14+
| +++++++++++++ ++++++++++
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/non_zero_assigned_something.rs:6:45
18+
|
19+
LL | let _: Option<std::num::NonZero<u64>> = 0;
20+
| ------------------------------ ^ expected `Option<NonZero<u64>>`, found integer
21+
| |
22+
| expected due to this
23+
|
24+
= note: expected enum `Option<NonZero<u64>>`
25+
found type `{integer}`
26+
help: consider calling `NonZero::new`
27+
|
28+
LL | let _: Option<std::num::NonZero<u64>> = NonZero::new(0);
29+
| +++++++++++++ +
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/non_zero_assigned_something.rs:10:37
33+
|
434
LL | let _: std::num::NonZero<u64> = 1;
535
| ---------------------- ^ expected `NonZero<u64>`, found integer
636
| |
@@ -14,7 +44,153 @@ LL | let _: std::num::NonZero<u64> = NonZero::new(1).unwrap();
1444
| +++++++++++++ ++++++++++
1545

1646
error[E0308]: mismatched types
17-
--> $DIR/non_zero_assigned_something.rs:6:45
47+
--> $DIR/non_zero_assigned_something.rs:13:37
48+
|
49+
LL | let _: std::num::NonZero<u64> = 1u8;
50+
| ---------------------- ^^^ expected `NonZero<u64>`, found `u8`
51+
| |
52+
| expected due to this
53+
|
54+
= note: expected struct `NonZero<u64>`
55+
found type `u8`
56+
57+
error[E0308]: mismatched types
58+
--> $DIR/non_zero_assigned_something.rs:15:37
59+
|
60+
LL | let _: std::num::NonZero<u64> = 1u64;
61+
| ---------------------- ^^^^ expected `NonZero<u64>`, found `u64`
62+
| |
63+
| expected due to this
64+
|
65+
= note: expected struct `NonZero<u64>`
66+
found type `u64`
67+
help: consider calling `NonZero::new`
68+
|
69+
LL | let _: std::num::NonZero<u64> = NonZero::new(1u64).unwrap();
70+
| +++++++++++++ ++++++++++
71+
72+
error[E0308]: mismatched types
73+
--> $DIR/non_zero_assigned_something.rs:19:36
74+
|
75+
LL | let _: std::num::NonZero<u8> = 255;
76+
| --------------------- ^^^ expected `NonZero<u8>`, found integer
77+
| |
78+
| expected due to this
79+
|
80+
= note: expected struct `NonZero<u8>`
81+
found type `{integer}`
82+
help: consider calling `NonZero::new`
83+
|
84+
LL | let _: std::num::NonZero<u8> = NonZero::new(255).unwrap();
85+
| +++++++++++++ ++++++++++
86+
87+
error[E0308]: mismatched types
88+
--> $DIR/non_zero_assigned_something.rs:22:36
89+
|
90+
LL | let _: std::num::NonZero<u8> = 256;
91+
| --------------------- ^^^ expected `NonZero<u8>`, found integer
92+
| |
93+
| expected due to this
94+
|
95+
= note: expected struct `NonZero<u8>`
96+
found type `{integer}`
97+
help: consider calling `NonZero::new`
98+
|
99+
LL | let _: std::num::NonZero<u8> = NonZero::new(256).unwrap();
100+
| +++++++++++++ ++++++++++
101+
102+
error[E0308]: mismatched types
103+
--> $DIR/non_zero_assigned_something.rs:26:36
104+
|
105+
LL | let _: std::num::NonZero<u8> = -10;
106+
| --------------------- ^^^ expected `NonZero<u8>`, found integer
107+
| |
108+
| expected due to this
109+
|
110+
= note: expected struct `NonZero<u8>`
111+
found type `{integer}`
112+
help: consider calling `NonZero::new`
113+
|
114+
LL | let _: std::num::NonZero<u8> = NonZero::new(-10).unwrap();
115+
| +++++++++++++ ++++++++++
116+
117+
error[E0308]: mismatched types
118+
--> $DIR/non_zero_assigned_something.rs:30:36
119+
|
120+
LL | let _: std::num::NonZero<i8> = -128;
121+
| --------------------- ^^^^ expected `NonZero<i8>`, found integer
122+
| |
123+
| expected due to this
124+
|
125+
= note: expected struct `NonZero<i8>`
126+
found type `{integer}`
127+
help: consider calling `NonZero::new`
128+
|
129+
LL | let _: std::num::NonZero<i8> = NonZero::new(-128).unwrap();
130+
| +++++++++++++ ++++++++++
131+
132+
error[E0308]: mismatched types
133+
--> $DIR/non_zero_assigned_something.rs:33:36
134+
|
135+
LL | let _: std::num::NonZero<i8> = -129;
136+
| --------------------- ^^^^ expected `NonZero<i8>`, found integer
137+
| |
138+
| expected due to this
139+
|
140+
= note: expected struct `NonZero<i8>`
141+
found type `{integer}`
142+
help: consider calling `NonZero::new`
143+
|
144+
LL | let _: std::num::NonZero<i8> = NonZero::new(-129).unwrap();
145+
| +++++++++++++ ++++++++++
146+
147+
error[E0308]: mismatched types
148+
--> $DIR/non_zero_assigned_something.rs:36:36
149+
|
150+
LL | let _: std::num::NonZero<i8> = -1;
151+
| --------------------- ^^ expected `NonZero<i8>`, found integer
152+
| |
153+
| expected due to this
154+
|
155+
= note: expected struct `NonZero<i8>`
156+
found type `{integer}`
157+
help: consider calling `NonZero::new`
158+
|
159+
LL | let _: std::num::NonZero<i8> = NonZero::new(-1).unwrap();
160+
| +++++++++++++ ++++++++++
161+
162+
error[E0308]: mismatched types
163+
--> $DIR/non_zero_assigned_something.rs:39:36
164+
|
165+
LL | let _: std::num::NonZero<i8> = 0;
166+
| --------------------- ^ expected `NonZero<i8>`, found integer
167+
| |
168+
| expected due to this
169+
|
170+
= note: expected struct `NonZero<i8>`
171+
found type `{integer}`
172+
help: consider calling `NonZero::new`
173+
|
174+
LL | let _: std::num::NonZero<i8> = NonZero::new(0).unwrap();
175+
| +++++++++++++ ++++++++++
176+
177+
error[E0308]: mismatched types
178+
--> $DIR/non_zero_assigned_something.rs:42:36
179+
|
180+
LL | let _: std::num::NonZero<i8> = 1;
181+
| --------------------- ^ expected `NonZero<i8>`, found integer
182+
| |
183+
| expected due to this
184+
|
185+
= note: expected struct `NonZero<i8>`
186+
found type `{integer}`
187+
help: consider calling `NonZero::new`
188+
|
189+
LL | let _: std::num::NonZero<i8> = NonZero::new(1).unwrap();
190+
| +++++++++++++ ++++++++++
191+
192+
error[E0308]: mismatched types
193+
--> $DIR/non_zero_assigned_something.rs:46:45
18194
|
19195
LL | let _: Option<std::num::NonZero<u64>> = 1;
20196
| ------------------------------ ^ expected `Option<NonZero<u64>>`, found integer
@@ -28,6 +204,6 @@ help: consider calling `NonZero::new`
28204
LL | let _: Option<std::num::NonZero<u64>> = NonZero::new(1);
29205
| +++++++++++++ +
30206

31-
error: aborting due to 2 previous errors
207+
error: aborting due to 14 previous errors
32208

33209
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)