Skip to content

Commit 27fe959

Browse files
Check for raw pointer dereference in THIR unsafeck
1 parent f94942d commit 27fe959

22 files changed

+113
-10
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
153153
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
154154
self.requires_unsafe(expr.span, UseOfInlineAssembly);
155155
}
156+
ExprKind::Deref { arg } => {
157+
if self.thir[arg].ty.is_unsafe_ptr() {
158+
self.requires_unsafe(expr.span, DerefOfRawPointer);
159+
}
160+
}
156161
_ => {}
157162
}
158163

@@ -203,7 +208,6 @@ enum UnsafeOpKind {
203208
UseOfMutableStatic,
204209
#[allow(dead_code)] // FIXME
205210
UseOfExternStatic,
206-
#[allow(dead_code)] // FIXME
207211
DerefOfRawPointer,
208212
#[allow(dead_code)] // FIXME
209213
AssignToDroppingUnionField,

src/test/ui/generator/issue-45729-unsafe-in-generator.stderr renamed to src/test/ui/generator/issue-45729-unsafe-in-generator.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/issue-45729-unsafe-in-generator.rs:5:9
2+
--> $DIR/issue-45729-unsafe-in-generator.rs:8:9
33
|
44
LL | *(1 as *mut u32) = 42;
55
| ^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer

src/test/ui/generator/issue-45729-unsafe-in-generator.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
#![feature(generators)]
25

36
fn main() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2+
--> $DIR/issue-45729-unsafe-in-generator.rs:8:9
3+
|
4+
LL | *(1 as *mut u32) = 42;
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: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/issues/issue-47412.stderr renamed to src/test/ui/issues/issue-47412.mir.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0133]: access to union field is unsafe and requires unsafe function or block
2-
--> $DIR/issue-47412.rs:11:11
2+
--> $DIR/issue-47412.rs:14:11
33
|
44
LL | match u.void {}
55
| ^^^^^^ access to union field
66
|
77
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
88

99
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
10-
--> $DIR/issue-47412.rs:17:11
10+
--> $DIR/issue-47412.rs:21:11
1111
|
1212
LL | match *ptr {}
1313
| ^^^^ dereference of raw pointer

src/test/ui/issues/issue-47412.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
#[derive(Copy, Clone)]
25
enum Void {}
36

@@ -9,7 +12,8 @@ fn union_field() {
912
union Union { unit: (), void: Void }
1013
let u = Union { unit: () };
1114
match u.void {}
12-
//~^ ERROR access to union field is unsafe
15+
//[mir]~^ ERROR access to union field is unsafe
16+
// FIXME(thir-unsafeck): AccessToUnionField unimplemented
1317
}
1418

1519
fn raw_ptr_deref() {
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2+
--> $DIR/issue-47412.rs:21:11
3+
|
4+
LL | match *ptr {}
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: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/traits/safety-fn-body.stderr renamed to src/test/ui/traits/safety-fn-body.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/safety-fn-body.rs:11:9
2+
--> $DIR/safety-fn-body.rs:14:9
33
|
44
LL | *self += 1;
55
| ^^^^^^^^^^ dereference of raw pointer

src/test/ui/traits/safety-fn-body.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Check that an unsafe impl does not imply that unsafe actions are
22
// legal in the methods.
33

4+
// revisions: mir thir
5+
// [thir]compile-flags: -Z thir-unsafeck
6+
47
unsafe trait UnsafeTrait : Sized {
58
fn foo(self) { }
69
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2+
--> $DIR/safety-fn-body.rs:14:9
3+
|
4+
LL | *self += 1;
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: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/unsafe/issue-45087-unreachable-unsafe.stderr renamed to src/test/ui/unsafe/issue-45087-unreachable-unsafe.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/issue-45087-unreachable-unsafe.rs:3:5
2+
--> $DIR/issue-45087-unreachable-unsafe.rs:6:5
33
|
44
LL | *(1 as *mut u32) = 42;
55
| ^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer

src/test/ui/unsafe/issue-45087-unreachable-unsafe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
fn main() {
25
return;
36
*(1 as *mut u32) = 42;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2+
--> $DIR/issue-45087-unreachable-unsafe.rs:6:5
3+
|
4+
LL | *(1 as *mut u32) = 42;
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: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/unsafe/unsafe-fn-assign-deref-ptr.stderr renamed to src/test/ui/unsafe/unsafe-fn-assign-deref-ptr.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/unsafe-fn-assign-deref-ptr.rs:2:5
2+
--> $DIR/unsafe-fn-assign-deref-ptr.rs:5:5
33
|
44
LL | *p = 0;
55
| ^^^^^^ dereference of raw pointer

src/test/ui/unsafe/unsafe-fn-assign-deref-ptr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
fn f(p: *mut u8) {
25
*p = 0; //~ ERROR dereference of raw pointer is unsafe
36
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2+
--> $DIR/unsafe-fn-assign-deref-ptr.rs:5:5
3+
|
4+
LL | *p = 0;
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: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/unsafe/unsafe-fn-deref-ptr.stderr renamed to src/test/ui/unsafe/unsafe-fn-deref-ptr.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/unsafe-fn-deref-ptr.rs:2:12
2+
--> $DIR/unsafe-fn-deref-ptr.rs:5:12
33
|
44
LL | return *p;
55
| ^^ dereference of raw pointer

src/test/ui/unsafe/unsafe-fn-deref-ptr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
fn f(p: *const u8) -> u8 {
25
return *p; //~ ERROR dereference of raw pointer is unsafe
36
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2+
--> $DIR/unsafe-fn-deref-ptr.rs:5:12
3+
|
4+
LL | return *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: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/unsafe/unsafe-unstable-const-fn.stderr renamed to src/test/ui/unsafe/unsafe-unstable-const-fn.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2-
--> $DIR/unsafe-unstable-const-fn.rs:8:5
2+
--> $DIR/unsafe-unstable-const-fn.rs:11:5
33
|
44
LL | *a == b
55
| ^^ dereference of raw pointer

src/test/ui/unsafe/unsafe-unstable-const-fn.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
#![stable(feature = "foo", since = "1.33.0")]
25
#![feature(staged_api)]
36
#![feature(const_raw_ptr_deref)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
2+
--> $DIR/unsafe-unstable-const-fn.rs:11:5
3+
|
4+
LL | *a == b
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: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)