Skip to content

Commit cc3470c

Browse files
committed
Add test for dereferencing raw pointers and immediately referencing again
1 parent 906a49e commit cc3470c

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/test/ui/consts/min_const_fn/min_const_fn_unsafe_feature_gate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const unsafe fn foo10_2() -> *const Vec<std::cell::Cell<u32>> {
4747
const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
4848
//~^ dereferencing raw pointers in constant functions
4949

50+
const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
51+
//~^ dereferencing raw pointers in constant functions
52+
5053
fn main() {}
5154

5255
const unsafe fn no_union() {

src/test/ui/consts/min_const_fn/min_const_fn_unsafe_feature_gate.stderr

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowe
66
|
77
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
88

9+
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
10+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:50:60
11+
|
12+
LL | const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
13+
| ^^^
14+
|
15+
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
16+
917
error[E0658]: unions in const fn are unstable (see issue #51909)
10-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:54:5
18+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:57:5
1119
|
1220
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
1321
| ^^^^^^^^^^^^^^^
@@ -46,14 +54,22 @@ LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowe
4654
|
4755
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
4856

57+
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
58+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:50:60
59+
|
60+
LL | const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
61+
| ^^^ dereference of raw pointer
62+
|
63+
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
64+
4965
error: access to union field is unsafe and unsafe operations are not allowed in const fn
50-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:54:5
66+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:57:5
5167
|
5268
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
5369
| ^^^^^^^^^^^^^^^ access to union field
5470
|
5571
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
5672

57-
error: aborting due to 7 previous errors
73+
error: aborting due to 9 previous errors
5874

5975
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)