about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSantiago Pastorino <spastorino@gmail.com>2024-04-26 18:35:54 -0300
committerSantiago Pastorino <spastorino@gmail.com>2024-06-04 14:19:42 -0300
commitbbddc9b58f3b2fcf1b624c2d0cabd49461f94575 (patch)
treee3512342f9e0b6e281870c861b2ee6312d1e5cea
parent3ba8de0b601ee1eaae710510ee8b0b8a9dbd78d8 (diff)
downloadrust-bbddc9b58f3b2fcf1b624c2d0cabd49461f94575.tar.gz
rust-bbddc9b58f3b2fcf1b624c2d0cabd49461f94575.zip
Allow using unsafe on functions inside extern blocks
-rw-r--r--compiler/rustc_ast_passes/src/ast_validation.rs6
-rw-r--r--tests/ui/parser/fn-header-semantic-fail.rs6
-rw-r--r--tests/ui/parser/fn-header-semantic-fail.stderr20
-rw-r--r--tests/ui/parser/no-const-fn-in-extern-block.rs1
-rw-r--r--tests/ui/parser/no-const-fn-in-extern-block.stderr11
-rw-r--r--tests/ui/parser/unsafe-foreign-mod-2.rs1
-rw-r--r--tests/ui/parser/unsafe-foreign-mod-2.stderr11
-rw-r--r--tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.edition2021.stderr11
-rw-r--r--tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.edition2024.stderr11
-rw-r--r--tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.rs21
10 files changed, 50 insertions, 49 deletions
diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs
index 7e1094da202..b405e888561 100644
--- a/compiler/rustc_ast_passes/src/ast_validation.rs
+++ b/compiler/rustc_ast_passes/src/ast_validation.rs
@@ -519,7 +519,7 @@ 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 {
@@ -527,10 +527,6 @@ impl<'a> AstValidator<'a> {
                 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 => (),
diff --git a/tests/ui/parser/fn-header-semantic-fail.rs b/tests/ui/parser/fn-header-semantic-fail.rs
index 6ed173b6854..3b98433cdd8 100644
--- a/tests/ui/parser/fn-header-semantic-fail.rs
+++ b/tests/ui/parser/fn-header-semantic-fail.rs
@@ -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`
diff --git a/tests/ui/parser/fn-header-semantic-fail.stderr b/tests/ui/parser/fn-header-semantic-fail.stderr
index cfc54839eb7..dc2a216754d 100644
--- a/tests/ui/parser/fn-header-semantic-fail.stderr
+++ b/tests/ui/parser/fn-header-semantic-fail.stderr
@@ -79,15 +79,6 @@ 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
    |
 LL |     extern "C" {
@@ -106,15 +97,6 @@ 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
    |
 LL |     extern "C" {
@@ -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`.
diff --git a/tests/ui/parser/no-const-fn-in-extern-block.rs b/tests/ui/parser/no-const-fn-in-extern-block.rs
index d6c578681cc..1993124edc3 100644
--- a/tests/ui/parser/no-const-fn-in-extern-block.rs
+++ b/tests/ui/parser/no-const-fn-in-extern-block.rs
@@ -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() {}
diff --git a/tests/ui/parser/no-const-fn-in-extern-block.stderr b/tests/ui/parser/no-const-fn-in-extern-block.stderr
index 948ce669112..2dec6f76931 100644
--- a/tests/ui/parser/no-const-fn-in-extern-block.stderr
+++ b/tests/ui/parser/no-const-fn-in-extern-block.stderr
@@ -7,15 +7,6 @@ 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
    |
 LL | extern "C" {
@@ -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
 
diff --git a/tests/ui/parser/unsafe-foreign-mod-2.rs b/tests/ui/parser/unsafe-foreign-mod-2.rs
index c0994090eff..6d339cd9088 100644
--- a/tests/ui/parser/unsafe-foreign-mod-2.rs
+++ b/tests/ui/parser/unsafe-foreign-mod-2.rs
@@ -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() {}
diff --git a/tests/ui/parser/unsafe-foreign-mod-2.stderr b/tests/ui/parser/unsafe-foreign-mod-2.stderr
index 620d042540e..0625e3362ed 100644
--- a/tests/ui/parser/unsafe-foreign-mod-2.stderr
+++ b/tests/ui/parser/unsafe-foreign-mod-2.stderr
@@ -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
 
diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.edition2021.stderr b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.edition2021.stderr
new file mode 100644
index 00000000000..67e582c7c56
--- /dev/null
+++ b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.edition2021.stderr
@@ -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`.
diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.edition2024.stderr b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.edition2024.stderr
new file mode 100644
index 00000000000..8a9410f8f13
--- /dev/null
+++ b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.edition2024.stderr
@@ -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`.
diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.rs b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.rs
new file mode 100644
index 00000000000..4b6d077e987
--- /dev/null
+++ b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-items.rs
@@ -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() {}