about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSantiago Pastorino <spastorino@gmail.com>2024-06-28 23:14:09 -0300
committerSantiago Pastorino <spastorino@gmail.com>2024-06-29 14:40:32 -0300
commit15d5dac32ecd54745d6f61659628286bd2678e03 (patch)
tree6e5d3c8c1f61baff274d244404eb99a3bed9450d
parenta62cbda57e23105d68d146cafce94b882882c0e1 (diff)
downloadrust-15d5dac32ecd54745d6f61659628286bd2678e03.tar.gz
rust-15d5dac32ecd54745d6f61659628286bd2678e03.zip
Avoid suggesting to add unsafe when the extern block is already unsafe
-rw-r--r--compiler/rustc_ast_passes/src/ast_validation.rs19
-rw-r--r--compiler/rustc_ast_passes/src/errors.rs2
-rw-r--r--tests/ui/parser/unsafe-foreign-mod-2.stderr5
3 files changed, 13 insertions, 13 deletions
diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs
index d02b8510975..60690a7e2cd 100644
--- a/compiler/rustc_ast_passes/src/ast_validation.rs
+++ b/compiler/rustc_ast_passes/src/ast_validation.rs
@@ -459,13 +459,18 @@ impl<'a> AstValidator<'a> {
     fn check_item_safety(&self, span: Span, safety: Safety) {
         match self.extern_mod_safety {
             Some(extern_safety) => {
-                if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_))
-                    && (extern_safety == Safety::Default || !self.features.unsafe_extern_blocks)
-                {
-                    self.dcx().emit_err(errors::InvalidSafetyOnExtern {
-                        item_span: span,
-                        block: self.current_extern_span().shrink_to_lo(),
-                    });
+                if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_)) {
+                    if extern_safety == Safety::Default {
+                        self.dcx().emit_err(errors::InvalidSafetyOnExtern {
+                            item_span: span,
+                            block: Some(self.current_extern_span().shrink_to_lo()),
+                        });
+                    } else if !self.features.unsafe_extern_blocks {
+                        self.dcx().emit_err(errors::InvalidSafetyOnExtern {
+                            item_span: span,
+                            block: None,
+                        });
+                    }
                 }
             }
             None => {
diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs
index 965d8fac712..bfb90476450 100644
--- a/compiler/rustc_ast_passes/src/errors.rs
+++ b/compiler/rustc_ast_passes/src/errors.rs
@@ -222,7 +222,7 @@ pub struct InvalidSafetyOnExtern {
     #[primary_span]
     pub item_span: Span,
     #[suggestion(code = "unsafe ", applicability = "machine-applicable", style = "verbose")]
-    pub block: Span,
+    pub block: Option<Span>,
 }
 
 #[derive(Diagnostic)]
diff --git a/tests/ui/parser/unsafe-foreign-mod-2.stderr b/tests/ui/parser/unsafe-foreign-mod-2.stderr
index 07dbd5568d0..8bd592b5d43 100644
--- a/tests/ui/parser/unsafe-foreign-mod-2.stderr
+++ b/tests/ui/parser/unsafe-foreign-mod-2.stderr
@@ -19,11 +19,6 @@ error: items in unadorned `extern` blocks cannot have safety qualifiers
    |
 LL |     unsafe fn foo();
    |     ^^^^^^^^^^^^^^^^
-   |
-help: add unsafe to this `extern` block
-   |
-LL | unsafe extern "C" unsafe {
-   | ++++++
 
 error: aborting due to 3 previous errors