Skip to content

Commit 819b008

Browse files
committed
Put dynamic check tests into their own file
1 parent cd09871 commit 819b008

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

src/test/ui/consts/const-mut-refs/mut_ref_in_final.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![feature(const_mut_refs)]
22
#![feature(const_fn)]
3-
#![feature(const_transmute)]
43
#![feature(raw_ref_op)]
54
#![feature(const_raw_ptr_deref)]
65

@@ -18,18 +17,8 @@ const B2: Option<&mut i32> = None;
1817
// Not ok, can't prove that no mutable allocation ends up in final value
1918
const B3: Option<&mut i32> = Some(&mut 42); //~ ERROR temporary value dropped while borrowed
2019

21-
const fn helper() -> Option<&'static mut i32> { unsafe {
22-
// Undefined behaviour, who doesn't love tests like this.
23-
// This code never gets executed, because the static checks fail before that.
24-
Some(&mut *(42 as *mut i32))
25-
} }
26-
// Check that we do not look into function bodies.
27-
// We treat all functions as not returning a mutable reference, because there is no way to
28-
// do that without causing the borrow checker to complain (see the B5/helper2 test below).
29-
const B4: Option<&mut i32> = helper();
30-
31-
const fn helper2(x: &mut i32) -> Option<&mut i32> { Some(x) }
32-
const B5: Option<&mut i32> = helper2(&mut 42); //~ ERROR temporary value dropped while borrowed
20+
const fn helper(x: &mut i32) -> Option<&mut i32> { Some(x) }
21+
const B4: Option<&mut i32> = helper(&mut 42); //~ ERROR temporary value dropped while borrowed
3322

3423
// Ok, because no references to mutable data exist here, since the `{}` moves
3524
// its value and then takes a reference to that.

src/test/ui/consts/const-mut-refs/mut_ref_in_final.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0764]: mutable references are not allowed in the final value of constants
2-
--> $DIR/mut_ref_in_final.rs:13:21
2+
--> $DIR/mut_ref_in_final.rs:12:21
33
|
44
LL | const B: *mut i32 = &mut 4;
55
| ^^^^^^
66

77
error[E0716]: temporary value dropped while borrowed
8-
--> $DIR/mut_ref_in_final.rs:19:40
8+
--> $DIR/mut_ref_in_final.rs:18:40
99
|
1010
LL | const B3: Option<&mut i32> = Some(&mut 42);
1111
| ----------^^-
@@ -15,17 +15,17 @@ LL | const B3: Option<&mut i32> = Some(&mut 42);
1515
| using this value as a constant requires that borrow lasts for `'static`
1616

1717
error[E0716]: temporary value dropped while borrowed
18-
--> $DIR/mut_ref_in_final.rs:32:43
18+
--> $DIR/mut_ref_in_final.rs:21:42
1919
|
20-
LL | const B5: Option<&mut i32> = helper2(&mut 42);
21-
| -------------^^-
22-
| | | |
23-
| | | temporary value is freed at the end of this statement
24-
| | creates a temporary which is freed while still in use
20+
LL | const B4: Option<&mut i32> = helper(&mut 42);
21+
| ------------^^-
22+
| | | |
23+
| | | temporary value is freed at the end of this statement
24+
| | creates a temporary which is freed while still in use
2525
| using this value as a constant requires that borrow lasts for `'static`
2626

2727
error[E0716]: temporary value dropped while borrowed
28-
--> $DIR/mut_ref_in_final.rs:47:65
28+
--> $DIR/mut_ref_in_final.rs:36:65
2929
|
3030
LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
3131
| -------------------------------^^--
@@ -35,7 +35,7 @@ LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
3535
| using this value as a constant requires that borrow lasts for `'static`
3636

3737
error[E0716]: temporary value dropped while borrowed
38-
--> $DIR/mut_ref_in_final.rs:50:67
38+
--> $DIR/mut_ref_in_final.rs:39:67
3939
|
4040
LL | static FOO2: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
4141
| -------------------------------^^--
@@ -45,7 +45,7 @@ LL | static FOO2: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
4545
| using this value as a static requires that borrow lasts for `'static`
4646

4747
error[E0716]: temporary value dropped while borrowed
48-
--> $DIR/mut_ref_in_final.rs:53:71
48+
--> $DIR/mut_ref_in_final.rs:42:71
4949
|
5050
LL | static mut FOO3: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
5151
| -------------------------------^^--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(const_mut_refs)]
2+
#![feature(const_fn)]
3+
#![feature(raw_ref_op)]
4+
#![feature(const_raw_ptr_deref)]
5+
6+
// This file checks that our dynamic checks catch things that the static checks miss.
7+
// We do not have static checks for these, because we do not look into function bodies.
8+
// We treat all functions as not returning a mutable reference, because there is no way to
9+
// do that without causing the borrow checker to complain (see the B4/helper test in
10+
// mut_ref_in_final.rs).
11+
12+
const fn helper() -> Option<&'static mut i32> { unsafe {
13+
// Undefined behaviour (integer as pointer), who doesn't love tests like this.
14+
// This code never gets executed, because the static checks fail before that.
15+
Some(&mut *(42 as *mut i32)) //~ ERROR any use of this value will cause an error
16+
} }
17+
// The error is an evaluation error and not a validation error, so the error is reported
18+
// directly at the site where it occurs.
19+
const A: Option<&mut i32> = helper();
20+
21+
const fn helper2() -> Option<&'static mut i32> { unsafe {
22+
// Undefined behaviour (dangling pointer), who doesn't love tests like this.
23+
// This code never gets executed, because the static checks fail before that.
24+
Some(&mut *(&mut 42 as *mut i32))
25+
} }
26+
const B: Option<&mut i32> = helper2(); //~ ERROR encountered dangling pointer in final constant
27+
28+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/mut_ref_in_final_dynamic_check.rs:15:10
3+
|
4+
LL | Some(&mut *(42 as *mut i32))
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| unable to turn bytes into a pointer
8+
| inside `helper` at $DIR/mut_ref_in_final_dynamic_check.rs:15:10
9+
| inside `A` at $DIR/mut_ref_in_final_dynamic_check.rs:19:29
10+
...
11+
LL | const A: Option<&mut i32> = helper();
12+
| -------------------------------------
13+
|
14+
= note: `#[deny(const_err)]` on by default
15+
16+
error: encountered dangling pointer in final constant
17+
--> $DIR/mut_ref_in_final_dynamic_check.rs:26:1
18+
|
19+
LL | const B: Option<&mut i32> = helper2();
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
22+
error: aborting due to 2 previous errors
23+

0 commit comments

Comments
 (0)