Skip to content

Commit a1fb6f3

Browse files
committed
error on unsafe attributes in pre-2024 editions
the `no_mangle`, `link_section` and `export_name` attributes are exceptions, and can still be used without an unsafe in earlier editions
1 parent 81d8c74 commit a1fb6f3

15 files changed

+82
-40
lines changed

Diff for: compiler/rustc_error_codes/src/error_codes/E0756.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Erroneous code example:
66
```compile_fail,E0756
77
#![feature(ffi_const)]
88
9-
#[ffi_const] // error!
9+
#[unsafe(ffi_const)] // error!
1010
pub fn foo() {}
1111
# fn main() {}
1212
```
@@ -18,7 +18,7 @@ which have no side effects except for their return value:
1818
#![feature(ffi_const)]
1919
2020
extern "C" {
21-
#[ffi_const] // ok!
21+
#[unsafe(ffi_const)] // ok!
2222
pub fn strlen(s: *const i8) -> i32;
2323
}
2424
# fn main() {}

Diff for: compiler/rustc_error_codes/src/error_codes/E0757.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ Erroneous code example:
66
#![feature(ffi_const, ffi_pure)]
77
88
extern "C" {
9-
#[ffi_const]
10-
#[ffi_pure] // error: `#[ffi_const]` function cannot be `#[ffi_pure]`
9+
#[unsafe(ffi_const)]
10+
#[unsafe(ffi_pure)]
11+
//~^ ERROR `#[ffi_const]` function cannot be `#[ffi_pure]`
1112
pub fn square(num: i32) -> i32;
1213
}
1314
```
@@ -19,7 +20,7 @@ As `ffi_const` provides stronger guarantees than `ffi_pure`, remove the
1920
#![feature(ffi_const)]
2021
2122
extern "C" {
22-
#[ffi_const]
23+
#[unsafe(ffi_const)]
2324
pub fn square(num: i32) -> i32;
2425
}
2526
```

Diff for: compiler/rustc_parse/src/validate_attr.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,20 @@ pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr:
167167
// square bracket respectively.
168168
let diag_span = attr_item.span();
169169

