Skip to content

Commit bbddc9b

Browse files
committed
Allow using unsafe on functions inside extern blocks
1 parent 3ba8de0 commit bbddc9b

10 files changed

+50
-49
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,14 @@ impl<'a> AstValidator<'a> {
519519
fn check_foreign_fn_headerless(
520520
&self,
521521
// Deconstruct to ensure exhaustiveness
522-
FnHeader { safety, coroutine_kind, constness, ext }: FnHeader,
522+
FnHeader { safety: _, coroutine_kind, constness, ext }: FnHeader,
523523
) {
524524
let report_err = |span| {
525525
self.dcx().emit_err(errors::FnQualifierInExtern {
526526
span: span,
527527
block: self.current_extern_span(),
528528
});
529529
};
530-
match safety {
531-
Safety::Unsafe(span) => report_err(span),
532-
Safety::Default => (),
533-
}
534530
match coroutine_kind {
535531
Some(knd) => report_err(knd.span()),
536532
None => (),

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 19 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 10 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 10 deletions
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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: call to unsafe function `test1` is unsafe and requires unsafe function or block
2+
--> $DIR/unsafe-items.rs:17:5
3+
|
4+
LL | test1(i);
5+
| ^^^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: call to unsafe function `test1` is unsafe and requires unsafe block
2+
--> $DIR/unsafe-items.rs:17:5
3+
|
4+
LL | test1(i);
5+
| ^^^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ revisions: edition2021 edition2024
2+
//@[edition2021] edition:2021
3+
//@[edition2024] edition:2024
4+
//@[edition2024] compile-flags: -Zunstable-options
5+
6+
unsafe extern "C" {
7+
unsafe fn test1(i: i32);
8+
}
9+
10+
fn test2(i: i32) {
11+
unsafe {
12+
test1(i);
13+
}
14+
}
15+
16+
fn test3(i: i32) {
17+
test1(i);
18+
//~^ ERROR: call to unsafe function `test1` is unsafe
19+
}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)