-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put dynamic check tests into their own file
- Loading branch information
Showing
4 changed files
with
64 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![feature(const_mut_refs)] | ||
#![feature(const_fn)] | ||
#![feature(raw_ref_op)] | ||
#![feature(const_raw_ptr_deref)] | ||
|
||
// This file checks that our dynamic checks catch things that the static checks miss. | ||
// We do not have static checks for these, because we do not look into function bodies. | ||
// We treat all functions as not returning a mutable reference, because there is no way to | ||
// do that without causing the borrow checker to complain (see the B4/helper test in | ||
// mut_ref_in_final.rs). | ||
|
||
const fn helper() -> Option<&'static mut i32> { unsafe { | ||
// Undefined behaviour (integer as pointer), who doesn't love tests like this. | ||
// This code never gets executed, because the static checks fail before that. | ||
Some(&mut *(42 as *mut i32)) //~ ERROR any use of this value will cause an error | ||
} } | ||
// The error is an evaluation error and not a validation error, so the error is reported | ||
// directly at the site where it occurs. | ||
const A: Option<&mut i32> = helper(); | ||
|
||
const fn helper2() -> Option<&'static mut i32> { unsafe { | ||
// Undefined behaviour (dangling pointer), who doesn't love tests like this. | ||
// This code never gets executed, because the static checks fail before that. | ||
Some(&mut *(&mut 42 as *mut i32)) | ||
} } | ||
const B: Option<&mut i32> = helper2(); //~ ERROR encountered dangling pointer in final constant | ||
|
||
fn main() {} |
23 changes: 23 additions & 0 deletions
23
src/test/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
error: any use of this value will cause an error | ||
--> $DIR/mut_ref_in_final_dynamic_check.rs:15:10 | ||
| | ||
LL | Some(&mut *(42 as *mut i32)) | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| unable to turn bytes into a pointer | ||
| inside `helper` at $DIR/mut_ref_in_final_dynamic_check.rs:15:10 | ||
| inside `A` at $DIR/mut_ref_in_final_dynamic_check.rs:19:29 | ||
... | ||
LL | const A: Option<&mut i32> = helper(); | ||
| ------------------------------------- | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error: encountered dangling pointer in final constant | ||
--> $DIR/mut_ref_in_final_dynamic_check.rs:26:1 | ||
| | ||
LL | const B: Option<&mut i32> = helper2(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|