Skip to content

Commit be758ef

Browse files
committed
Add ui test.
1 parent 39f2657 commit be758ef

6 files changed

+128
-11
lines changed

tests/ui/binding/issue-53114-safety-checks.rs

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ fn let_wild_gets_unsafe_field() {
3030
let (_,) = (&u2.a,); //~ ERROR [E0133]
3131
}
3232

33+
fn let_ascribe_gets_unsafe_field() {
34+
let u1 = U { a: I(0) };
35+
let u2 = U { a: I(1) };
36+
let p = P { a: &2, b: &3 };
37+
let _: _ = &p.b; //~ ERROR reference to packed field
38+
let _: _ = u1.a; //~ ERROR [E0133]
39+
let _: _ = &u2.a; //~ ERROR [E0133]
40+
41+
// variation on above with `_` in substructure
42+
let (_,): _ = (&p.b,); //~ ERROR reference to packed field
43+
let (_,): _ = (u1.a,); //~ ERROR [E0133]
44+
let (_,): _ = (&u2.a,); //~ ERROR [E0133]
45+
}
46+
3347
fn match_unsafe_field_to_wild() {
3448
let u1 = U { a: I(0) };
3549
let u2 = U { a: I(1) };

tests/ui/binding/issue-53114-safety-checks.stderr

+57-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@ LL | let (_,) = (&p.b,);
1717
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
1818

1919
error[E0793]: reference to packed field is unaligned
20-
--> $DIR/issue-53114-safety-checks.rs:37:11
20+
--> $DIR/issue-53114-safety-checks.rs:37:16
21+
|
22+
LL | let _: _ = &p.b;
23+
| ^^^^
24+
|
25+
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
26+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
27+
28+
error[E0793]: reference to packed field is unaligned
29+
--> $DIR/issue-53114-safety-checks.rs:42:20
30+
|
31+
LL | let (_,): _ = (&p.b,);
32+
| ^^^^
33+
|
34+
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
35+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
36+
37+
error[E0793]: reference to packed field is unaligned
38+
--> $DIR/issue-53114-safety-checks.rs:51:11
2139
|
2240
LL | match &p.b { _ => { } }
2341
| ^^^^
@@ -26,7 +44,7 @@ LL | match &p.b { _ => { } }
2644
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
2745

2846
error[E0793]: reference to packed field is unaligned
29-
--> $DIR/issue-53114-safety-checks.rs:42:12
47+
--> $DIR/issue-53114-safety-checks.rs:56:12
3048
|
3149
LL | match (&p.b,) { (_,) => { } }
3250
| ^^^^
@@ -59,38 +77,70 @@ LL | let (_,) = (&u2.a,);
5977
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
6078

6179
error[E0133]: access to union field is unsafe and requires unsafe function or block
62-
--> $DIR/issue-53114-safety-checks.rs:38:11
80+
--> $DIR/issue-53114-safety-checks.rs:38:12
81+
|
82+
LL | let _: _ = u1.a;
83+
| ^ access to union field
84+
|
85+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
86+
87+
error[E0133]: access to union field is unsafe and requires unsafe function or block
88+
--> $DIR/issue-53114-safety-checks.rs:39:16
89+
|
90+
LL | let _: _ = &u2.a;
91+
| ^^^^^ access to union field
92+
|
93+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
94+
95+
error[E0133]: access to union field is unsafe and requires unsafe function or block
96+
--> $DIR/issue-53114-safety-checks.rs:43:20
97+
|
98+
LL | let (_,): _ = (u1.a,);
99+
| ^^^^ access to union field
100+
|
101+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
102+
103+
error[E0133]: access to union field is unsafe and requires unsafe function or block
104+
--> $DIR/issue-53114-safety-checks.rs:44:20
105+
|
106+
LL | let (_,): _ = (&u2.a,);
107+
| ^^^^^ access to union field
108+
|
109+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
110+
111+
error[E0133]: access to union field is unsafe and requires unsafe function or block
112+
--> $DIR/issue-53114-safety-checks.rs:52:11
63113
|
64114
LL | match u1.a { _ => { } }
65115
| ^^^^ access to union field
66116
|
67117
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
68118

69119
error[E0133]: access to union field is unsafe and requires unsafe function or block
70-
--> $DIR/issue-53114-safety-checks.rs:39:11
120+
--> $DIR/issue-53114-safety-checks.rs:53:11
71121
|
72122
LL | match &u2.a { _ => { } }
73123
| ^^^^^ access to union field
74124
|
75125
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
76126

77127
error[E0133]: access to union field is unsafe and requires unsafe function or block
78-
--> $DIR/issue-53114-safety-checks.rs:43:12
128+
--> $DIR/issue-53114-safety-checks.rs:57:12
79129
|
80130
LL | match (u1.a,) { (_,) => { } }
81131
| ^^^^ access to union field
82132
|
83133
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
84134

85135
error[E0133]: access to union field is unsafe and requires unsafe function or block
86-
--> $DIR/issue-53114-safety-checks.rs:44:12
136+
--> $DIR/issue-53114-safety-checks.rs:58:12
87137
|
88138
LL | match (&u2.a,) { (_,) => { } }
89139
| ^^^^^ access to union field
90140
|
91141
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
92142

93-
error: aborting due to 11 previous errors
143+
error: aborting due to 17 previous errors
94144

95145
Some errors have detailed explanations: E0133, E0793.
96146
For more information about an error, try `rustc --explain E0133`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// check-pass
2+
3+
fn let_underscore(string: &Option<&str>, mut num: Option<i32>) {
4+
let _ = if let Some(s) = *string { s.len() } else { 0 };
5+
let _ = if let Some(s) = &num { s } else { &0 };
6+
let _ = if let Some(s) = &mut num {
7+
*s += 1;
8+
s
9+
} else {
10+
&mut 0
11+
};
12+
let _ = if let Some(ref s) = num { s } else { &0 };
13+
let _ = if let Some(mut s) = num {
14+
s += 1;
15+
s
16+
} else {
17+
0
18+
};
19+
let _ = if let Some(ref mut s) = num {
20+
*s += 1;
21+
s
22+
} else {
23+
&mut 0
24+
};
25+
}
26+
27+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/unsafe-fn-deref-ptr.rs:5:12
2+
--> $DIR/unsafe-fn-deref-ptr.rs:6:12
3+
|
4+
LL | let _: u8 = *p;
5+
| ^^ dereference of raw pointer
6+
|
7+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
8+
9+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
10+
--> $DIR/unsafe-fn-deref-ptr.rs:7:12
311
|
412
LL | return *p;
513
| ^^ dereference of raw pointer
614
|
715
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
816

9-
error: aborting due to previous error
17+
error: aborting due to 2 previous errors
1018

1119
For more information about this error, try `rustc --explain E0133`.

tests/ui/unsafe/unsafe-fn-deref-ptr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// [thir]compile-flags: -Z thir-unsafeck
33

44
fn f(p: *const u8) -> u8 {
5+
let _ = *p; //[thir]~ ERROR dereference of raw pointer is unsafe
6+
let _: u8 = *p; //~ ERROR dereference of raw pointer is unsafe
57
return *p; //~ ERROR dereference of raw pointer is unsafe
68
}
79

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/unsafe-fn-deref-ptr.rs:5:12
2+
--> $DIR/unsafe-fn-deref-ptr.rs:5:13
3+
|
4+
LL | let _ = *p;
5+
| ^^ dereference of raw pointer
6+
|
7+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
8+
9+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
10+
--> $DIR/unsafe-fn-deref-ptr.rs:6:17
11+
|
12+
LL | let _: u8 = *p;
13+
| ^^ dereference of raw pointer
14+
|
15+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
16+
17+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
18+
--> $DIR/unsafe-fn-deref-ptr.rs:7:12
319
|
420
LL | return *p;
521
| ^^ dereference of raw pointer
622
|
723
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
824

9-
error: aborting due to previous error
25+
error: aborting due to 3 previous errors
1026

1127
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)