Skip to content

Commit

Permalink
Allow using unsafe on functions inside extern blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Apr 29, 2024
1 parent f9329cb commit 3a0ee07
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 49 deletions.
6 changes: 1 addition & 5 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,14 @@ impl<'a> AstValidator<'a> {
fn check_foreign_fn_headerless(
&self,
// Deconstruct to ensure exhaustiveness
FnHeader { safety, coroutine_kind, constness, ext }: FnHeader,
FnHeader { safety: _, coroutine_kind, constness, ext }: FnHeader,
) {
let report_err = |span| {
self.dcx().emit_err(errors::FnQualifierInExtern {
span: span,
block: self.current_extern_span(),
});
};
match safety {
Safety::Unsafe(span) => report_err(span),
Safety::Default => (),
}
match coroutine_kind {
Some(knd) => report_err(knd.span()),
None => (),
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/parser/fn-header-semantic-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ fn main() {

extern "C" {
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
extern "C" fn fe4(); //~ ERROR functions in `extern` blocks cannot have qualifiers
const async unsafe extern "C" fn fe5(); //~ ERROR functions in `extern` blocks
//~| 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 cannot be both `const` and `async`
Expand Down
20 changes: 1 addition & 19 deletions tests/ui/parser/fn-header-semantic-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,6 @@ LL | extern "C" {
LL | async fn fe1();
| ^^^^^ 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
--> $DIR/fn-header-semantic-fail.rs:48:9
|
Expand All @@ -105,15 +96,6 @@ LL | extern "C" {
LL | extern "C" fn fe4();
| ^^^^^^^^^^ 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
--> $DIR/fn-header-semantic-fail.rs:50:15
|
Expand Down Expand Up @@ -150,6 +132,6 @@ LL | const async unsafe extern "C" fn fe5();
| | `async` 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`.
1 change: 0 additions & 1 deletion tests/ui/parser/no-const-fn-in-extern-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern "C" {
//~^ ERROR functions in `extern` blocks cannot have qualifiers
const unsafe fn bar();
//~^ ERROR functions in `extern` blocks cannot have qualifiers
//~| ERROR functions in `extern` blocks cannot have qualifiers
}

fn main() {}
11 changes: 1 addition & 10 deletions tests/ui/parser/no-const-fn-in-extern-block.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ LL | extern "C" {
LL | const fn foo();
| ^^^^^ 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
--> $DIR/no-const-fn-in-extern-block.rs:4:5
|
Expand All @@ -24,5 +15,5 @@ LL | extern "C" {
LL | const unsafe fn bar();
| ^^^^^ help: remove this qualifier

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

1 change: 0 additions & 1 deletion tests/ui/parser/unsafe-foreign-mod-2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern "C" unsafe {
//~^ ERROR expected `{`, found keyword `unsafe`
unsafe fn foo();
//~^ ERROR functions in `extern` blocks cannot have qualifiers
}

fn main() {}
11 changes: 1 addition & 10 deletions tests/ui/parser/unsafe-foreign-mod-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,5 @@ error: expected `{`, found keyword `unsafe`
LL | extern "C" unsafe {
| ^^^^^^ expected `{`

error: functions in `extern` blocks cannot have qualifiers
--> $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
error: aborting due to 1 previous error

17 changes: 17 additions & 0 deletions tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ revisions: edition2021 edition2024
//@[edition2021] edition:2021
//@[edition2024] edition:2024
//@[edition2024] compile-flags: -Zunstable-options
//@ check-pass

unsafe extern "C" {
unsafe fn test1(i: i32);
}

fn test2(i: i32) {
unsafe {
test1(i);
}
}

fn main() {}

0 comments on commit 3a0ee07

Please sign in to comment.