mirror of https://github.com/rust-lang/rust.git
Allow using unsafe on functions inside extern blocks
This commit is contained in:
parent
3ba8de0b60
commit
bbddc9b58f
|
@ -519,7 +519,7 @@ impl<'a> AstValidator<'a> {
|
||||||
fn check_foreign_fn_headerless(
|
fn check_foreign_fn_headerless(
|
||||||
&self,
|
&self,
|
||||||
// Deconstruct to ensure exhaustiveness
|
// Deconstruct to ensure exhaustiveness
|
||||||
FnHeader { safety, coroutine_kind, constness, ext }: FnHeader,
|
FnHeader { safety: _, coroutine_kind, constness, ext }: FnHeader,
|
||||||
) {
|
) {
|
||||||
let report_err = |span| {
|
let report_err = |span| {
|
||||||
self.dcx().emit_err(errors::FnQualifierInExtern {
|
self.dcx().emit_err(errors::FnQualifierInExtern {
|
||||||
|
@ -527,10 +527,6 @@ impl<'a> AstValidator<'a> {
|
||||||
block: self.current_extern_span(),
|
block: self.current_extern_span(),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
match safety {
|
|
||||||
Safety::Unsafe(span) => report_err(span),
|
|
||||||
Safety::Default => (),
|
|
||||||
}
|
|
||||||
match coroutine_kind {
|
match coroutine_kind {
|
||||||
Some(knd) => report_err(knd.span()),
|
Some(knd) => report_err(knd.span()),
|
||||||
None => (),
|
None => (),
|
||||||
|
|
|
@ -44,11 +44,11 @@ fn main() {
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
async fn fe1(); //~ ERROR functions in `extern` blocks cannot have qualifiers
|
async fn fe1(); //~ ERROR functions in `extern` blocks cannot have qualifiers
|
||||||
unsafe fn fe2(); //~ ERROR functions in `extern` blocks cannot have qualifiers
|
unsafe fn fe2();
|
||||||
const fn fe3(); //~ ERROR functions in `extern` blocks cannot have qualifiers
|
const fn fe3(); //~ ERROR functions in `extern` blocks cannot have qualifiers
|
||||||
extern "C" fn fe4(); //~ ERROR functions in `extern` blocks cannot have qualifiers
|
extern "C" fn fe4(); //~ ERROR functions in `extern` blocks cannot have qualifiers
|
||||||
const async unsafe extern "C" fn fe5(); //~ ERROR functions in `extern` blocks
|
const async unsafe extern "C" fn fe5();
|
||||||
//~| ERROR functions in `extern` blocks
|
//~^ ERROR functions in `extern` blocks
|
||||||
//~| ERROR functions in `extern` blocks
|
//~| ERROR functions in `extern` blocks
|
||||||
//~| ERROR functions in `extern` blocks
|
//~| ERROR functions in `extern` blocks
|
||||||
//~| ERROR functions cannot be both `const` and `async`
|
//~| ERROR functions cannot be both `const` and `async`
|
||||||
|
|
|
@ -78,15 +78,6 @@ LL | extern "C" {
|
||||||
LL | async fn fe1();
|
LL | async fn fe1();
|
||||||
| ^^^^^ help: remove this qualifier
|
| ^^^^^ help: remove this qualifier
|
||||||
|
|
||||||
error: functions in `extern` blocks cannot have qualifiers
|
|
||||||
--> $DIR/fn-header-semantic-fail.rs:47:9
|
|
||||||
|
|
|
||||||
LL | extern "C" {
|
|
||||||
| ---------- in this `extern` block
|
|
||||||
LL | async fn fe1();
|
|
||||||
LL | unsafe fn fe2();
|
|
||||||
| ^^^^^^ help: remove this qualifier
|
|
||||||
|
|
||||||
error: functions in `extern` blocks cannot have qualifiers
|
error: functions in `extern` blocks cannot have qualifiers
|
||||||
--> $DIR/fn-header-semantic-fail.rs:48:9
|
--> $DIR/fn-header-semantic-fail.rs:48:9
|
||||||
|
|
|
|
||||||
|
@ -105,15 +96,6 @@ LL | extern "C" {
|
||||||
LL | extern "C" fn fe4();
|
LL | extern "C" fn fe4();
|
||||||
| ^^^^^^^^^^ help: remove this qualifier
|
| ^^^^^^^^^^ help: remove this qualifier
|
||||||
|
|
||||||
error: functions in `extern` blocks cannot have qualifiers
|
|
||||||
--> $DIR/fn-header-semantic-fail.rs:50:21
|
|
||||||
|
|
|
||||||
LL | extern "C" {
|
|
||||||
| ---------- in this `extern` block
|
|
||||||
...
|
|
||||||
LL | const async unsafe extern "C" fn fe5();
|
|
||||||
| ^^^^^^ help: remove this qualifier
|
|
||||||
|
|
||||||
error: functions in `extern` blocks cannot have qualifiers
|
error: functions in `extern` blocks cannot have qualifiers
|
||||||
--> $DIR/fn-header-semantic-fail.rs:50:15
|
--> $DIR/fn-header-semantic-fail.rs:50:15
|
||||||
|
|
|
|
||||||
|
@ -150,6 +132,6 @@ LL | const async unsafe extern "C" fn fe5();
|
||||||
| | `async` because of this
|
| | `async` because of this
|
||||||
| `const` because of this
|
| `const` because of this
|
||||||
|
|
||||||
error: aborting due to 17 previous errors
|
error: aborting due to 15 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0379`.
|
For more information about this error, try `rustc --explain E0379`.
|
||||||
|
|
|
@ -3,7 +3,6 @@ extern "C" {
|
||||||
//~^ ERROR functions in `extern` blocks cannot have qualifiers
|
//~^ ERROR functions in `extern` blocks cannot have qualifiers
|
||||||
const unsafe fn bar();
|
const unsafe fn bar();
|
||||||
//~^ ERROR functions in `extern` blocks cannot have qualifiers
|
//~^ ERROR functions in `extern` blocks cannot have qualifiers
|
||||||
//~| ERROR functions in `extern` blocks cannot have qualifiers
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
|
@ -6,15 +6,6 @@ LL | extern "C" {
|
||||||
LL | const fn foo();
|
LL | const fn foo();
|
||||||
| ^^^^^ help: remove this qualifier
|
| ^^^^^ help: remove this qualifier
|
||||||
|
|
||||||
error: functions in `extern` blocks cannot have qualifiers
|
|
||||||
--> $DIR/no-const-fn-in-extern-block.rs:4:11
|
|
||||||
|
|
|
||||||
LL | extern "C" {
|
|
||||||
| ---------- in this `extern` block
|
|
||||||
...
|
|
||||||
LL | const unsafe fn bar();
|
|
||||||
| ^^^^^^ help: remove this qualifier
|
|
||||||
|
|
||||||
error: functions in `extern` blocks cannot have qualifiers
|
error: functions in `extern` blocks cannot have qualifiers
|
||||||
--> $DIR/no-const-fn-in-extern-block.rs:4:5
|
--> $DIR/no-const-fn-in-extern-block.rs:4:5
|
||||||
|
|
|
|
||||||
|
@ -24,5 +15,5 @@ LL | extern "C" {
|
||||||
LL | const unsafe fn bar();
|
LL | const unsafe fn bar();
|
||||||
| ^^^^^ help: remove this qualifier
|
| ^^^^^ help: remove this qualifier
|
||||||
|
|
||||||
error: aborting due to 3 previous errors
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
extern "C" unsafe {
|
extern "C" unsafe {
|
||||||
//~^ ERROR expected `{`, found keyword `unsafe`
|
//~^ ERROR expected `{`, found keyword `unsafe`
|
||||||
unsafe fn foo();
|
unsafe fn foo();
|
||||||
//~^ ERROR functions in `extern` blocks cannot have qualifiers
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
|
@ -4,14 +4,5 @@ error: expected `{`, found keyword `unsafe`
|
||||||
LL | extern "C" unsafe {
|
LL | extern "C" unsafe {
|
||||||
| ^^^^^^ expected `{`
|
| ^^^^^^ expected `{`
|
||||||
|
|
||||||
error: functions in `extern` blocks cannot have qualifiers
|
error: aborting due to 1 previous error
|
||||||
--> $DIR/unsafe-foreign-mod-2.rs:3:5
|
|
||||||
|
|
|
||||||
LL | extern "C" unsafe {
|
|
||||||
| ----------------- in this `extern` block
|
|
||||||
LL |
|
|
||||||
LL | unsafe fn foo();
|
|
||||||
| ^^^^^^ help: remove this qualifier
|
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
error[E0133]: call to unsafe function `test1` is unsafe and requires unsafe function or block
|
||||||
|
--> $DIR/unsafe-items.rs:17:5
|
||||||
|
|
|
||||||
|
LL | test1(i);
|
||||||
|
| ^^^^^^^^ call to unsafe function
|
||||||
|
|
|
||||||
|
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0133`.
|
|
@ -0,0 +1,11 @@
|
||||||
|
error[E0133]: call to unsafe function `test1` is unsafe and requires unsafe block
|
||||||
|
--> $DIR/unsafe-items.rs:17:5
|
||||||
|
|
|
||||||
|
LL | test1(i);
|
||||||
|
| ^^^^^^^^ call to unsafe function
|
||||||
|
|
|
||||||
|
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0133`.
|
|
@ -0,0 +1,21 @@
|
||||||
|
//@ revisions: edition2021 edition2024
|
||||||
|
//@[edition2021] edition:2021
|
||||||
|
//@[edition2024] edition:2024
|
||||||
|
//@[edition2024] compile-flags: -Zunstable-options
|
||||||
|
|
||||||
|
unsafe extern "C" {
|
||||||
|
unsafe fn test1(i: i32);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test2(i: i32) {
|
||||||
|
unsafe {
|
||||||
|
test1(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test3(i: i32) {
|
||||||
|
test1(i);
|
||||||
|
//~^ ERROR: call to unsafe function `test1` is unsafe
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
Loading…
Reference in New Issue