170-
if attr.span.at_least_rust_2024() {
170+
// Attributes that were safe before edition 2024, but are unsafe afterwards.
171+
const SAFE_BEFORE_RUST_2024: &[Symbol] =
172+
&[sym::export_name, sym::link_section, sym::no_mangle];
173+
174+
// For attributes whose unsafety changed in edition 2024, only emit the error when
175+
// compiling for that edition or later. Other unsafe attributes will error even in
176+
// earlier editions.
177+
let emit_error = if SAFE_BEFORE_RUST_2024.iter().any(|s| attr.has_name(*s)) {
178+
attr.span.at_least_rust_2024()
179+
} else {
180+
true
181+
};
182+
183+
if emit_error {
171184
psess.dcx().emit_err(errors::UnsafeAttrOutsideUnsafe {
172185
span: path_span,
173186
suggestion: errors::UnsafeAttrOutsideUnsafeSuggestion {

Diff for: tests/codegen/cffi/ffi-const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ extern "C" {
1010
// CHECK-LABEL: declare{{.*}}void @foo()
1111
// CHECK-SAME: [[ATTRS:#[0-9]+]]
1212
// CHECK-DAG: attributes [[ATTRS]] = { {{.*}}memory(none){{.*}} }
13-
#[ffi_const]
13+
#[unsafe(ffi_const)]
1414
pub fn foo();
1515
}

Diff for: tests/codegen/cffi/ffi-pure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ extern "C" {
1010
// CHECK-LABEL: declare{{.*}}void @foo()
1111
// CHECK-SAME: [[ATTRS:#[0-9]+]]
1212
// CHECK-DAG: attributes [[ATTRS]] = { {{.*}}memory(read){{.*}} }
13-
#[ffi_pure]
13+
#[unsafe(ffi_pure)]
1414
pub fn foo();
1515
}

Diff for: tests/ui/feature-gates/feature-gate-ffi_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![crate_type = "lib"]
22

33
extern "C" {
4-
#[ffi_const] //~ ERROR the `#[ffi_const]` attribute is an experimental feature
4+
#[unsafe(ffi_const)] //~ ERROR the `#[ffi_const]` attribute is an experimental feature
55
pub fn foo();
66
}

Diff for: tests/ui/feature-gates/feature-gate-ffi_const.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: the `#[ffi_const]` attribute is an experimental feature
22
--> $DIR/feature-gate-ffi_const.rs:4:5
33
|
4-
LL | #[ffi_const]
5-
| ^^^^^^^^^^^^
4+
LL | #[unsafe(ffi_const)]
5+
| ^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: see issue #58328 <https://github.com/rust-lang/rust/issues/58328> for more information
88
= help: add `#![feature(ffi_const)]` to the crate attributes to enable

Diff for: tests/ui/feature-gates/feature-gate-ffi_pure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![crate_type = "lib"]
22

33
extern "C" {
4-
#[ffi_pure] //~ ERROR the `#[ffi_pure]` attribute is an experimental feature
4+
#[unsafe(ffi_pure)] //~ ERROR the `#[ffi_pure]` attribute is an experimental feature
55
pub fn foo();
66
}

Diff for: tests/ui/feature-gates/feature-gate-ffi_pure.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: the `#[ffi_pure]` attribute is an experimental feature
22
--> $DIR/feature-gate-ffi_pure.rs:4:5
33
|
4-
LL | #[ffi_pure]
5-
| ^^^^^^^^^^^
4+
LL | #[unsafe(ffi_pure)]
5+
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: see issue #58329 <https://github.com/rust-lang/rust/issues/58329> for more information
88
= help: add `#![feature(ffi_pure)]` to the crate attributes to enable

Diff for: tests/ui/ffi-attrs/ffi_const.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#![feature(ffi_const)]
22
#![crate_type = "lib"]
33

4-
#[ffi_const] //~ ERROR `#[ffi_const]` may only be used on foreign functions
4+
#[unsafe(ffi_const)] //~ ERROR `#[ffi_const]` may only be used on foreign functions
55
pub fn foo() {}
66

7-
#[ffi_const] //~ ERROR `#[ffi_const]` may only be used on foreign functions
7+
#[unsafe(ffi_const)] //~ ERROR `#[ffi_const]` may only be used on foreign functions
88
macro_rules! bar {
9-
() => ()
9+
() => {};
1010
}
1111

1212
extern "C" {
13-
#[ffi_const] //~ ERROR `#[ffi_const]` may only be used on foreign functions
13+
#[unsafe(ffi_const)] //~ ERROR `#[ffi_const]` may only be used on foreign functions
1414
static INT: i32;
15+
16+
#[ffi_const] //~ ERROR unsafe attribute used without unsafe
17+
fn bar();
1518
}

Diff for: tests/ui/ffi-attrs/ffi_const.stderr

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
error: unsafe attribute used without unsafe
2+
--> $DIR/ffi_const.rs:16:7
3+
|
4+
LL | #[ffi_const]
5+
| ^^^^^^^^^ usage of unsafe attribute
6+
|
7+
help: wrap the attribute in `unsafe(...)`
8+
|
9+
LL | #[unsafe(ffi_const)]
10+
| +++++++ +
11+
112
error[E0756]: `#[ffi_const]` may only be used on foreign functions
213
--> $DIR/ffi_const.rs:4:1
314
|
4-
LL | #[ffi_const]
5-
| ^^^^^^^^^^^^
15+
LL | #[unsafe(ffi_const)]
16+
| ^^^^^^^^^^^^^^^^^^^^
617

718
error[E0756]: `#[ffi_const]` may only be used on foreign functions
819
--> $DIR/ffi_const.rs:7:1
920
|
10-
LL | #[ffi_const]
11-
| ^^^^^^^^^^^^
21+
LL | #[unsafe(ffi_const)]
22+
| ^^^^^^^^^^^^^^^^^^^^
1223

1324
error[E0756]: `#[ffi_const]` may only be used on foreign functions
1425
--> $DIR/ffi_const.rs:13:5
1526
|
16-
LL | #[ffi_const]
17-
| ^^^^^^^^^^^^
27+
LL | #[unsafe(ffi_const)]
28+
| ^^^^^^^^^^^^^^^^^^^^
1829

19-
error: aborting due to 3 previous errors
30+
error: aborting due to 4 previous errors
2031

2132
For more information about this error, try `rustc --explain E0756`.

Diff for: tests/ui/ffi-attrs/ffi_const2.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![feature(ffi_const, ffi_pure)]
22

33
extern "C" {
4-
#[ffi_pure] //~ ERROR `#[ffi_const]` function cannot be `#[ffi_pure]`
5-
#[ffi_const]
4+
#[unsafe(ffi_pure)] //~ ERROR `#[ffi_const]` function cannot be `#[ffi_pure]`
5+
#[unsafe(ffi_const)]
66
pub fn baz();
77
}
88

Diff for: tests/ui/ffi-attrs/ffi_const2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0757]: `#[ffi_const]` function cannot be `#[ffi_pure]`
22
--> $DIR/ffi_const2.rs:4:5
33
|
4-
LL | #[ffi_pure]
5-
| ^^^^^^^^^^^
4+
LL | #[unsafe(ffi_pure)]
5+
| ^^^^^^^^^^^^^^^^^^^
66

77
error: aborting due to 1 previous error
88

Diff for: tests/ui/ffi-attrs/ffi_pure.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#![feature(ffi_pure)]
22
#![crate_type = "lib"]
33

4-
#[ffi_pure] //~ ERROR `#[ffi_pure]` may only be used on foreign functions
4+
#[unsafe(ffi_pure)] //~ ERROR `#[ffi_pure]` may only be used on foreign functions
55
pub fn foo() {}
66

7-
#[ffi_pure] //~ ERROR `#[ffi_pure]` may only be used on foreign functions
7+
#[unsafe(ffi_pure)] //~ ERROR `#[ffi_pure]` may only be used on foreign functions
88
macro_rules! bar {
9-
() => ()
9+
() => {};
1010
}
1111

1212
extern "C" {
13-
#[ffi_pure] //~ ERROR `#[ffi_pure]` may only be used on foreign functions
13+
#[unsafe(ffi_pure)] //~ ERROR `#[ffi_pure]` may only be used on foreign functions
1414
static INT: i32;
15+
16+
#[ffi_pure] //~ ERROR unsafe attribute used without unsafe
17+
fn bar();
1518
}

Diff for: tests/ui/ffi-attrs/ffi_pure.stderr

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
error: unsafe attribute used without unsafe
2+
--> $DIR/ffi_pure.rs:16:7
3+
|
4+
LL | #[ffi_pure]
5+
| ^^^^^^^^ usage of unsafe attribute
6+
|
7+
help: wrap the attribute in `unsafe(...)`
8+
|
9+
LL | #[unsafe(ffi_pure)]
10+
| +++++++ +
11+
112
error[E0755]: `#[ffi_pure]` may only be used on foreign functions
213
--> $DIR/ffi_pure.rs:4:1
314
|
4-
LL | #[ffi_pure]
5-
| ^^^^^^^^^^^
15+
LL | #[unsafe(ffi_pure)]
16+
| ^^^^^^^^^^^^^^^^^^^
617

718
error[E0755]: `#[ffi_pure]` may only be used on foreign functions
819
--> $DIR/ffi_pure.rs:7:1
920
|
10-
LL | #[ffi_pure]
11-
| ^^^^^^^^^^^
21+
LL | #[unsafe(ffi_pure)]
22+
| ^^^^^^^^^^^^^^^^^^^
1223

1324
error[E0755]: `#[ffi_pure]` may only be used on foreign functions
1425
--> $DIR/ffi_pure.rs:13:5
1526
|
16-
LL | #[ffi_pure]
17-
| ^^^^^^^^^^^
27+
LL | #[unsafe(ffi_pure)]
28+
| ^^^^^^^^^^^^^^^^^^^
1829

19-
error: aborting due to 3 previous errors
30+
error: aborting due to 4 previous errors
2031

2132
For more information about this error, try `rustc --explain E0755`.

0 commit comments

Comments
 (0)