Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions compiler/rustc_attr_parsing/src/validate_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub fn check_attribute_safety(

// - Normal builtin attribute
// - Writing `#[unsafe(..)]` is not permitted on normal builtin attributes
(Some(AttributeSafety::Normal), Safety::Unsafe(unsafe_span)) => {
(None | Some(AttributeSafety::Normal), Safety::Unsafe(unsafe_span)) => {
psess.dcx().emit_err(errors::InvalidAttrUnsafe {
span: unsafe_span,
name: attr_item.path.clone(),
Expand All @@ -218,15 +218,10 @@ pub fn check_attribute_safety(

// - Normal builtin attribute
// - No explicit `#[unsafe(..)]` written.
(Some(AttributeSafety::Normal), Safety::Default) => {
(None | Some(AttributeSafety::Normal), Safety::Default) => {
// OK
}

// - Non-builtin attribute
(None, Safety::Unsafe(_) | Safety::Default) => {
// OK (not checked here)
}

(
Some(AttributeSafety::Unsafe { .. } | AttributeSafety::Normal) | None,
Safety::Safe(..),
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,9 +886,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
}
} else if let SyntaxExtensionKind::NonMacroAttr = ext {
if let ast::Safety::Unsafe(span) = attr.get_normal_item().unsafety {
self.cx.dcx().span_err(span, "unnecessary `unsafe` on safe attribute");
}
// `-Zmacro-stats` ignores these because they don't do any real expansion.
self.cx.expanded_inert_attrs.mark(&attr);
item.visit_attrs(|attrs| attrs.insert(pos, attr));
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/attributes/proc-macro-unsafe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ proc-macro: external-macro-use.rs

extern crate external_macro_use;

#[unsafe(external_macro_use::a)]
//~^ ERROR unnecessary `unsafe` on safe attribute
fn f() {}

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/attributes/proc-macro-unsafe.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unnecessary `unsafe` on safe attribute
--> $DIR/proc-macro-unsafe.rs:5:3
|
LL | #[unsafe(external_macro_use::a)]
| ^^^^^^

error: aborting due to 1 previous error

2 changes: 1 addition & 1 deletion tests/ui/attributes/unsafe/double-unsafe-attributes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[unsafe(unsafe(no_mangle))]
//~^ ERROR expected identifier, found keyword `unsafe`
//~| ERROR cannot find attribute `r#unsafe` in this scope
//~| ERROR unnecessary `unsafe`
//~| ERROR `r#unsafe` is not an unsafe attribute
fn a() {}

fn main() {}
6 changes: 4 additions & 2 deletions tests/ui/attributes/unsafe/double-unsafe-attributes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ help: escape `unsafe` to use it as an identifier
LL | #[unsafe(r#unsafe(no_mangle))]
| ++

error: unnecessary `unsafe` on safe attribute
error: `r#unsafe` is not an unsafe attribute
--> $DIR/double-unsafe-attributes.rs:1:3
|
LL | #[unsafe(unsafe(no_mangle))]
| ^^^^^^
| ^^^^^^ this is not an unsafe attribute
|
= note: extraneous unsafe is not allowed in attributes

error: cannot find attribute `r#unsafe` in this scope
--> $DIR/double-unsafe-attributes.rs:1:10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[unsafe(diagnostic::on_unimplemented( //~ ERROR: unnecessary `unsafe`
#[unsafe(diagnostic::on_unimplemented( //~ ERROR: `diagnostic::on_unimplemented` is not an unsafe attribute
message = "testing",
))]
trait Foo {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: unnecessary `unsafe` on safe attribute
error: `diagnostic::on_unimplemented` is not an unsafe attribute
--> $DIR/unsafe-safe-attribute_diagnostic.rs:1:3
|
LL | #[unsafe(diagnostic::on_unimplemented(
| ^^^^^^
| ^^^^^^ this is not an unsafe attribute
|
= note: extraneous unsafe is not allowed in attributes

error: aborting due to 1 previous error

Loading