Skip to content

Commit c8618d2

Browse files
Refactor safety checking for attributes
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
1 parent 0bbef55 commit c8618d2

File tree

6 files changed

+12
-16
lines changed

6 files changed

+12
-16
lines changed

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub fn check_attribute_safety(
209209

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

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

225-
// - Non-builtin attribute
226-
(None, Safety::Unsafe(_) | Safety::Default) => {
227-
// OK (not checked here)
228-
}
229-
230225
(
231226
Some(AttributeSafety::Unsafe { .. } | AttributeSafety::Normal) | None,
232227
Safety::Safe(..),

compiler/rustc_expand/src/expand.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
886886
}
887887
}
888888
} else if let SyntaxExtensionKind::NonMacroAttr = ext {
889-
if let ast::Safety::Unsafe(span) = attr.get_normal_item().unsafety {
890-
self.cx.dcx().span_err(span, "unnecessary `unsafe` on safe attribute");
891-
}
892889
// `-Zmacro-stats` ignores these because they don't do any real expansion.
893890
self.cx.expanded_inert_attrs.mark(&attr);
894891
item.visit_attrs(|attrs| attrs.insert(pos, attr));
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[unsafe(unsafe(no_mangle))]
22
//~^ ERROR expected identifier, found keyword `unsafe`
33
//~| ERROR cannot find attribute `r#unsafe` in this scope
4-
//~| ERROR unnecessary `unsafe`
4+
//~| ERROR `r#unsafe` is not an unsafe attribute
55
fn a() {}
66

77
fn main() {}

tests/ui/attributes/unsafe/double-unsafe-attributes.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ help: escape `unsafe` to use it as an identifier
99
LL | #[unsafe(r#unsafe(no_mangle))]
1010
| ++
1111

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

1820
error: cannot find attribute `r#unsafe` in this scope
1921
--> $DIR/double-unsafe-attributes.rs:1:10

tests/ui/attributes/unsafe/unsafe-safe-attribute_diagnostic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[unsafe(diagnostic::on_unimplemented( //~ ERROR: unnecessary `unsafe`
1+
#[unsafe(diagnostic::on_unimplemented( //~ ERROR: `diagnostic::on_unimplemented` is not an unsafe attribute
22
message = "testing",
33
))]
44
trait Foo {}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: unnecessary `unsafe` on safe attribute
1+
error: `diagnostic::on_unimplemented` is not an unsafe attribute
22
--> $DIR/unsafe-safe-attribute_diagnostic.rs:1:3
33
|
44
LL | #[unsafe(diagnostic::on_unimplemented(
5-
| ^^^^^^
5+
| ^^^^^^ this is not an unsafe attribute
6+
|
7+
= note: extraneous unsafe is not allowed in attributes
68

79
error: aborting due to 1 previous error
810

0 commit comments

Comments
 (0)