Skip to content

Commit 1710cb9

Browse files
committed
Allow using unsafe on functions inside extern blocks
1 parent e79992a commit 1710cb9

8 files changed

+24
-49
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -522,18 +522,14 @@ impl<'a> AstValidator<'a> {
522522
fn check_foreign_fn_headerless(
523523
&self,
524524
// Deconstruct to ensure exhaustiveness
525-
FnHeader { safety, coroutine_kind, constness, ext }: FnHeader,
525+
FnHeader { safety: _, coroutine_kind, constness, ext }: FnHeader,
526526
) {
527527
let report_err = |span| {
528528
self.dcx().emit_err(errors::FnQualifierInExtern {
529529
span: span,
530530
block: self.current_extern_span(),
531531
});
532532
};
533-
match safety {
534-
Safety::Unsafe(span) => report_err(span),
535-
Safety::Default => (),
536-
}
537533
match coroutine_kind {
538534
Some(knd) => report_err(knd.span()),
539535
None => (),

tests/ui/parser/fn-header-semantic-fail.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ fn main() {
4444

4545
extern "C" {
4646
async fn fe1(); //~ ERROR functions in `extern` blocks cannot have qualifiers
47-
unsafe fn fe2(); //~ ERROR functions in `extern` blocks cannot have qualifiers
47+
unsafe fn fe2();
4848
const fn fe3(); //~ ERROR functions in `extern` blocks cannot have qualifiers
4949
extern "C" fn fe4(); //~ ERROR functions in `extern` blocks cannot have qualifiers
50-
const async unsafe extern "C" fn fe5(); //~ ERROR functions in `extern` blocks
51-
//~| ERROR functions in `extern` blocks
50+
const async unsafe extern "C" fn fe5();
51+
//~^ ERROR functions in `extern` blocks
5252
//~| ERROR functions in `extern` blocks
5353
//~| ERROR functions in `extern` blocks
5454
//~| ERROR functions cannot be both `const` and `async`

tests/ui/parser/fn-header-semantic-fail.stderr

+1-19
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,6 @@ LL | extern "C" {
7878
LL | async fn fe1();
7979
| ^^^^^ help: remove this qualifier
8080

81-
error: functions in `extern` blocks cannot have qualifiers
82-
--> $DIR/fn-header-semantic-fail.rs:47:9
83-
|
84-
LL | extern "C" {
85-
| ---------- in this `extern` block
86-
LL | async fn fe1();
87-
LL | unsafe fn fe2();
88-
| ^^^^^^ help: remove this qualifier
89-
9081
error: functions in `extern` blocks cannot have qualifiers
9182
--> $DIR/fn-header-semantic-fail.rs:48:9
9283
|
@@ -105,15 +96,6 @@ LL | extern "C" {
10596
LL | extern "C" fn fe4();
10697
| ^^^^^^^^^^ help: remove this qualifier
10798

108-
error: functions in `extern` blocks cannot have qualifiers
109-
--> $DIR/fn-header-semantic-fail.rs:50:21
110-
|
111-
LL | extern "C" {
112-
| ---------- in this `extern` block
113-
...
114-
LL | const async unsafe extern "C" fn fe5();
115-
| ^^^^^^ help: remove this qualifier
116-
11799
error: functions in `extern` blocks cannot have qualifiers
118100
--> $DIR/fn-header-semantic-fail.rs:50:15
119101
|
@@ -150,6 +132,6 @@ LL | const async unsafe extern "C" fn fe5();
150132
| | `async` because of this
151133
| `const` because of this
152134

153-
error: aborting due to 17 previous errors
135+
error: aborting due to 15 previous errors
154136

155137
For more information about this error, try `rustc --explain E0379`.

tests/ui/parser/no-const-fn-in-extern-block.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ extern "C" {
33
//~^ ERROR functions in `extern` blocks cannot have qualifiers
44
const unsafe fn bar();
55
//~^ ERROR functions in `extern` blocks cannot have qualifiers
6-
//~| ERROR functions in `extern` blocks cannot have qualifiers
76
}
87

98
fn main() {}

tests/ui/parser/no-const-fn-in-extern-block.stderr

+1-10
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ LL | extern "C" {
66
LL | const fn foo();
77
| ^^^^^ help: remove this qualifier
88

9-
error: functions in `extern` blocks cannot have qualifiers
10-
--> $DIR/no-const-fn-in-extern-block.rs:4:11
11-
|
12-
LL | extern "C" {
13-
| ---------- in this `extern` block
14-
...
15-
LL | const unsafe fn bar();
16-
| ^^^^^^ help: remove this qualifier
17-
189
error: functions in `extern` blocks cannot have qualifiers
1910
--> $DIR/no-const-fn-in-extern-block.rs:4:5
2011
|
@@ -24,5 +15,5 @@ LL | extern "C" {
2415
LL | const unsafe fn bar();
2516
| ^^^^^ help: remove this qualifier
2617

27-
error: aborting due to 3 previous errors
18+
error: aborting due to 2 previous errors
2819

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
extern "C" unsafe {
22
//~^ ERROR expected `{`, found keyword `unsafe`
33
unsafe fn foo();
4-
//~^ ERROR functions in `extern` blocks cannot have qualifiers
54
}
65

76
fn main() {}

tests/ui/parser/unsafe-foreign-mod-2.stderr

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,5 @@ error: expected `{`, found keyword `unsafe`
44
LL | extern "C" unsafe {
55
| ^^^^^^ expected `{`
66

7-
error: functions in `extern` blocks cannot have qualifiers
8-
--> $DIR/unsafe-foreign-mod-2.rs:3:5
9-
|
10-
LL | extern "C" unsafe {
11-
| ----------------- in this `extern` block
12-
LL |
13-
LL | unsafe fn foo();
14-
| ^^^^^^ help: remove this qualifier
15-
16-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
178

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ revisions: edition2021 edition2024
2+
//@[edition2021] edition:2021
3+
//@[edition2024] edition:2024
4+
//@[edition2024] compile-flags: -Zunstable-options
5+
//@ check-pass
6+
7+
unsafe extern "C" {
8+
unsafe fn test1(i: i32);
9+
}
10+
11+
fn test2(i: i32) {
12+
unsafe {
13+
test1(i);
14+
}
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)