Rollup merge of #105147 - nbdd0121:inline_const_unsafe, r=oli-obk
Allow unsafe through inline const Handle similar to closures. Address https://github.com/rust-lang/rust/pull/104087#issuecomment-1324173328 Note that this PR does not fix the issue for `unsafe { [0; function_requiring_unsafe()] }`. This is fundamentally unfixable for MIR unsafeck IMO. This PR also does not fix unsafety checking for inline const in pattern position. It actually breaks it, allowing unsafe functions to be used in inline const in pattern position without unsafe blocks. Inline const in pattern position is not visible in MIR so ignored by MIR unsafety checking (currently it is also not checked by borrow checker, which is the reason why it's considered an incomplete feature). `@rustbot` label: +T-lang +F-inline_const
This commit is contained in:
commit
7357cfbf3d
12 changed files with 177 additions and 19 deletions
11
src/test/ui/inline-const/expr-unsafe-err.mir.stderr
Normal file
11
src/test/ui/inline-const/expr-unsafe-err.mir.stderr
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
|
||||
--> $DIR/expr-unsafe-err.rs:8:9
|
||||
|
|
||||
LL | require_unsafe();
|
||||
| ^^^^^^^^^^^^^^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0133`.
|
||||
11
src/test/ui/inline-const/expr-unsafe-err.rs
Normal file
11
src/test/ui/inline-const/expr-unsafe-err.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// revisions: mir thir
|
||||
// [thir]compile-flags: -Z thir-unsafeck
|
||||
#![feature(inline_const)]
|
||||
const unsafe fn require_unsafe() -> usize { 1 }
|
||||
|
||||
fn main() {
|
||||
const {
|
||||
require_unsafe();
|
||||
//~^ ERROR [E0133]
|
||||
}
|
||||
}
|
||||
11
src/test/ui/inline-const/expr-unsafe-err.thir.stderr
Normal file
11
src/test/ui/inline-const/expr-unsafe-err.thir.stderr
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
error[E0133]: call to unsafe function `require_unsafe` is unsafe and requires unsafe function or block
|
||||
--> $DIR/expr-unsafe-err.rs:8:9
|
||||
|
|
||||
LL | require_unsafe();
|
||||
| ^^^^^^^^^^^^^^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0133`.
|
||||
14
src/test/ui/inline-const/expr-unsafe.mir.stderr
Normal file
14
src/test/ui/inline-const/expr-unsafe.mir.stderr
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
warning: unnecessary `unsafe` block
|
||||
--> $DIR/expr-unsafe.rs:12:13
|
||||
|
|
||||
LL | unsafe {}
|
||||
| ^^^^^^ unnecessary `unsafe` block
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/expr-unsafe.rs:4:9
|
||||
|
|
||||
LL | #![warn(unused_unsafe)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
||||
16
src/test/ui/inline-const/expr-unsafe.rs
Normal file
16
src/test/ui/inline-const/expr-unsafe.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// check-pass
|
||||
// revisions: mir thir
|
||||
// [thir]compile-flags: -Z thir-unsafeck
|
||||
#![warn(unused_unsafe)]
|
||||
#![feature(inline_const)]
|
||||
const unsafe fn require_unsafe() -> usize { 1 }
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
const {
|
||||
require_unsafe();
|
||||
unsafe {}
|
||||
//~^ WARNING unnecessary `unsafe` block
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/test/ui/inline-const/expr-unsafe.thir.stderr
Normal file
17
src/test/ui/inline-const/expr-unsafe.thir.stderr
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
warning: unnecessary `unsafe` block
|
||||
--> $DIR/expr-unsafe.rs:12:13
|
||||
|
|
||||
LL | unsafe {
|
||||
| ------ because it's nested under this `unsafe` block
|
||||
...
|
||||
LL | unsafe {}
|
||||
| ^^^^^^ unnecessary `unsafe` block
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/expr-unsafe.rs:4:9
|
||||
|
|
||||
LL | #![warn(unused_unsafe)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
warning: 1 warning emitted
|
||||
|
||||
17
src/test/ui/inline-const/pat-unsafe-err.rs
Normal file
17
src/test/ui/inline-const/pat-unsafe-err.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// ignore-test This is currently broken
|
||||
// revisions: mir thir
|
||||
// [thir]compile-flags: -Z thir-unsafeck
|
||||
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(inline_const_pat)]
|
||||
|
||||
const unsafe fn require_unsafe() -> usize { 1 }
|
||||
|
||||
fn main() {
|
||||
match () {
|
||||
const {
|
||||
require_unsafe();
|
||||
//~^ ERROR [E0133]
|
||||
} => (),
|
||||
}
|
||||
}
|
||||
22
src/test/ui/inline-const/pat-unsafe.rs
Normal file
22
src/test/ui/inline-const/pat-unsafe.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// ignore-test This is currently broken
|
||||
// check-pass
|
||||
// revisions: mir thir
|
||||
// [thir]compile-flags: -Z thir-unsafeck
|
||||
|
||||
#![allow(incomplete_features)]
|
||||
#![warn(unused_unsafe)]
|
||||
#![feature(inline_const_pat)]
|
||||
|
||||
const unsafe fn require_unsafe() -> usize { 1 }
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
match () {
|
||||
const {
|
||||
require_unsafe();
|
||||
unsafe {}
|
||||
//~^ WARNING unnecessary `unsafe` block
|
||||
} => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue